这篇文章主要介绍了C# 添加PDF页眉/页脚的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

概述
页眉页脚是一篇完整、精致的文档的重要组成部分。在页眉页脚处,可以呈现的内容很多,如公司名称、页码、工作表名、日期、图片,如LOGO、标记等。在下面的文章中,将介绍如何在PDF中添加页眉页脚。通过代码测试,添加页眉页脚可以分两种情况来实现效果:

1.通过添加新的一页,在新建的页面上来添加页眉页脚

2.通过给现有文档直接添加页眉页脚

下面将根据这两种情况介绍具体的C#代码操作

使用工具

Free Spire.PDF for .NET 4.3(社区版)

示例代码(供参考)

1.新建一页来添加页眉页脚
1.1 添加页眉

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using System;namespace AddHeader_PDF
{class Program{static void Main(string[] args){//新建一个PdfDocument类对象,并添加一页PdfDocument pdf = new PdfDocument();PdfPageBase page = pdf.Pages.Add();//设置marginPdfUnitConvertor 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;//调用AddHeader()方法添加页眉AddHeader(pdf, PdfPageSize.A4, margin);//保存并打开文档pdf.SaveToFile("PDF页眉.pdf");System.Diagnostics.Process.Start("PDF页眉.pdf");}static void AddHeader(PdfDocument doc, SizeF pageSize, PdfMargins margin){//初始化一个PdfPageTemplateElement对象,用于创建页眉PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top);headerSpace.Foreground = true;doc.Template.Top = headerSpace;//在页眉部分绘入文字PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true);PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);String headerText = "WORLD TRADE ORGANIZATION, WTO \n THE INTERNATIONAL ORGANIZATION THAT REGULATES INTERNATIONAL TRADE";float x = PdfPageSize.A4.Width;float y = 0;headerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Black, x, y, format);//在页眉部分绘入图片PdfImage headerImage = PdfImage.FromFile(@"C:\Users\Administrator\Desktop\1.png");float width = headerImage.Width / 2;float height = headerImage.Height / 3;headerSpace.Graphics.DrawImage(headerImage, 0, 0, width, height);}}
}

页眉添加效果:

1.2添加页脚

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using System;
using Spire.Pdf.AutomaticFields;namespace AddFooter_PDF
{class Program{static void Main(string[] args){//新建一个PdfDocument类对象,添加一页PdfDocument doc = new PdfDocument();PdfPageBase page = doc.Pages.Add();//设置marginPdfUnitConvertor 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;//调用AddFooter()方法添加页脚AddFooter(doc, PdfPageSize.A4, margin);//调用AddPageNumber()方法添加页码AddPageNumber(doc, margin);//保存并打开文档doc.SaveToFile("PDF页脚.pdf");System.Diagnostics.Process.Start("PDF页脚.pdf");}static void AddFooter(PdfDocument doc, SizeF pageSize, PdfMargins margin){//初始化一个PdfPageTemplateElement对象,用于创建页脚PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margin.Bottom);footerSpace.Foreground = true;doc.Template.Bottom = footerSpace;//在页脚部分绘入文字PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true);PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center);String headerText = "Website : www.wto.org";float x = PdfPageSize.A4.Width / 2;float y = 0;footerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Black, x, y, format);}static void AddPageNumber(PdfDocument doc, PdfMargins margin){//添加页码到页脚部分foreach (PdfPageBase page in doc.Pages){PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Left);int x1 = Convert.ToInt32(page.Canvas.ClientSize.Width / 2);int y1 = Convert.ToInt32(page.Canvas.ClientSize.Height - margin.Bottom + 20);Rectangle bounds = new Rectangle(x1, y1, 20, 20);PdfPageNumberField field = new PdfPageNumberField();PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 10f), true);field.Font = font;field.StringFormat = format1;field.Brush = PdfBrushes.Black;field.Bounds = bounds;field.Draw(page.Canvas);}}}
}

页脚添加效果:

2.给现有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);}}}
}

测试效果:

注意事项
安装之后,添加引用Spire.PDF.dll文件到项目程序即可,dll文件可在安装路径下的Bin文件夹中获取。

以上是本次关于C#添加PDF页眉页脚的全部内容,两种情况中的示例方法,可以选择参考使用。

C# 添加PDF页眉/页脚相关推荐

  1. XMLWorkerHelper生成pdf文件添加页眉页脚

    一.Controller代码 import java.io.ByteArrayInputStream; import java.io.FileOutputStream; import java.io. ...

  2. 如何给PDF文件添加页眉页脚,一分钟轻松搞定

    如何给PDF文件添加页眉页脚?很多在使用PDF文件的人都会有这样的问题,不知道如何给PDF文件添加页眉页脚,想要给PDF文件添加页眉页脚需要使用到PDF编辑器,下面就使用迅捷PDF编辑器为大家操作一下 ...

  3. itext给已有pdf添加页眉页脚

    网上百度到的基本都是生成pdf的时候,添加页眉页脚,但是假如对已有的pdf添加页眉页脚就比较麻烦,突然想到了可以曲线救国,用itext进行复制pdf的时候,在添加页眉页脚,最后可以成功的. 生成pdf ...

  4. 编辑PDF的软件哪个好 PDF文件怎么添加页眉页脚

    PDF文件每天都可以看到,上班也是要用到的,PDF添加页眉页脚是经常要做的,编辑PDF的软件哪个好,PDF文件怎么添加页眉页脚?这个很多人都不知道,下面分享下. 1.要在百度打开工具迅捷PDF编辑器, ...

  5. itext总页数_itext 生成pdf文件添加页眉页脚

    原文来自:https://www.cnblogs.com/joann/p/5511905.html 我只是记录所有jar版本,由于版本冲突及不兼容很让人头疼的,一共需要5个jar, 其中itextpd ...

  6. PDF编辑技巧2:设置页眉页脚和添加页码

    一份优秀的PDF文档肯定是需要经过多番的修改和编辑的.编辑PDF的方法各有不同,但是都需要借助第三方PDF编辑软件来实现.在这里给大家推荐一款十分好用的PDF编辑器--迅捷PDF编辑器,绝对能让你快速 ...

  7. 解决如何为扫描件图片PDF添加页眉页脚页码日期的问题

    前言:在日常办公中,会面临这样的情况:需要将几份盖章的扫描件整合到一个文档中,并编制页眉/页脚/页码.普通的word文档,可以直接添加页眉页脚.页码,并通过"另存为..."pdf格 ...

  8. itext对已经存在的pdf添加页眉页脚

    接上一篇拼接pdf后,需要对不同文件的pdf展示不同的页眉及页脚,所以,这篇分享对于已存在的pdf进行页眉页脚的添加. public static String RederAndCopyByPDF(S ...

  9. 为PDF文档添加页眉页脚的方法,快来学学

    PDF文档在职场上越来越流行,驾驭PDF文档的技巧也越拉越多,这其中给PDF文档添加页眉页脚的方法有些小伙伴还不清楚,今天就跟着小编一起来学习吧! 1.准备一款PDF编辑器,比如福昕PDF编辑器: 2 ...

最新文章

  1. 实例讲解决策树分类器
  2. 使用 collections 来创建类似元组对象
  3. 201521123024 《Java程序设计》 第九周学习总结
  4. Linuc C 编程实例1
  5. ML之LGBMRegressor(Competition):2018年全国大学生计算机技能应用大赛《住房月租金预测大数据赛》——设计思路以及核心代码—191017再次更新
  6. MySQL带ANY关键字的子查询
  7. 重设忘记的Mysql密码
  8. 华为云服务器linux切换账号,华为云Windows服务器如何切换为Linux系统?
  9. apache shiro怎么升级_Springboot整合Shiro之授权
  10. XMPP文件传输(XEP-0096协议说明)
  11. idea+springboot+mongodb的简单测试使用分享
  12. orchard文档之-搜索和索引
  13. 惭入佳境之HADOOP的NAMENODE不能正常启动的问题解决
  14. Spark 解析 : DAGScheduler中的DAG划分与提交
  15. LeetCode 28. Implement strStr()
  16. Win10+caffe+CUDA9.1+vs2013+Matlab2018b+GPU环境,跑通faster_rcnn-master
  17. 黑客帝国中比较酷炫的代码雨的实现
  18. 计算机的色彩在哪调整w10,win10电脑显示器颜色不对如何调整
  19. 实现HTML格式电子邮件群发
  20. [推荐]《人一生要读的60本书》

热门文章

  1. lzma java sdk,Java LZMA 磁盘与内存压缩实现
  2. linux lzma 函数 调用,Lzma(7-zip)和zlib
  3. 给中国学生的第七封信:21世纪最需要的7种人才(李开复)
  4. 用三角函数计算两个坐标点距离
  5. 10度角的三角函数计算(续)
  6. Variational Inference入门:variational bayesian EM
  7. 计算机素质教育论文800,关于素质教育的作文800字
  8. 一些蓝桥杯的简单模拟题目
  9. 物联网毕设(基于STM32的蓝牙检测心率+步数+手机APP)
  10. win7计算机不能设置双屏怎么回事,告诉你win7怎么设置双屏显示