HeaderFooter.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using iTextSharp.text;
  2. using iTextSharp.text.pdf;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using PdfFont = iTextSharp.text.Font;
  7. public class HeaderFooter : PdfPageEventHelper
  8. {
  9. private PdfTemplate totalPages;
  10. private BaseFont baseFont;
  11. private PdfFont headFont;
  12. private PdfFont pdfFont;
  13. private Image headLogo;
  14. public override void OnOpenDocument(PdfWriter writer, Document document)
  15. {
  16. baseFont = BaseFont.CreateFont(Application.streamingAssetsPath + "/Fonts/成绩字体.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  17. headLogo = Image.GetInstance(Application.streamingAssetsPath + "/Logos/成绩logo.png");
  18. headFont = new PdfFont(baseFont, 10, PdfFont.NORMAL, new BaseColor(128, 128, 128));
  19. pdfFont = new PdfFont(baseFont, 12);
  20. totalPages = writer.DirectContent.CreateTemplate(100, 100);
  21. }
  22. public override void OnEndPage(PdfWriter writer, Document document)
  23. {
  24. base.OnEndPage(writer, document);
  25. AddHeader(writer, document);
  26. AddFooter(writer, document);
  27. }
  28. public override void OnCloseDocument(PdfWriter writer, Document document)
  29. {
  30. base.OnCloseDocument(writer, document);
  31. // 在文档关闭时写入总页数
  32. //ColumnText.ShowTextAligned(totalPages, Element.ALIGN_LEFT,
  33. // new Phrase((writer.PageNumber - 1).ToString(), headFont),
  34. // 2, 2, 0);
  35. }
  36. private void AddHeader(PdfWriter writer, Document document)
  37. {
  38. PdfPTable headerTable = new PdfPTable(2);
  39. headerTable.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin;
  40. //设置比例宽度
  41. headerTable.SetWidths(new float[] { 1, 1.5f });
  42. //图片高度
  43. float targetHeight = 30;
  44. //图片单元格
  45. headLogo.ScaleAbsoluteWidth((targetHeight / headLogo.Height) * headLogo.Width);
  46. headLogo.ScaleAbsoluteHeight(targetHeight);
  47. PdfPCell imageCell = new PdfPCell(headLogo);
  48. imageCell.Border = Rectangle.BOTTOM_BORDER;
  49. imageCell.HorizontalAlignment = Element.ALIGN_LEFT;
  50. imageCell.BorderColor = BaseColor.GRAY;
  51. imageCell.BorderWidth = 1f;
  52. imageCell.PaddingBottom = 5f;
  53. //创建一个单元格
  54. PdfPCell cell = new PdfPCell(new Phrase("虚拟仿真实验中心", headFont));
  55. //单元格边界
  56. cell.Border = Rectangle.BOTTOM_BORDER;
  57. cell.BorderColor = BaseColor.GRAY;
  58. cell.BorderWidth = 1f;
  59. cell.PaddingBottom = 5f;
  60. cell.HorizontalAlignment = Element.ALIGN_LEFT;
  61. cell.VerticalAlignment = Element.ALIGN_BOTTOM;
  62. headerTable.AddCell(imageCell);
  63. headerTable.AddCell(cell);
  64. headerTable.WriteSelectedRows(0, -1, document.LeftMargin, document.PageSize.Height - 20, writer.DirectContent);
  65. }
  66. /// <summary>
  67. /// 添加页脚
  68. /// </summary>
  69. /// <param name="writer"></param>
  70. /// <param name="document"></param>
  71. private void AddFooter(PdfWriter writer, Document document)
  72. {
  73. PdfPTable footerTable = new PdfPTable(2);
  74. footerTable.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin;
  75. PdfPCell leftCell = new PdfPCell(new Phrase("虚拟仿真实验考核系统 © 2025", headFont));
  76. leftCell.Border = Rectangle.TOP_BORDER;
  77. leftCell.BorderColor = BaseColor.GRAY;
  78. leftCell.HorizontalAlignment = Element.ALIGN_LEFT;
  79. PdfPCell middleCell = new PdfPCell(new Phrase($"第{writer.PageNumber}页", headFont));
  80. middleCell.Border = Rectangle.TOP_BORDER;
  81. middleCell.BorderColor = BaseColor.GRAY;
  82. middleCell.HorizontalAlignment = Element.ALIGN_RIGHT;
  83. //PdfPCell rightCell = new PdfPCell(new Phrase("总页数", headFont));
  84. //rightCell.Border = Rectangle.TOP_BORDER;
  85. //rightCell.BorderColor = BaseColor.GRAY;
  86. //rightCell.HorizontalAlignment = Element.ALIGN_RIGHT;
  87. footerTable.AddCell(leftCell);
  88. footerTable.AddCell(middleCell);
  89. //footerTable.AddCell(rightCell);
  90. footerTable.WriteSelectedRows(0, -1, document.LeftMargin, document.BottomMargin - 10, writer.DirectContent);
  91. }
  92. }