| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Drawing;
- using PdfFont = iTextSharp.text.Font;
- using Image = iTextSharp.text.Image;
- public class HeaderFooter1 : PdfPageEventHelper
- {
- private PdfTemplate totalPdfTemplate;
- private BaseFont baseFont;
- 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");
- totalPdfTemplate = writer.DirectContent.CreateTemplate(100, 100);
- }
- public override void OnEndPage(PdfWriter writer, Document document)
- {
- //添加页眉
- AddHeader(writer,document);
- //添加页脚
- AddFooter(writer, document);
- }
- public override void OnCloseDocument(PdfWriter writer, Document document)
- {
- // 在文档关闭时设置总页数
- totalPdfTemplate.BeginText();
- totalPdfTemplate.SetFontAndSize(baseFont, 10);
- totalPdfTemplate.SetTextMatrix(0, 0);
- totalPdfTemplate.ShowText((writer.PageNumber).ToString() + " 页");
- totalPdfTemplate.EndText();
- }
- private void AddHeader(PdfWriter writer, Document document)
- {
- PdfContentByte cb = writer.DirectContent;
- cb.SaveState();
- cb.SetColorFill(new BaseColor(128, 128, 128));
- string text1 = "虚拟仿真实验中心";
- float textSize1 = baseFont.GetWidthPoint(text1, 10);
- float x1 = document.PageSize.Width / 2;
- float y1 = document.Top + 20;
- // 绘制页码文本
- cb.BeginText();
- cb.SetFontAndSize(baseFont, 10);
- cb.SetTextMatrix(x1 - textSize1/2, y1);
- cb.ShowText(text1);
- cb.EndText();
- //页眉分割线
- cb.SetColorStroke(BaseColor.GRAY);
- cb.SetLineWidth(0.5f);
- float headY = document.Top + 15;
- cb.MoveTo(document.Left, headY);
- cb.LineTo(document.Right, headY);
- cb.Stroke();
- cb.RestoreState();
- float targetHeight = 30f;
- headLogo.ScaleAbsoluteWidth((targetHeight / headLogo.Height) * headLogo.Width);
- headLogo.ScaleAbsoluteHeight(targetHeight);
- headLogo.SetAbsolutePosition(document.LeftMargin,
- document.Top + 15);
- writer.DirectContent.AddImage(headLogo);
- }
- /// <summary>
- /// 添加页脚
- /// </summary>
- /// <param name="writer"></param>
- /// <param name="document"></param>
- private void AddFooter(PdfWriter writer, Document document)
- {
- PdfContentByte cb = writer.DirectContent;
- cb.SaveState();
- cb.SetColorFill(new BaseColor(128, 128, 128));
-
- //页脚分隔线
- cb.MoveTo(document.BottomMargin, document.Bottom);
- cb.LineTo(document.PageSize.Width - document.RightMargin, document.Bottom);
- cb.Stroke();
- string text1 = $"虚拟仿真实验考核系统 @2025";
- float x1 = document.LeftMargin;
- float y1 = document.BottomMargin - 15;
- // 绘制页码文本
- cb.BeginText();
- cb.SetFontAndSize(baseFont, 10);
- cb.SetTextMatrix(x1, y1);
- cb.ShowText(text1);
- cb.EndText();
- string text2 = $"第 {writer.PageNumber} 页 / 共 ";
- float textSize2 = baseFont.GetWidthPoint(text2, 10);
- float x2 = document.PageSize.Width / 2;
- float y2 = document.BottomMargin - 15;
- // 绘制页码文本
- cb.BeginText();
- cb.SetFontAndSize(baseFont, 10);
- cb.SetTextMatrix(x2 - textSize2 / 2, y2);
- cb.ShowText(text2);
- cb.EndText();
- // 添加总页数占位符
- cb.AddTemplate(totalPdfTemplate, x2 + textSize2 / 2 - baseFont.GetWidthPoint(" ", 10), y2);
- cb.RestoreState();
- }
- }
|