//字体Font font = new Font(BaseFont.CreateFont("字体文件路径", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED), 24, Font.BOLD));Document document = new Document(PageSize.A4);                document.SetMargins(55, 55, 38, 70);//页边距设置:左,右,上,下PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("生成的PDF文件全路径和名称", FileMode.Create));SetFooter(writer);//设置页脚(包括页眉)document.Open();               // 初始化头部表格PdfPTable headTable = new PdfPTable(2){HorizontalAlignment = (Element.ALIGN_CENTER),WidthPercentage = (100)};headTable.SetWidths(new int[] { 55, 45 });headTable.SpacingBefore = (20);// 标题名称               string title = "PDF的标题";Chunk formChunk = new Chunk(title, font);PdfPCell formCell = new PdfPCell(new Phrase(formChunk));formCell.HorizontalAlignment = (Element.ALIGN_CENTER);formCell.Border = (0);formCell.Colspan = (2);headTable.AddCell(formCell);// 添加头部表格document.Add(headTable);PdfPTable blxxTab = CreateBlxxPdfTable();document.Add(blxxTab);// 已占用高度float topHeight = headTable.TotalHeight + blxxTab.TotalHeight;int pageCount = writer.PageNumber;document.Close();//主体内容private PdfPTable CreateBlxxPdfTable()        {            PdfPTable blxxTab = new PdfPTable(42);blxxTab.HorizontalAlignment = (Element.ALIGN_CENTER);blxxTab.WidthPercentage = (100);blxxTab.SetWidths(new int[] { 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 14, 15 });blxxTab.SpacingBefore = (20);//时间PdfPCell Lable = PdfPrintUtil.GetTableCell("时间", font, 0, qdTableRowMiniHeight);Lable.BorderWidth = (1);Lable.HorizontalAlignment = (Element.ALIGN_LEFT);Lable.Border = (Rectangle.LEFT_BORDER); ;Lable.DisableBorderSide(Rectangle.LEFT_BORDER);Lable.Colspan = (3);blxxTab.AddCell(Lable);PdfPCell time = PdfPrintUtil.GetTableCell(DateTime.Now.ToString(), font, 0, qdTableRowMiniHeight);time.Colspan = (39);time.Border = (Rectangle.BOTTOM_BORDER);blxxTab.AddCell(time);return blxxTab;}/// <summary>/// 设置页脚/// </summary>/// <param name="writer"></param>private void SetFooter(PdfWriter writer){         PdfHeaderFooter headerFooter = new PdfHeaderFooter();            writer.SetBoxSize("art", PageSize.A4);writer.PageEvent = headerFooter;}using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
public class PdfHeaderFooter : PdfPageEventHelper
{/**
* 页眉
*/public String header = "";/*** 文档字体大小,页脚页眉最好和文本大小一致*/public int presentFontSize = 15;/*** 文档页面大小,最好前面传入,否则默认为A4纸张*/public Rectangle pageSize = PageSize.A4;// 模板public PdfTemplate total;// 基础字体对象public BaseFont bf = null;// 利用基础字体生成的字体对象,一般用于生成中文文字public Font fontDetail = null;/**** Creates a new instance of PdfReportM1HeaderFooter 无参构造方法.**/public PdfReportM1HeaderFooter(){}/**** Creates a new instance of PdfReportM1HeaderFooter 构造方法.** @param yeMei*            页眉字符串* @param presentFontSize*            数据体字体大小* @param pageSize*            页面文档大小,A4,A5,A6横转翻转等Rectangle对象*/public PdfReportM1HeaderFooter(String yeMei, int presentFontSize, Rectangle pageSize){this.header = yeMei;this.presentFontSize = presentFontSize;this.pageSize = pageSize;}public void setHeader(String header){this.header = header;}public void SetPresentFontSize(int presentFontSize){this.presentFontSize = presentFontSize;}/**** TODO 文档打开时创建模板** @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)*/public override void OnOpenDocument(PdfWriter writer, Document document){total = writer.DirectContent.CreateTemplate(50, 50);// 共 页 的矩形的长宽高}/**** TODO 关闭每页的时候,写入** @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)*/public override void OnEndPage(PdfWriter writer, Document document){float y = 20;if (bf == null){bf = new Font(BaseFont.CreateFont("字体文件路径", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED), 24, Font.BOLD));}if (fontDetail == null){fontDetail = new Font(BaseFont.CreateFont("字体文件路径", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED), 24, Font.BOLD));}// 1.写入页眉ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_LEFT, new Phrase(header, fontDetail), document.Left, document.Top + 20, 0);// 2.写入 第 X页/共          int pageS = writer.PageNumber;String foot3 = "第 " + pageS + " 页 共";Phrase footer3 = new Phrase(foot3, fontDetail);// 3.计算前半部分的foot1的长度,后面好定位最后一部分的'Y页'这俩字的x轴坐标,字体长度也要计算进去 = lenfloat len = bf.GetWidthPoint(foot3, presentFontSize);// 4.拿到当前的PdfContentBytePdfContentByte cb = writer.DirectContent;//上下添加横线var line = new iTextSharp.text.pdf.draw.LineSeparator(1F, (document.Right - document.LeftMargin) * -1, Color.BLACK, Element.ALIGN_LEFT, 7F);            if (pageS > 1){line = new iTextSharp.text.pdf.draw.LineSeparator(1F, (document.Right - document.LeftMargin) * -1, Color.BLACK, Element.ALIGN_LEFT, 7.5F);var lineTop = new iTextSharp.text.pdf.draw.LineSeparator(1.5F, (document.Right - document.LeftMargin) * -1, Color.BLACK, Element.ALIGN_LEFT, 0F);Paragraph pTop = new Paragraph(new Chunk(lineTop));ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, pTop, document.LeftMargin, document.Top, 0);//首行横线}           Paragraph p = new Paragraph(new Chunk(line));ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, p, document.LeftMargin, document.BottomMargin, 0);//页脚横线// 5.写入页脚1,x轴就是(右margin+左margin + right() -left()- len)/2.0F 再给偏移20F适合人类视觉感受,否则肉眼看上去就太偏左了 ,y轴就是底边界-20,否则就贴边重叠到数据体里了就不是页脚了;注意Y轴是从下往上累加的,最上方的Top值是大于Bottom好几百开外的。           ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, footer3, document.Right - 165 + document.LeftMargin, y, 0);// 6.写入页脚2的模板(就是页脚的Y页这俩字)添加到文档中,计算模板的和Y轴,X=(右边界-左边界 - 前半部分的len值)/2.0F + len , y 轴和之前的保持一致,底边界-20cb.AddTemplate(total, document.Right - 165 + document.LeftMargin + len, y); // 调节模版显示的位置if (pageS > 0){QrCodePath = Path.Combine(HTKApp.RunFolder, AppPath.TmpDir, $"{HTKApp.RandomSn}.png");MakeQrCode.GetBlBarCodeFilePath(QrCodePath, Bllx, BlxxId, pageS, 150, 150);if (File.Exists(QrCodePath)){try{Image image = Image.GetInstance(QrCodePath);image.SetDpi(1000, 1000);image.ScaleToFit(65, 65);image.SetAbsolutePosition(490f, 25f);cb.AddImage(image);}catch (Exception){}}}}/**** TODO 关闭文档时,替换模板,完成整个页眉页脚组件** @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument(com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)*/public override void OnCloseDocument(PdfWriter writer, Document document){// 7.最后一步了,就是关闭文档的时候,将模板替换成实际的 Y 值,至此,page x of y 制作完毕,完美兼容各种文档size。total.BeginText();total.SetFontAndSize(bf, presentFontSize);// 生成的模版的字体、颜色String foot2 = " " + (writer.PageNumber - 1) + " 页";total.ShowText(foot2);// 模版显示的内容total.EndText();total.ClosePath();}
}

iTextSharp页脚使用,添加横线相关推荐

  1. PDF编辑技巧2:设置页眉页脚和添加页码

    一份优秀的PDF文档肯定是需要经过多番的修改和编辑的.编辑PDF的方法各有不同,但是都需要借助第三方PDF编辑软件来实现.在这里给大家推荐一款十分好用的PDF编辑器--迅捷PDF编辑器,绝对能让你快速 ...

  2. C# Microsoft.Office.Interop.Word设置Word页脚之添加当前页数

    用C#操作Word,电脑上原生的库为Microsoft.Office.Interop.Word,虽然有些慢,但也能用.操作word的另外一个库是Spire.Doc,但是商业的,破解版也不好搞,还是凑合 ...

  3. C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)...

    Microsoft.Office.Interop.Word.Application wordApp = new ApplicationClass() word对象 2. Microsoft.Offic ...

  4. 对java导出PDF进行图片,(图片/文字)水印,页眉页脚的添加

    可直接复制粘贴到测试类中,进行测试,当做随手笔记了,用的时候方便 jar: iText-2.0.8.jar iTextAsian.jar import java.awt.Color; import j ...

  5. word 添加页眉页脚,添加不同页码

    查看,删除 分页符 视图->视图->草稿 设置页脚 是否与上一节相同 不同节,页眉,取消"链接到前一节",可以单独使用各自的页眉 不同节,页脚,使用"链接到前 ...

  6. iText7添加页眉、页脚

    iText7添加页眉.页脚 iText7添加页眉.页脚 PdfHeaderFooterEvent PdfTest iText7添加页眉.页脚 因为iText5对部分pdf操作的支持没有iText7的多 ...

  7. C# 添加Word页眉、页脚和页码

    在Word文档中,我们可以通过添加页眉.页脚的方式来丰富文档内容.添加页眉.页脚时,可以添加时间.日期.文档标题,文档引用信息.页码.内容解释.图片/LOGO等多种图文信息.同时也可根据需要调整文字或 ...

  8. Word开发工具Aspose.Words功能演示:在C ++中以编程方式在Word文档中添加或删除页眉和页脚

    Word文档中的页眉和页脚用于格式化和显示重要信息,例如主题,章节,页码,Copywrite等.以编程方式使用Word文档时,可能需要添加或删除页眉和页脚.为此,本文将教您如何使用C ++在Word文 ...

  9. Java操作poi添加页眉页脚:字体_颜色_大小

    Excel中的页眉或页脚仅在页面布局模式和打印预览模式才可见.我们可以在页眉页脚位置添加页码.工作表名.日期.文字.图片等信息.本文将通过Java代码示例来介绍页眉页脚的添加方法. 使用工具: Fre ...

最新文章

  1. 《强化学习周刊》第11期:强化学习应用之模拟到真实
  2. 在.NET客户端程序中使用多线程
  3. 成功解决RuntimeWarning: invalid value encountered in double_scalars
  4. Linux下创建用于并指定该用户的主目录和相关权限
  5. python加go_[Python异步博客开发] 加入Golang, go~!
  6. IIS的状态代码(微软文档)
  7. IT战略规划项目方法论(德勤、埃森哲、IBM、凯捷)
  8. 网页关注微信服务号登录
  9. recovery模式是什么意思?recovery模式怎么刷机?
  10. Python+KinectV2——PyKinect
  11. ssms mysql_SQL Server Management Studio(SSMS)复制数据库的方法
  12. aop:aspectj-autoproxy的作用
  13. ts无损剪辑合并_软购团荐丨视频无损极速分割、合并、拼接。 处理视频比你想的更简单!...
  14. IGRP和EIGRP为什么是距离矢量协议
  15. Java:外包Java项目有什么好处?
  16. Echarts5.3.2可视化案例-应用篇
  17. opencv建立图片坐标系位置
  18. 算法导论--斐波那契堆
  19. 为什么Move将超越Solidity成为主流编程语言?
  20. PSP编程环境破解个人研究笔记——HelloWorld

热门文章

  1. Duilib 鼠标光标放在Combox上滚动鼠标时,Combox中的值会不断变化
  2. java设计模式之代理模式多种写法(三)
  3. 谁说英语四级难过,汉语四级让老外更难过
  4. UDX710平台 NR针对N78和N79锁频操作
  5. 【多种隐写】LSB+DCT文本图片嵌入+提取【含GUI Matlab源码 1851期】
  6. PSINS初始对准方法
  7. 【SequoiaDB巨杉数据库】特殊类型对象-BinData
  8. 1080显卡主机配置cuda+cudnn+opencv+caffe经历
  9. MPF企业版-标签说明【03】 【QQ群:206378966】
  10. [paper]SPATIALLY TRANSFORMED ADVERSARIAL EXAMPLES