C#在PDF文档中如何插入页眉页脚
发表于:2024-10-25 作者:千家信息网编辑
千家信息网最后更新 2024年10月25日,小编给大家分享一下C#在PDF文档中如何插入页眉页脚,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!1.新建一页来添加页眉页
千家信息网最后更新 2024年10月25日C#在PDF文档中如何插入页眉页脚
小编给大家分享一下C#在PDF文档中如何插入页眉页脚,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
1.新建一页来添加页眉页脚
1.1 添加页眉
【C#】
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(); //设置margin 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; //调用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添加页脚
【C#】
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(); //设置margin 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; //调用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文档添加页眉页脚
【C#】
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); } } }}
测试效果:
以上是"C#在PDF文档中如何插入页眉页脚"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
页眉
文档
C#
位置
方法
对象
页码
页面
效果
文字
篇文章
两个
内容
变量
图片
大小
横线
空白
脚部
部位
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库程序编辑软件
用什么软件开发设备管理系统
泰拉瑞亚服务器开启指定地图
乐窝互联网科技有限公司
世界上的网络安全问题
动态数据库连接
上海企业软件开发大概多少钱
软件开发psp
html 数据库操作系统
微信复审违反网络安全法
软件开发工程师的理解
国产拉曼数据库多少
数据库中同时查两个id的值
sql语句新建数据库和表
富盟网络技术有限公司官网
养殖管理软件开发
收银机不能连接到数据库
网络技术的经营范围
数据库 和文件系统
关于网络安全发生的事
asp.net连接数据库的功能
盛传网络安全教育
莆田水利局网络安全建设
进入网络安全模式鼠标还是不能用
03智能应用软件开发
怎么提升服务器租用安全性
软件开发怎么接私活
数据库检索查询
一个服务器支持多少人
数据库实验七肇庆学院