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 = ;
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( , );
topSpace.Graphics.DrawImage(headerImage,,,width/,height/);
}
接下来再创建一个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 = ;
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(, );
topSpace.Graphics.DrawImage(headerImage, , , width / , height / );
}
}
}
谢谢浏览!
C#添加PDF页眉——添加文本、图片到页眉的更多相关文章
- C#给PDF文档添加文本和图片页眉
页眉常用于显示文档的附加信息,我们可以在页眉中插入文本或者图形,例如,页码.日期.公司徽标.文档标题.文件名或作者名等等.那么我们如何以编程的方式添加页眉呢?今天,这篇文章向大家分享如何使用了免费组件 ...
- iText + Freemarker实现pdf的导出,支持中文、css以及图片,页眉页脚,页眉添加图片
本文为了记录自己的实现过程,具体的实现步骤是参考博文 https://www.cnblogs.com/youzhibing/p/7692366.html 来实现的,只是在他的基础上添加了页眉页脚及页眉 ...
- java操作pdf添加页眉条码添加水印图片
添加条码页眉以及图片水印 1. 引入jar包 1. itext-4.2.1.jar 2. itext-asian-5.2.0.jar 3. jbarcode-0.2.8.jar ...
- ABBYY FineReader 15 如何为PDF文档添加页眉页脚
页眉.页脚是文档页面顶部或底部重复出现的文本信息.很多用户会习惯在文档页面的顶部与底部区域添加页眉.页脚来展现页码.文档标题.作者姓名.品牌名称等附加信息.而ABBYY FineReader 15(W ...
- C# 给现有PDF文档添加页眉、页脚
概述 页眉页脚是一篇完整.精致的文档的重要组成部分.在页眉页脚处,可以呈现的内容很多,如公司名称.页码.工作表名.日期.图片,如LOGO.标记等.在之前的文章中介绍了如何通过新建一页空白PDF页来添加 ...
- 利用iTextSharp组件给PDF文档添加图片水印,文字水印
最近在做关于PDF文档添加水印的功能,折腾了好久,终于好了.以下做个记录: 首先会用到iTextSharp组件,大家可以去官网下载,同时我也会在本文中附加进来. 代码中添加引用为: using S ...
- java PDF添加图层,支持多页图层添加
java PDF添加图层,支持多页图层添加 代码: import java.io.File; import java.io.FileOutputStream; import java.io.IOEx ...
- word页眉添加横线与删除横线
一.删除横线 1.打开已有页眉Word2010文档,并且页眉有横线的,双击页眉 2.选中整个页眉段落,注意:一定要选择段落标记. 3.单击菜单“开始”功能模块. 4.在“段落”中单击边框线下三角按钮. ...
- 如何给PDF文档添加和删除贝茨编号
PDF文件的使用频率高了,我们也不只局限于使用PDF文件了,也会需要编辑PDF文件的时候,那么如何在PDF文件中添加和去除贝茨编号呢,应该有很多小伙伴都想知道吧,今天就来跟大家分享一下吧,小伙伴们就一 ...
随机推荐
- js jq 字符串数组对象
数组是有序的,对象是无序,数组是特殊的对象 数组 声明数组 var arr=new Array('red','blue','yellow'); //["red", "bl ...
- 一、开水白菜(steamed Chinese cabbage in supreme soup)
菜品历史 相传,开水白菜是由颇受慈禧赏识的川菜名厨黄敬临在清宫御膳房创制的. 黄敬临当厨时,不少人贬损川菜"只会麻辣,粗俗土气",为了破谣立证,他冥思苦想多时并经由百番尝试,终于开 ...
- Android Studio 设置不同分辨率的图标Icon
右键你的项目 -->"NEW"-->"Image Asset" 'Asset Type' 勾选”Image“才可以选择”Path“,其他选项可以自己 ...
- mysql查看编码格式以及修改编码格式
1.进入mysql,输入show variables like 'character%';查看当前字符集编码情况,显示如下: 其中,character_set_client为客户端编码方式: char ...
- 在ASP.NET Core中使用brotli压缩
Brotli是一种全新的数据格式,可以提供比Zopfli高20-26%的压缩比.据谷歌研究,Brotli压缩速度同zlib的Deflate实现大致相同,而在Canterbury语料库上的压缩密度比LZ ...
- Java中的队列同步器AQS
一.AQS概念 1.队列同步器是用来构建锁或者其他同步组件的基础框架,使用一个int型变量代表同步状态,通过内置的队列来完成线程的排队工作. 2.下面是JDK8文档中对于AQS的部分介绍 public ...
- Java中,多态的实现有哪些要求?实现多态的关键技术?
多态指的是允许不同类的对象对同一消息做出响应,即同一消息可以根据发送对象的不同而采用多种不同的行为方式(发送消息就是函数调用).实现多态的方法是动态绑定( Dynamic Binding),动态绑定 ...
- Python网络爬虫与如何爬取段子的项目实例
一.网络爬虫 Python爬虫开发工程师,从网站某一个页面(通常是首页)开始,读取网页的内容,找到在网页中的其它链接地址,然后通过这些链接地址寻找下一个网页,这样一直循环下去,直到把这个网站所有的网页 ...
- 音视频编解码技术(一):MPEG-4/H.264 AVC 编解码标准
一.H264 概述 H.264,通常也被称之为H.264/AVC(或者H.264/MPEG-4 AVC或MPEG-4/H.264 AVC) 1. H.264视频编解码的意义 H.264的出现就是为了创 ...
- [Swift]LeetCode220. 存在重复元素 III | Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...