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

 更新时间:2017年01月05日 15:46:27   作者:Yesi  
页眉常用于显示文档的附加信息,我们可以在页眉中插入文本或者图形,例如,页码、日期、公司徽标、文档标题、文件名或作者名等等。那么我们如何以编程的方式添加页眉呢?这篇文章主要介绍了利用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:\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);
 }
 }
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用C#能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

相关文章

  • Unity的IPreprocessBuildWithReport实用案例深入解析

    Unity的IPreprocessBuildWithReport实用案例深入解析

    这篇文章主要为大家介绍了Unity的IPreprocessBuildWithReport实用案例深入解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • C#调用微信接口的相关代码

    C#调用微信接口的相关代码

    这篇文章主要为大家详细介绍了C#调用微信接口的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • C#中如何执行存储过程方法

    C#中如何执行存储过程方法

    C#中如何执行存储过程方法,需要的朋友可以参考一下
    2013-04-04
  • C#对文件名智能排序的算法

    C#对文件名智能排序的算法

    这篇文章介绍了C#对文件名智能排序的算法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • C# 中的多态底层虚方法调用详情

    C# 中的多态底层虚方法调用详情

    这篇文章主要介绍了C# 中的多态底层虚方法调用详情,文章围绕主题展开详细的内容介绍,需要的小伙伴你可以参考一下
    2022-06-06
  • 解析错误富文本json字符串(带双引号)的快速解决方法

    解析错误富文本json字符串(带双引号)的快速解决方法

    下面小编就为大家带来一篇解析错误富文本json字符串(带双引号)的快速解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-08-08
  • Winform让DataGridView左侧显示图片

    Winform让DataGridView左侧显示图片

    本文主要介绍在如何让DataGridView左侧显示图片,这里主要讲解重写DataGridView的OnRowPostPaint方法,需要的朋友可以参考下。
    2016-05-05
  • C#实现TCP和UDP通信的示例详解

    C#实现TCP和UDP通信的示例详解

    这篇文章主要为大家详细介绍了C#实现TCP和UDP通信的相关知识,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以了解一下
    2023-03-03
  • C#类的成员之Field字段的使用

    C#类的成员之Field字段的使用

    本文主要介绍了C#类的成员之Field字段的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • WPF仿LiveCharts实现饼图的绘制

    WPF仿LiveCharts实现饼图的绘制

    这篇文章主要介绍了如何利用WPF仿LiveCharts实现饼图的绘制,文中的示例代码讲解详细,对我们学习或工作有一定帮助,需要的可以参考一下
    2022-07-07

最新评论