HeaderFooter1.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using iTextSharp.text;
  2. using iTextSharp.text.pdf;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using System.Drawing;
  7. using PdfFont = iTextSharp.text.Font;
  8. using Image = iTextSharp.text.Image;
  9. public class HeaderFooter1 : PdfPageEventHelper
  10. {
  11. private PdfTemplate total;
  12. private BaseFont baseFont;
  13. private PdfFont headFont;
  14. private Image headLogo;
  15. public override void OnOpenDocument(PdfWriter writer, Document document)
  16. {
  17. baseFont = BaseFont.CreateFont(Application.streamingAssetsPath + "/Fonts/成绩字体.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  18. headLogo = Image.GetInstance(Application.streamingAssetsPath + "/Logos/成绩logo.png");
  19. headFont = new PdfFont(baseFont, 10, PdfFont.NORMAL, new BaseColor(128, 128, 128));
  20. total = writer.DirectContent.CreateTemplate(100, 100);
  21. }
  22. public override void OnEndPage(PdfWriter writer, Document document)
  23. {
  24. //添加页眉
  25. AddHeader(writer,document);
  26. //添加页脚
  27. AddFooter(writer, document);
  28. }
  29. public override void OnCloseDocument(PdfWriter writer, Document document)
  30. {
  31. // 在文档关闭时设置总页数
  32. total.BeginText();
  33. total.SetFontAndSize(baseFont, 10);
  34. total.SetTextMatrix(0, 0);
  35. total.ShowText((writer.PageNumber).ToString() + " 页");
  36. total.EndText();
  37. }
  38. private void AddHeader(PdfWriter writer, Document document)
  39. {
  40. PdfContentByte cb = writer.DirectContent;
  41. cb.SaveState();
  42. cb.SetColorFill(new BaseColor(128, 128, 128));
  43. string text1 = "虚拟仿真实验中心";
  44. float textSize1 = baseFont.GetWidthPoint(text1, 10);
  45. float x1 = document.PageSize.Width / 2;
  46. float y1 = document.Top + 20;
  47. // 绘制页码文本
  48. cb.BeginText();
  49. cb.SetFontAndSize(baseFont, 10);
  50. cb.SetTextMatrix(x1 - textSize1/2, y1);
  51. cb.ShowText(text1);
  52. cb.EndText();
  53. //页眉分割线
  54. cb.SetColorStroke(BaseColor.GRAY);
  55. cb.SetLineWidth(0.5f);
  56. float headY = document.Top + 15;
  57. cb.MoveTo(document.Left, headY);
  58. cb.LineTo(document.Right, headY);
  59. cb.Stroke();
  60. cb.RestoreState();
  61. float targetHeight = 30f;
  62. headLogo.ScaleAbsoluteWidth((targetHeight / headLogo.Height) * headLogo.Width);
  63. headLogo.ScaleAbsoluteHeight(targetHeight);
  64. headLogo.SetAbsolutePosition(document.LeftMargin,
  65. document.Top + 15);
  66. writer.DirectContent.AddImage(headLogo);
  67. }
  68. /// <summary>
  69. /// 添加页脚
  70. /// </summary>
  71. /// <param name="writer"></param>
  72. /// <param name="document"></param>
  73. private void AddFooter(PdfWriter writer, Document document)
  74. {
  75. PdfContentByte cb = writer.DirectContent;
  76. cb.SaveState();
  77. cb.SetColorFill(new BaseColor(128, 128, 128));
  78. //页脚分隔线
  79. float footY = document.Bottom;
  80. cb.MoveTo(document.BottomMargin, footY);
  81. cb.LineTo(document.PageSize.Width - document.RightMargin, footY);
  82. cb.Stroke();
  83. string text1 = $"虚拟仿真实验考核系统 @2025";
  84. float textSize1 = baseFont.GetWidthPoint(text1, 10);
  85. float x1 = document.LeftMargin;
  86. float y1 = document.BottomMargin - 15;
  87. // 绘制页码文本
  88. cb.BeginText();
  89. cb.SetFontAndSize(baseFont, 10);
  90. cb.SetTextMatrix(x1, y1);
  91. cb.ShowText(text1);
  92. cb.EndText();
  93. string text2 = $"第 {writer.PageNumber} 页 / 共 ";
  94. float textSize2 = baseFont.GetWidthPoint(text2, 10);
  95. float x2 = document.PageSize.Width / 2;
  96. float y2 = document.BottomMargin - 15;
  97. // 绘制页码文本
  98. cb.BeginText();
  99. cb.SetFontAndSize(baseFont, 10);
  100. cb.SetTextMatrix(x2 - textSize2 / 2, y2);
  101. cb.ShowText(text2);
  102. cb.EndText();
  103. // 添加总页数占位符
  104. cb.AddTemplate(total, x2 + textSize2 / 2 - baseFont.GetWidthPoint(" ", 10), y2);
  105. cb.RestoreState();
  106. }
  107. }