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文件中添加和去除贝茨编号呢,应该有很多小伙伴都想知道吧,今天就来跟大家分享一下吧,小伙伴们就一 ...
随机推荐
- 文件访问时间简记(Modify time 和 Change time)
[root@77-29-68-bx-core]# stat hql.out File: 'hql.out' Size: 13750 Blocks: 32 IO Block: 4096 regular ...
- MVC Web.Config 配置错误
配置一个MVC项目的 SessionState,查阅了大量的资料,发现都是直接在web.config中直接加入类似的语句就可以了 <sessionState timeout="60&q ...
- ivew ui
render操作: render:(h, params) => { return h('div', [ h('Button',{ on:{ click:()=>{ this.edit(pa ...
- RevDebug -- VS 调试神器,你值得拥有!
1. What's RevDebug Don't debug - replay! Trace the root cause of bugs in a matter of seconds, save y ...
- 深入理解Spring Redis的使用 (六)、用Spring Aop 实现注解Dao层的自动Spring Redis缓存
摘要: 主要针对Dao层的一些数据库查询的操作,数据实时性不强,直接加入缓存.当缓存中有的时候,就使用缓存中的数据.这样的方法,最终仅仅使用一个注解实现.对于之前的hibernate二级缓存使用,比较 ...
- [Swift]LeetCode34. 在排序数组中查找元素的第一个和最后一个位置 | Find First and Last Position of Element in Sorted Array
Given an array of integers nums sorted in ascending order, find the starting and ending position of ...
- [Swift]LeetCode160. 相交链表 | Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [Swift]LeetCode886. 可能的二分法 | Possible Bipartition
Given a set of N people (numbered 1, 2, ..., N), we would like to split everyone into two groups of ...
- [Swift]LeetCode1001. 网格照明 | Grid Illumination
On a N x N grid of cells, each cell (x, y) with 0 <= x < N and 0 <= y < N has a lamp. In ...
- 9.Git分支-分支的创建与合并-02
在 8.Git分支-分支的创建与合并-01 主要通过一个例子介绍了分支的新建,以及在工作中,git分支的新建切换以及使用,这里主要介绍分支的合并.这里的例子和8.Git分支-分支的创建与合并-01 的 ...