添加页眉后的效果图:

全部代码:
using System;
using Spire.Pdf;
using System.Drawing;
using Spire.Pdf.Graphics;
namespace PDF添加页眉
{
class Program
{
static void Main(string[] args)
{
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;
SetDocumentTemplate(doc, PdfPageSize.A4, margin);
PdfPageBase page = doc.Pages.Add();
doc.Pages.Add();
doc.SaveToFile("页眉.pdf");
System.Diagnostics.Process.Start("页眉.pdf");
}
static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
{
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(@"C:UsersAdministratorPicturesunder_construction.jpg");
float width = headerImage.Width;
float height = headerImage.Height;
PointF pageLeftTop = new PointF(0, 0);
topSpace.Graphics.DrawImage(headerImage, 0, 0, width / 2, height / 2);
}
}
}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用C#能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对ASPKU的支持。
注:相关教程知识阅读请移步到c#教程频道。










