C#添加PDF頁眉——添加文本、圖片到頁眉


頁眉常用於顯示文檔的附加信息,我們可以在頁眉中插入文本或者圖形,例如,頁碼、日期、公司徽標、文檔標題、文件名或作者名等等。那么我們如何以編程的方式添加頁眉呢?今天,這篇文章向大家分享如何使用了免費組件Free Spire.PDF給PDF文檔添加文本和圖片頁眉。這個組件提供了一些方法,可以幫助我們快速方便地實現此目的。

添加頁眉步驟:

首先,創建一個Visual C#控制台項目,添加組件引用並使用以下命名空間。

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

在下列代碼中,我們先定義一個SetDocumentTemplate()方法來創建一個PDF文檔模板,這個模板只包含文本和圖片頁眉。然后,調用DrawString(string s, PdfFontBase font, PdfBrush brush, float x, float y, PdfStringFormat format)方法和DrawImage(PdfImage image, float x, float y, float width, float height)方法插入自定義的文本和圖片頁眉。

static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
{
    //創建PDF模板
    PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top);
    topSpace.Foreground = true;
    doc.Template.Top = topSpace;
    //添加文本頁眉
    PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("宋體", 15f), true);
    PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
    String  Text = "PDF文本頁眉";
    float y = 0;
    float x = PdfPageSize.A4.Width;
     topSpace.Graphics.DrawString(Text, font1, PdfBrushes.PaleVioletRed, x, y, format);  
    //添加圖片頁眉
    PdfImage headerImage = PdfImage.FromFile(@"logo.png");
    float width = headerImage.Width;
    float height = headerImage.Height;
    PointF pageLeftTop = new PointF(0 , 0);
    topSpace.Graphics.DrawImage(headerImage,0,0,width/2,height/2);         
}

接下來再創建一個PDF文檔,主函數內調用SetDocumentTemplate()方法將帶有文本和圖片頁眉的模板應用到新建的PDF文檔中。具體步驟:

第一步:創建一個PDF文檔對象。

PdfDocument doc = new PdfDocument();

第二步:設置頁邊距。

PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(4.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;       

第三步:PDF文檔中應用模板。

SetDocumentTemplate(doc, PdfPageSize.A4, margin);

第四步:PDF文檔添加頁面。

PdfPageBase page = doc.Pages.Add();
doc.Pages.Add();

第五步:保存並打開文檔。

doc.SaveToFile("頁眉.pdf");
System.Diagnostics.Process.Start("頁眉.pdf");

添加頁眉后的效果圖:

全部代碼:

 1 using System;
 2 using Spire.Pdf;
 3 using System.Drawing;
 4 using Spire.Pdf.Graphics;
 5 
 6 namespace PDF添加頁眉
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             PdfDocument doc = new PdfDocument();
13 
14             PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
15             PdfMargins margin = new PdfMargins();
16             margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
17             margin.Bottom = margin.Top;
18             margin.Left = unitCvtr.ConvertUnits(4.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
19             margin.Right = margin.Left;
20 
21             SetDocumentTemplate(doc, PdfPageSize.A4, margin);
22             PdfPageBase page = doc.Pages.Add();
23             doc.Pages.Add();
24 
25             doc.SaveToFile("頁眉.pdf");
26             System.Diagnostics.Process.Start("頁眉.pdf");
27         }
28 
29         static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
30         {
31             PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top);
32             topSpace.Foreground = true;
33             doc.Template.Top = topSpace;
34            
35             PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("宋體", 15f), true);
36             PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
37             String  Text = "PDF文本頁眉";
38             float y = 0;
39             float x = PdfPageSize.A4.Width;
40             topSpace.Graphics.DrawString(Text, font1, PdfBrushes.PaleVioletRed, x, y, format);
41             
42             PdfImage headerImage = PdfImage.FromFile(@"C:\Users\Administrator\Pictures\under_construction.jpg");
43             float width = headerImage.Width;
44             float height = headerImage.Height;
45             PointF pageLeftTop = new PointF(0, 0);
46             topSpace.Graphics.DrawImage(headerImage, 0, 0, width / 2, height / 2);
47         }
48     }
49 }
View Code

謝謝瀏覽!

 


免責聲明!

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



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