using System; using System.Drawing; using Spire.Pdf; using Spire.Pdf.Graphics;
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);
}
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");
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:\Users\Administrator\Pictures\under_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);
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有