//字体
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;
}
///
/// 设置页脚
///
///
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轴坐标,字体长度也要计算进去 = len
float len = bf.GetWidthPoint(foot3, presentFontSize);
// 4.拿到当前的PdfContentByte
PdfContentByte 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 轴和之前的保持一致,底边界-20
cb.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();
}
}