页眉常用于显示文档的附加信息,我们可以在页眉中插入文本或者图形,例如,页码、日期、公司徽标、文档标题、文件名或作者名等等。那么我们如何以编程的方式添加页眉呢?今天,这篇文章向大家分享如何使用了免费组件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页眉——添加文本、图片到页眉的更多相关文章

  1. C#给PDF文档添加文本和图片页眉

    页眉常用于显示文档的附加信息,我们可以在页眉中插入文本或者图形,例如,页码.日期.公司徽标.文档标题.文件名或作者名等等.那么我们如何以编程的方式添加页眉呢?今天,这篇文章向大家分享如何使用了免费组件 ...

  2. iText + Freemarker实现pdf的导出,支持中文、css以及图片,页眉页脚,页眉添加图片

    本文为了记录自己的实现过程,具体的实现步骤是参考博文 https://www.cnblogs.com/youzhibing/p/7692366.html 来实现的,只是在他的基础上添加了页眉页脚及页眉 ...

  3. java操作pdf添加页眉条码添加水印图片

    添加条码页眉以及图片水印 1. 引入jar包     1. itext-4.2.1.jar     2. itext-asian-5.2.0.jar     3. jbarcode-0.2.8.jar ...

  4. ABBYY FineReader 15 如何为PDF文档添加页眉页脚

    页眉.页脚是文档页面顶部或底部重复出现的文本信息.很多用户会习惯在文档页面的顶部与底部区域添加页眉.页脚来展现页码.文档标题.作者姓名.品牌名称等附加信息.而ABBYY FineReader 15(W ...

  5. C# 给现有PDF文档添加页眉、页脚

    概述 页眉页脚是一篇完整.精致的文档的重要组成部分.在页眉页脚处,可以呈现的内容很多,如公司名称.页码.工作表名.日期.图片,如LOGO.标记等.在之前的文章中介绍了如何通过新建一页空白PDF页来添加 ...

  6. 利用iTextSharp组件给PDF文档添加图片水印,文字水印

    最近在做关于PDF文档添加水印的功能,折腾了好久,终于好了.以下做个记录: 首先会用到iTextSharp组件,大家可以去官网下载,同时我也会在本文中附加进来. 代码中添加引用为:   using S ...

  7. java PDF添加图层,支持多页图层添加

     java PDF添加图层,支持多页图层添加 代码: import java.io.File; import java.io.FileOutputStream; import java.io.IOEx ...

  8. word页眉添加横线与删除横线

    一.删除横线 1.打开已有页眉Word2010文档,并且页眉有横线的,双击页眉 2.选中整个页眉段落,注意:一定要选择段落标记. 3.单击菜单“开始”功能模块. 4.在“段落”中单击边框线下三角按钮. ...

  9. 如何给PDF文档添加和删除贝茨编号

    PDF文件的使用频率高了,我们也不只局限于使用PDF文件了,也会需要编辑PDF文件的时候,那么如何在PDF文件中添加和去除贝茨编号呢,应该有很多小伙伴都想知道吧,今天就来跟大家分享一下吧,小伙伴们就一 ...

随机推荐

  1. javascript 零碎笔记

    使用 live-serve 这个工具,可以热更新 js 代码 逻辑运算符: 常用于单边条件判断,比如 真判断(获取子属性) {error && <div className=&q ...

  2. mvc根据绝对路径下载文件

    首先页面需要一个a标签直接指向下载文件的Action并传值:图片地址,以及图片名称(记住要带后缀名的). 然后是Action里面的代码. SiteHelper.DownloadFile(fileUrl ...

  3. C语言复习2_运算符

    今天复习一下C语言的运算符 1.赋值运算符 单等号 = 顺序是:从右往左 2.复合运算符 #include <stdio.h> #include <stdlib.h> int ...

  4. 为什么 web 开发人员需要迁移到. NET Core, 并使用 ASP.NET Core MVC 构建 web 和 webservice/API

    2018 .NET开发者调查报告: .NET Core 是怎么样的状态,这里我们看到了还有非常多的.net开发人员还在观望,本文给大家一个建议.这仅代表我的个人意见, 我有充分的理由推荐.net 程序 ...

  5. JavaScript中如何检测一个变量是一个String类型?

    typeof x === "string" typeof(x) === "string' // 小写 x.constructor === String // 大写类型 同 ...

  6. #Java学习之路——面试题

    (一)[基础知识梳理——JAVAse部分]Java中的变量和常量        在程序中存在大量的数据来代表程序的状态,其中有些数据在程序的运行过程中值会发生改变,有些数据在程序运行过程中值不能发生改 ...

  7. [Swift]LeetCode8. 字符串转整数 (atoi) | String to Integer (atoi)

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  8. [Swift]LeetCode230. 二叉搜索树中第K小的元素 | Kth Smallest Element in a BST

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  9. [Swift]LeetCode234. 回文链表 | Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  10. [Swift]LeetCode826. 安排工作以达到最大收益 | Most Profit Assigning Work

    We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith ...