HeaderAndFooterEvent.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using iTextSharp.text;
  2. using iTextSharp.text.pdf;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. namespace Common
  9. {
  10. public class HeaderAndFooterEvent : PdfPageEventHelper, IPdfPageEvent
  11. {
  12. #region 静态字段
  13. public static PdfTemplate tpl = null;
  14. public static bool PAGE_NUMBER = false;//为True时就生成 页眉和页脚
  15. public static iTextSharp.text.Rectangle rect = PageSize.A4; //文档大小
  16. /// <summary>
  17. /// 正文字体
  18. /// </summary>
  19. private static Font font;
  20. /// <summary>
  21. /// 页眉页脚字体
  22. /// </summary>
  23. public static string HeaderFooterFontName = "黑体";
  24. /// <summary>
  25. /// 页头页脚字号
  26. /// </summary>
  27. public static int HeaderFooterFontSize = 10;
  28. /// <summary>
  29. /// 页头页尾字体颜色
  30. /// </summary>
  31. public static BaseColor HeaderFooterFontColor = BaseColor.BLACK;
  32. /// <summary>
  33. /// 左边页眉
  34. /// </summary>
  35. public static string HeaderLeft { get; set; }
  36. /// <summary>
  37. /// 右边页眉
  38. /// </summary>
  39. public static string HeaderRight { get; set; }
  40. /// <summary>
  41. /// 左边页脚
  42. /// </summary>
  43. public static string FooterLeft { get; set; }
  44. /// <summary>
  45. /// 右边页脚
  46. /// </summary>
  47. public static string FooterRight { get; set; }
  48. #endregion
  49. #region 设置页面大小
  50. /// <summary>
  51. /// 设置页面大小
  52. /// </summary>
  53. /// <param name="type">页面大小(如"A4")</param>
  54. public static void SetPageSize(string type)
  55. {
  56. switch (type.Trim())
  57. {
  58. case "A4":
  59. rect = PageSize.A4;
  60. break;
  61. case "A8":
  62. rect = PageSize.A8;
  63. break;
  64. }
  65. }
  66. #endregion
  67. #region 设置字体
  68. /// <summary>
  69. /// 设置字体
  70. /// </summary>
  71. /// <param name="size">字体大小</param>
  72. public static void SetFont(BaseColor color, string fontName = "华文中宋", float size = 12, int style = Font.NORMAL)
  73. {
  74. font = new Font(BaseFontAndSize(fontName), size, style, color);
  75. }
  76. #endregion
  77. #region 生成页眉页脚
  78. /// <summary>
  79. /// 关闭一个页面时发生
  80. /// </summary>
  81. public override void OnEndPage(PdfWriter writer, Document document)
  82. {
  83. if (PAGE_NUMBER)
  84. {
  85. Font HeaderFooterFont = FontAndSize(HeaderFooterFontName, HeaderFooterFontSize, Font.NORMAL, HeaderFooterFontColor);
  86. Phrase header_left = new Phrase(HeaderLeft, HeaderFooterFont);
  87. Phrase header_right = new Phrase(HeaderRight, HeaderFooterFont);
  88. Phrase footer_left = new Phrase(FooterLeft, HeaderFooterFont);
  89. Phrase footer_center = new Phrase("第" + (writer.PageNumber) + "页/共 页", HeaderFooterFont);
  90. Phrase footer_right = new Phrase(FooterRight, HeaderFooterFont);
  91. PdfContentByte cb = writer.DirectContent;
  92. //模版 显示总共页数
  93. cb.AddTemplate(tpl, document.Right - 290 + document.LeftMargin, document.Bottom - 15);//调节模版显示的位置
  94. //页眉显示的位置
  95. ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, header_right,
  96. document.Right - 50 + document.LeftMargin, document.Top + 15, 0);
  97. ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, header_left,
  98. document.Right - 500 + document.LeftMargin, document.Top + 15, 0);
  99. //页脚显示的位置
  100. ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, footer_left,
  101. document.Right - 535 + document.LeftMargin, document.Bottom - 15, 0);
  102. ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, footer_center,
  103. document.Right - 300 + document.LeftMargin, document.Bottom - 15, 0);
  104. ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, footer_right,
  105. document.Right - 80 + document.LeftMargin, document.Bottom - 15, 0);
  106. }
  107. }
  108. /// <summary>
  109. /// 打开一个新页面时发生
  110. /// </summary>
  111. public override void OnStartPage(PdfWriter writer, Document document)
  112. {
  113. if (PAGE_NUMBER)
  114. {
  115. writer.PageCount = writer.PageNumber - 1;
  116. }
  117. }
  118. /// <summary>
  119. /// 关闭PDF文档时发生该事件
  120. /// </summary>
  121. public override void OnCloseDocument(PdfWriter writer, Document document)
  122. {
  123. BaseFont bf = BaseFontAndSize(HeaderFooterFontName);
  124. tpl.BeginText();
  125. tpl.SetFontAndSize(bf, HeaderFooterFontSize);
  126. tpl.ShowText((writer.PageNumber - 1).ToString());//总页数
  127. tpl.EndText();
  128. tpl.ClosePath();
  129. }
  130. #endregion
  131. #region 私有方法
  132. private static Font FontAndSize(string font_name, int size, int style, BaseColor baseColor)
  133. {
  134. BaseFont baseFont;
  135. BaseFont.AddToResourceSearch("iTextAsian.dll");
  136. BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
  137. Font font = null;
  138. string file_name = "";
  139. int fontStyle;
  140. switch (font_name)
  141. {
  142. case "黑体":
  143. file_name = "SIMHEI.TTF";
  144. break;
  145. case "华文中宋":
  146. file_name = "STZHONGS.TTF";
  147. break;
  148. case "宋体":
  149. file_name = "SIMYOU.TTF";
  150. break;
  151. default:
  152. file_name = "SIMYOU.TTF";
  153. break;
  154. }
  155. baseFont = BaseFont.CreateFont(@"c:/windows/fonts/" + file_name, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  156. if (style < -1)
  157. {
  158. fontStyle = Font.NORMAL;
  159. }
  160. else
  161. {
  162. fontStyle = style;
  163. }
  164. font = new Font(baseFont, size, fontStyle, baseColor);
  165. return font;
  166. }
  167. private static BaseFont BaseFontAndSize(string font_name)
  168. {
  169. BaseFont baseFont;
  170. BaseFont.AddToResourceSearch("iTextAsian.dll");
  171. BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
  172. string file_name = "";
  173. switch (font_name)
  174. {
  175. case "黑体":
  176. file_name = "SIMHEI.TTF";
  177. break;
  178. case "华文中宋":
  179. file_name = "STZHONGS.TTF";
  180. break;
  181. case "宋体":
  182. file_name = "SIMFANG.TTF";
  183. break;
  184. default:
  185. file_name = "SIMFANG.TTF";
  186. //file_name = "SIMYOU.TTF";
  187. break;
  188. }
  189. baseFont = BaseFont.CreateFont(@"c:/windows/fonts/" + file_name, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  190. return baseFont;
  191. }
  192. #endregion
  193. #region 添加段落
  194. /// <summary>
  195. /// 添加段落
  196. /// </summary>
  197. /// <param name="content">内容</param>
  198. /// <param name="Alignment">对齐方式(1为居中,0为居左,2为居右)</param>
  199. /// <param name="SpacingAfter">段后空行数(0为默认值)</param>
  200. /// <param name="SpacingBefore">段前空行数(0为默认值)</param>
  201. /// <param name="MultipliedLeading">行间距(0为默认值)</param>
  202. public static Paragraph AddParagraph(string content, int Alignment, float SpacingAfter, float SpacingBefore, float MultipliedLeading)
  203. {
  204. Paragraph pra = new Paragraph(content, font);
  205. pra.Alignment = Alignment;
  206. pra.SpacingAfter = SpacingAfter;
  207. pra.SpacingBefore = SpacingBefore;
  208. pra.MultipliedLeading = MultipliedLeading;
  209. return pra;
  210. }
  211. public static Paragraph AddParagraph(string content, int Alignment, float MultipliedLeading)
  212. {
  213. Paragraph pra = new Paragraph(content, font);
  214. pra.Alignment = Alignment;
  215. pra.MultipliedLeading = MultipliedLeading;
  216. return pra;
  217. }
  218. public static void AddPhrase(PdfWriter writer, Document document, string content, float marginLift, float marginBottom)
  219. {
  220. Phrase phrase = new Phrase(content, font);
  221. PdfContentByte cb = writer.DirectContent;
  222. ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, phrase,
  223. marginLift + document.LeftMargin, marginBottom, 0);
  224. }
  225. #endregion
  226. #region 添加图片
  227. /// <summary>
  228. /// 添加图片
  229. /// <param name="Alignment">对齐方式 (0/1/2)</param>
  230. /// <param name="marginRight">页边距</param>
  231. /// <param name="marginBottom">页边距</param>
  232. /// </summary>
  233. public static iTextSharp.text.Image AddImage(string path, int Alignment, float marginRight, float marginBottom)
  234. {
  235. Image img = Image.GetInstance(new Uri(path));
  236. img.Alignment = Alignment;
  237. //等比缩放,宽与高的缩放系数哪个大就取哪一个(比如高的系数是0.8,宽的是0.7,则取0.7。这样图片就不会超出页面范围)
  238. if (img.Width > img.Height)
  239. {
  240. //这里计算图片的缩放系数,因为图片width>height,所以将图片旋转90度以适应页面,计算缩放系数的时候宽与高对调
  241. float PageHeight = PageSize.A4.Height - marginBottom * 3;
  242. double percentHeight = Math.Round((PageHeight / img.Width), 2);
  243. float PageWidth = PageSize.A4.Width - marginRight * 2;
  244. double percentWidth = Math.Round((PageWidth / img.Height), 2);
  245. double percent = percentHeight > percentWidth ? percentWidth : percentHeight;
  246. img.ScalePercent((float)percent * 100);
  247. img.RotationDegrees = 90f;
  248. }
  249. else
  250. {
  251. float PageHeight = PageSize.A4.Height - marginBottom * 3;
  252. double percentHeight = Math.Round((PageHeight / img.Height), 2);
  253. float PageWidth = PageSize.A4.Width - marginRight * 2;
  254. double percentWidth = Math.Round((PageWidth / img.Width), 2);
  255. double percent = percentHeight > percentWidth ? percentWidth : percentHeight;
  256. img.ScalePercent((float)percent * 100);
  257. }
  258. return img;
  259. }
  260. /// <summary>
  261. /// 在PDF生成表格
  262. /// </summary>
  263. public static void CreateTable(DataTable dt, Document document)
  264. {
  265. iTextSharp.text.pdf.PdfPTable datatable = new PdfPTable(5);
  266. float[] headerwidths = { 20, 50, 40, 40, 50};
  267. datatable.SetWidths(headerwidths);
  268. datatable.WidthPercentage = 100;
  269. datatable.HeaderRows = 1;
  270. //datatable.PaddingTop = 5;
  271. string[] array = new string[] { "序号", "项目", "结果", "单位", "参考区间" };
  272. for (int i = 0; i < array.Length; i++)
  273. {
  274. Phrase ph = new Phrase(array[i], font);
  275. iTextSharp.text.pdf.PdfPCell cell1 = new PdfPCell(ph);
  276. cell1.HorizontalAlignment = Element.ALIGN_CENTER;
  277. cell1.BackgroundColor = new iTextSharp.text.BaseColor(0xC0, 0xC0, 0xC0);
  278. datatable.AddCell(cell1);
  279. }
  280. foreach (DataRow R in dt.Rows)
  281. {
  282. datatable.AddCell(new Phrase(R["sequenceNo"].ToString(), font));
  283. datatable.AddCell(new Phrase(R["itemName"].ToString(), font));
  284. datatable.AddCell(new Phrase(R["result"].ToString(), font));
  285. datatable.AddCell(new Phrase(R["unit"].ToString(), font));
  286. datatable.AddCell(new Phrase(R["range"].ToString(), font));
  287. }
  288. document.Add(datatable);
  289. }
  290. #endregion
  291. }
  292. }