源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-05-01 15:44 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:利用C#如何给PDF文档添加文本与图片页眉
[b]前言[/b] 下面这篇文章向大家分享如何使用了免费组件[url=https://www.e-iceblue.com/Introduce/free-pdf-component.html]Free Spire.PDF[/url]给PDF文档添加文本和图片页眉。这个组件提供了一些方法,可以帮助我们快速方便地实现此目的。 [b]添加页眉步骤:[/b] 首先,创建一个Visual C#控制台项目,添加组件引用并使用以下命名空间。
using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
在下列代码中,我们先定义一个[code]SetDocumentTemplate()[/code]方法来创建一个PDF文档模板,这个模板只包含文本和图片页眉。然后,调用[code]DrawString(string s, PdfFontBase font, PdfBrush brush, float x, float y, PdfStringFormat format)[/code]方法和[code]DrawImage(PdfImage image, float x, float y, float width, float height)[/code]方法,插入自定义的文本和图片页眉。
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文档,主函数内调用[code]SetDocumentTemplate()[/code]方法将带有文本和图片页眉的模板应用到新建的PDF文档中。 [b]具体步骤:[/b] [b]第一步:创建一个PDF文档对象。[/b]
PdfDocument doc = new PdfDocument();
[b]第二步:设置页边距。[/b]
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;
[b]第三步:PDF文档中应用模板。[/b]
SetDocumentTemplate(doc, PdfPageSize.A4, margin);
[b]第四步:PDF文档添加页面。[/b]
PdfPageBase page = doc.Pages.Add();
doc.Pages.Add();
[b]第五步:保存并打开文档。[/b]
doc.SaveToFile("页眉.pdf");
System.Diagnostics.Process.Start("页眉.pdf");
[b]添加页眉后的效果图:[/b] [img]http://files.jb51.net/file_images/article/201701/201715154434200.png?201705154443[/img] [b]全部代码:[/b]
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);
 }
 }
}
[b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用C#能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程素材网的支持。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部