栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

利用C#如何给PDF文档添加文本与图片页眉

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

利用C#如何给PDF文档添加文本与图片页眉

前言

下面这篇文章向大家分享如何使用了免费组件Free Spire.PDF给PDF文档添加文本和图片页眉。这个组件提供了一些方法,可以帮助我们快速方便地实现此目的。

添加页眉步骤:

首先,创建一个Visual C#控制台项目,添加组件引用并使用以下命名空间。

using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;

在下列代码中,我们先定义一个SetdocumentTemplate()方法来创建一个PDF文档模板,这个模板只包含文本和图片页眉。然后,调用DrawString(string s, PdfFontbase font, PdfBrush brush, float x, float y, PdfStringFormat format)方法和DrawImage(PdfImage image, float x, float y, float width, float height)方法,插入自定义的文本和图片页眉。

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); 
}

接下来再创建一个PDF文档,主函数内调用SetdocumentTemplate()方法将带有文本和图片页眉的模板应用到新建的PDF文档中。

具体步骤:

第一步:创建一个PDF文档对象。

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;

第三步:PDF文档中应用模板。

SetdocumentTemplate(doc, PdfPageSize.A4, margin);

第四步:PDF文档添加页面。

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: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#能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对考高分网的支持。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/123800.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号