C# 給現有PDF文檔添加頁眉、頁腳


概述

頁眉頁腳是一篇完整、精致的文檔的重要組成部分。在頁眉頁腳處,可以呈現的內容很多,如公司名稱、頁碼、工作表名、日期、圖片,如LOGO、標記等。在之前的文章中介紹了如何通過新建一頁空白PDF頁來添加頁眉到該頁面,包括文字頁面、圖片頁眉。但是在實際應用中,該方法會有一定局限性,通過測試,下面將介紹C#給現有的PDF文檔添加頁眉頁腳的方法。該方法中,豐富了我們對於添加頁眉頁腳的內容形式,包括添加圖片、文字、超鏈接、頁碼等。

使用工具

注:下載該類庫后,注意在程序中添加引用Spire.Pdf.dll(dll文件可在安裝路徑下的Bin文件夾中獲取)

 

C# 代碼步驟(供參考)

步驟 1 :添加using指令

using Spire.Pdf;
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;

步驟 2 :加載測試文檔

//實例化PdfDocument類,加載測試文檔
PdfDocument existingPdf = new PdfDocument();
existingPdf.LoadFromFile("Test.pdf");

步驟 3 :添加頁眉頁腳

//調用DrawHeader()方法在現有文檔添加頁眉
DrawHeader(existingPdf);

//調用DrawFooter()方法在現有文檔添加頁腳
DrawFooter(existingPdf);

注:這里需要自定義方法來分別添加頁眉、頁腳到PDF文檔。

自定義方法添加頁眉:

//在頁面上方空白部位繪制頁眉
static void DrawHeader(PdfDocument doc)
{
    //獲取頁面大小
    SizeF pageSize = doc.Pages[0].Size;

    //聲明x,y兩個float型變量
    float x = 90;
    float y = 20;

    for (int i = 0; i < doc.Pages.Count; i++)
    {
        //在每一頁的指定位置繪制圖片
        PdfImage headerImage = PdfImage.FromFile("logo.png");
        float width = headerImage.Width / 7;
        float height = headerImage.Height / 7;
        doc.Pages[i].Canvas.DrawImage(headerImage, x, y, width, height);

        //在每一頁的指定位置繪制橫線
        PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
        doc.Pages[i].Canvas.DrawLine(pen, x, y + height + 2, pageSize.Width - x, y + height + 2);
    }
}

自定義方法添加頁腳:

//在頁面下方空白部位繪制頁腳
static void DrawFooter(PdfDocument doc)
{
    //獲取頁面大小
    SizeF pageSize = doc.Pages[0].Size;

    //聲明x,y兩個float型變量
    float x = 90;
    float y = pageSize.Height - 72;

    for (int i = 0; i < doc.Pages.Count; i++)
    {
        //在每一頁的指定位置繪制橫線
        PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
        doc.Pages[i].Canvas.DrawLine(pen, x, y, pageSize.Width - x, y);

        //在每一頁的指定位置繪制文字
        y = y + 5;
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("黑體", 10f, FontStyle.Bold), true);
        PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
        String footerText = " Website\n https://g20.org/";
        doc.Pages[i].Canvas.DrawString(footerText, font, PdfBrushes.Black, x, y, format);  
       
        //在每一頁的指定位置當前頁碼和總頁碼
        PdfPageNumberField number = new PdfPageNumberField();
        PdfPageCountField count = new PdfPageCountField();
        PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, "{0}/{1}", number, count);
        compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top);
        SizeF size = font.MeasureString(compositeField.Text);
        compositeField.Bounds = new RectangleF(pageSize.Width - x - size.Width, y, size.Width, size.Height);
        compositeField.Draw(doc.Pages[i].Canvas);
    }
}

步驟 4 :保存文檔

existingPdf.SaveToFile("output.pdf");
System.Diagnostics.Process.Start("output.pdf");

代碼完成后,調試運行程序,生成文檔。打開文檔后,效果如下:

 全部代碼:

using Spire.Pdf;
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;

namespace PdfHeader
{
    class Program
    {
        static void Main(string[] args)
        {
            //加載一個測試文檔 
            PdfDocument existingPdf = new PdfDocument();
            existingPdf.LoadFromFile("Test.pdf");

            //調用DrawHeader()方法在現有文檔添加頁眉
            DrawHeader(existingPdf);

            //調用DrawFooter()方法在現有文檔添加頁腳
            DrawFooter(existingPdf);

            //保存並打開文檔
            existingPdf.SaveToFile("output.pdf");
            System.Diagnostics.Process.Start("output.pdf");

        }
        //在頁面上方空白部位繪制頁眉
        static void DrawHeader(PdfDocument doc)
        {
            //獲取頁面大小
            SizeF pageSize = doc.Pages[0].Size;

            //聲明x,y兩個float型變量
            float x = 90;
            float y = 20;

            for (int i = 0; i < doc.Pages.Count; i++)
            {
                //在每一頁的指定位置繪制圖片
                PdfImage headerImage = PdfImage.FromFile("logo.png");
                float width = headerImage.Width / 7;
                float height = headerImage.Height / 7;
                doc.Pages[i].Canvas.DrawImage(headerImage, x, y, width, height);

                //在每一頁的指定位置繪制橫線
                PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
                doc.Pages[i].Canvas.DrawLine(pen, x, y + height + 2, pageSize.Width - x, y + height + 2);
            }
        }

        //在頁面下方空白部位繪制頁腳
        static void DrawFooter(PdfDocument doc)
        {
            //獲取頁面大小
            SizeF pageSize = doc.Pages[0].Size;

            //聲明x,y兩個float型變量
            float x = 90;
            float y = pageSize.Height - 72;

            for (int i = 0; i < doc.Pages.Count; i++)
            {
                //在每一頁的指定位置繪制橫線
                PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
                doc.Pages[i].Canvas.DrawLine(pen, x, y, pageSize.Width - x, y);

                //在每一頁的指定位置繪制文字
                y = y + 5;
                PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("黑體", 10f, FontStyle.Bold), true);
                PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
                String footerText = " Website\n https://g20.org/";
                doc.Pages[i].Canvas.DrawString(footerText, font, PdfBrushes.Black, x, y, format);  
               
                //在每一頁的指定位置當前頁碼和總頁碼
                PdfPageNumberField number = new PdfPageNumberField();
                PdfPageCountField count = new PdfPageCountField();
                PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, "{0}/{1}", number, count);
                compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top);
                SizeF size = font.MeasureString(compositeField.Text);
                compositeField.Bounds = new RectangleF(pageSize.Width - x - size.Width, y, size.Width, size.Height);
                compositeField.Draw(doc.Pages[i].Canvas);
            }
        }
    }
}
View Code

總結

相較於上篇文章中的添加頁眉的方法,本方法在處理現有的PDF文檔中更具實用性。當然,兩種方法針對不同的程序設計需要,滿足不同的需求,我們在選擇這兩種方法時,可酌情而定。

(本文完)

如需轉載,請注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM