C# 如何添加PDF页眉页脚
2021-03-31 05:25
标签:color blog void addheader 名称 如何 res process shadow 【C#】 页眉添加效果: 【C#】 页脚添加效果: 【C#】 测试效果: C# 如何添加PDF页眉页脚 标签:color blog void addheader 名称 如何 res process shadow 原文地址:http://blog.51cto.com/eiceblue/2136180
下面将根据这两种情况介绍具体的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();
//设置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添加页脚
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文档添加页眉页脚
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
以上是本次关于C#添加PDF页眉页脚的全部内容,两种情况中的示例方法,可以选择参考使用。
本文完。
(如需转载,请注明出处)
下一篇:Rails 的 API