using iTextSharp.text; using iTextSharp.text.pdf; using System.Collections; using System.Collections.Generic; using UnityEngine; using PdfFont = iTextSharp.text.Font; public class HeaderFooter : PdfPageEventHelper { private PdfTemplate totalPages; private BaseFont baseFont; private PdfFont headFont; private PdfFont pdfFont; private Image headLogo; public override void OnOpenDocument(PdfWriter writer, Document document) { baseFont = BaseFont.CreateFont(Application.streamingAssetsPath + "/Fonts/成绩字体.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); headLogo = Image.GetInstance(Application.streamingAssetsPath + "/Logos/成绩logo.png"); headFont = new PdfFont(baseFont, 10, PdfFont.NORMAL, new BaseColor(128, 128, 128)); pdfFont = new PdfFont(baseFont, 12); totalPages = writer.DirectContent.CreateTemplate(100, 100); } public override void OnEndPage(PdfWriter writer, Document document) { base.OnEndPage(writer, document); AddHeader(writer, document); AddFooter(writer, document); } public override void OnCloseDocument(PdfWriter writer, Document document) { base.OnCloseDocument(writer, document); // 在文档关闭时写入总页数 //ColumnText.ShowTextAligned(totalPages, Element.ALIGN_LEFT, // new Phrase((writer.PageNumber - 1).ToString(), headFont), // 2, 2, 0); } private void AddHeader(PdfWriter writer, Document document) { PdfPTable headerTable = new PdfPTable(2); headerTable.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin; //设置比例宽度 headerTable.SetWidths(new float[] { 1, 1.5f }); //图片高度 float targetHeight = 30; //图片单元格 headLogo.ScaleAbsoluteWidth((targetHeight / headLogo.Height) * headLogo.Width); headLogo.ScaleAbsoluteHeight(targetHeight); PdfPCell imageCell = new PdfPCell(headLogo); imageCell.Border = Rectangle.BOTTOM_BORDER; imageCell.HorizontalAlignment = Element.ALIGN_LEFT; imageCell.BorderColor = BaseColor.GRAY; imageCell.BorderWidth = 1f; imageCell.PaddingBottom = 5f; //创建一个单元格 PdfPCell cell = new PdfPCell(new Phrase("虚拟仿真实验中心", headFont)); //单元格边界 cell.Border = Rectangle.BOTTOM_BORDER; cell.BorderColor = BaseColor.GRAY; cell.BorderWidth = 1f; cell.PaddingBottom = 5f; cell.HorizontalAlignment = Element.ALIGN_LEFT; cell.VerticalAlignment = Element.ALIGN_BOTTOM; headerTable.AddCell(imageCell); headerTable.AddCell(cell); headerTable.WriteSelectedRows(0, -1, document.LeftMargin, document.PageSize.Height - 20, writer.DirectContent); } /// /// 添加页脚 /// /// /// private void AddFooter(PdfWriter writer, Document document) { PdfPTable footerTable = new PdfPTable(2); footerTable.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin; PdfPCell leftCell = new PdfPCell(new Phrase("虚拟仿真实验考核系统 © 2025", headFont)); leftCell.Border = Rectangle.TOP_BORDER; leftCell.BorderColor = BaseColor.GRAY; leftCell.HorizontalAlignment = Element.ALIGN_LEFT; PdfPCell middleCell = new PdfPCell(new Phrase($"第{writer.PageNumber}页", headFont)); middleCell.Border = Rectangle.TOP_BORDER; middleCell.BorderColor = BaseColor.GRAY; middleCell.HorizontalAlignment = Element.ALIGN_RIGHT; //PdfPCell rightCell = new PdfPCell(new Phrase("总页数", headFont)); //rightCell.Border = Rectangle.TOP_BORDER; //rightCell.BorderColor = BaseColor.GRAY; //rightCell.HorizontalAlignment = Element.ALIGN_RIGHT; footerTable.AddCell(leftCell); footerTable.AddCell(middleCell); //footerTable.AddCell(rightCell); footerTable.WriteSelectedRows(0, -1, document.LeftMargin, document.BottomMargin - 10, writer.DirectContent); } }