C#给PDF文档添加文本和图片页眉

简介: 页眉常用于显示文档的附加信息,我们可以在页眉中插入文本或者图形,例如,页码、日期、公司徽标、文档标题、文件名或作者名等等。那么我们如何以编程的方式添加页眉呢?今天,这篇文章向大家分享如何使用了免费组件Free Spire.PDF给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

谢谢浏览!

 

目录
相关文章
|
1月前
|
数据挖掘 数据安全/隐私保护 开发者
使用Spire.PDF for Python插件从PDF文件提取文字和图片信息
使用Spire.PDF for Python插件从PDF文件提取文字和图片信息
62 0
|
2月前
|
应用服务中间件
使用 Adobe Livecycle Enterprise service 将 word 文档转换成 PDF 格式
使用 Adobe Livecycle Enterprise service 将 word 文档转换成 PDF 格式
28 0
|
2月前
|
程序员 数据安全/隐私保护 计算机视觉
手把手教你用 Python 去除图片和 PDF 水印
手把手教你用 Python 去除图片和 PDF 水印
84 0
|
3月前
|
Ubuntu Java Linux
在Spring Boot中使用iTextPDF创建动态PDF文档
iTextPDF 是一个用于创建和操作 PDF(Portable Document Format)文档的流行的 Java 库。它提供了一套全面的功能,用于处理 PDF 文件,包括创建新文档、修改现有文档以及提取信息。
84 1
|
2月前
|
编解码 数据可视化 数据挖掘
【办公自动化】用Python将PDF文件转存为图片
【办公自动化】用Python将PDF文件转存为图片
62 1
|
25天前
|
存储 SQL 数据库
C# 将 Word 转文本存储到数据库并进行管理
C# 将 Word 转文本存储到数据库并进行管理
|
25天前
|
API C# 数据安全/隐私保护
C# 实现网页内容保存为图片并生成压缩包
C# 实现网页内容保存为图片并生成压缩包
|
1月前
|
数据采集 移动开发 前端开发
springboot使用html模版导出pdf文档
springboot使用html模版导出pdf文档
|
1月前
|
机器学习/深度学习 文字识别 数据安全/隐私保护
Python实现从PDF和图片提取文字的方法总结
Python实现从PDF和图片提取文字的方法总结
43 0
|
2月前
|
Java Linux 数据安全/隐私保护
Java【代码 16】将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理
【2月更文挑战第3天】Java 将word、excel文件转换为pdf格式和将pdf文档转换为image格式工具类分享(Gitee源码)aspose转换中文乱码问题处理
96 0