C#中PDF文档操作类

清疚 2021-06-26 16:06 494阅读 0赞

PDFOperation.cs

  1. using System.IO;
  2. using iTextSharp.text;
  3. using iTextSharp.text.pdf;
  4. namespace DotNet.Utilities
  5. {
  6. /// <summary>
  7. /// PDF文档操作类
  8. /// </summary>
  9. //------------------------------------调用--------------------------------------------
  10. //PDFOperation pdf = new PDFOperation();
  11. //pdf.Open(new FileStream(path, FileMode.Create));
  12. //pdf.SetBaseFont(@"C:\Windows\Fonts\SIMHEI.TTF");
  13. //pdf.AddParagraph("测试文档(生成时间:" + DateTime.Now + ")", 15, 1, 20, 0, 0);
  14. //pdf.Close();
  15. //-------------------------------------------------------------------------------------
  16. public class PDFOperation
  17. {
  18. #region 构造函数
  19. /// <summary>
  20. /// 构造函数
  21. /// </summary>
  22. public PDFOperation()
  23. {
  24. rect = PageSize.A4;
  25. document = new Document(rect);
  26. }
  27. /// <summary>
  28. /// 构造函数
  29. /// </summary>
  30. /// <param name="type">页面大小(如"A4")</param>
  31. public PDFOperation(string type)
  32. {
  33. SetPageSize(type);
  34. document = new Document(rect);
  35. }
  36. /// <summary>
  37. /// 构造函数
  38. /// </summary>
  39. /// <param name="type">页面大小(如"A4")</param>
  40. /// <param name="marginLeft">内容距左边框距离</param>
  41. /// <param name="marginRight">内容距右边框距离</param>
  42. /// <param name="marginTop">内容距上边框距离</param>
  43. /// <param name="marginBottom">内容距下边框距离</param>
  44. public PDFOperation(string type, float marginLeft, float marginRight, float marginTop, float marginBottom)
  45. {
  46. SetPageSize(type);
  47. document = new Document(rect, marginLeft, marginRight, marginTop, marginBottom);
  48. }
  49. #endregion
  50. #region 私有字段
  51. private Font font;
  52. private Rectangle rect; //文档大小
  53. private Document document;//文档对象
  54. private BaseFont basefont;//字体
  55. #endregion
  56. #region 设置字体
  57. /// <summary>
  58. /// 设置字体
  59. /// </summary>
  60. public void SetBaseFont(string path)
  61. {
  62. basefont = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  63. }
  64. /// <summary>
  65. /// 设置字体
  66. /// </summary>
  67. /// <param name="size">字体大小</param>
  68. public void SetFont(float size)
  69. {
  70. font = new Font(basefont, size);
  71. }
  72. #endregion
  73. #region 设置页面大小
  74. /// <summary>
  75. /// 设置页面大小
  76. /// </summary>
  77. /// <param name="type">页面大小(如"A4")</param>
  78. public void SetPageSize(string type)
  79. {
  80. switch (type.Trim())
  81. {
  82. case "A4":
  83. rect = PageSize.A4;
  84. break;
  85. case "A8":
  86. rect = PageSize.A8;
  87. break;
  88. }
  89. }
  90. #endregion
  91. #region 实例化文档
  92. /// <summary>
  93. /// 实例化文档
  94. /// </summary>
  95. /// <param name="os">文档相关信息(如路径,打开方式等)</param>
  96. public void GetInstance(Stream os)
  97. {
  98. PdfWriter.GetInstance(document, os);
  99. }
  100. #endregion
  101. #region 打开文档对象
  102. /// <summary>
  103. /// 打开文档对象
  104. /// </summary>
  105. /// <param name="os">文档相关信息(如路径,打开方式等)</param>
  106. public void Open(Stream os)
  107. {
  108. GetInstance(os);
  109. document.Open();
  110. }
  111. #endregion
  112. #region 关闭打开的文档
  113. /// <summary>
  114. /// 关闭打开的文档
  115. /// </summary>
  116. public void Close()
  117. {
  118. document.Close();
  119. }
  120. #endregion
  121. #region 添加段落
  122. /// <summary>
  123. /// 添加段落
  124. /// </summary>
  125. /// <param name="content">内容</param>
  126. /// <param name="fontsize">字体大小</param>
  127. public void AddParagraph(string content, float fontsize)
  128. {
  129. SetFont(fontsize);
  130. Paragraph pra = new Paragraph(content, font);
  131. document.Add(pra);
  132. }
  133. /// <summary>
  134. /// 添加段落
  135. /// </summary>
  136. /// <param name="content">内容</param>
  137. /// <param name="fontsize">字体大小</param>
  138. /// <param name="Alignment">对齐方式(1为居中,0为居左,2为居右)</param>
  139. /// <param name="SpacingAfter">段后空行数(0为默认值)</param>
  140. /// <param name="SpacingBefore">段前空行数(0为默认值)</param>
  141. /// <param name="MultipliedLeading">行间距(0为默认值)</param>
  142. public void AddParagraph(string content, float fontsize, int Alignment, float SpacingAfter, float SpacingBefore, float MultipliedLeading)
  143. {
  144. SetFont(fontsize);
  145. Paragraph pra = new Paragraph(content, font);
  146. pra.Alignment = Alignment;
  147. if (SpacingAfter != 0)
  148. {
  149. pra.SpacingAfter = SpacingAfter;
  150. }
  151. if (SpacingBefore != 0)
  152. {
  153. pra.SpacingBefore = SpacingBefore;
  154. }
  155. if (MultipliedLeading != 0)
  156. {
  157. pra.MultipliedLeading = MultipliedLeading;
  158. }
  159. document.Add(pra);
  160. }
  161. #endregion
  162. #region 添加图片
  163. /// <summary>
  164. /// 添加图片
  165. /// </summary>
  166. /// <param name="path">图片路径</param>
  167. /// <param name="Alignment">对齐方式(1为居中,0为居左,2为居右)</param>
  168. /// <param name="newWidth">图片宽(0为默认值,如果宽度大于页宽将按比率缩放)</param>
  169. /// <param name="newHeight">图片高</param>
  170. public void AddImage(string path, int Alignment, float newWidth, float newHeight)
  171. {
  172. Image img = Image.GetInstance(path);
  173. img.Alignment = Alignment;
  174. if (newWidth != 0)
  175. {
  176. img.ScaleAbsolute(newWidth, newHeight);
  177. }
  178. else
  179. {
  180. if (img.Width > PageSize.A4.Width)
  181. {
  182. img.ScaleAbsolute(rect.Width, img.Width * img.Height / rect.Height);
  183. }
  184. }
  185. document.Add(img);
  186. }
  187. #endregion
  188. #region 添加链接、点
  189. /// <summary>
  190. /// 添加链接
  191. /// </summary>
  192. /// <param name="Content">链接文字</param>
  193. /// <param name="FontSize">字体大小</param>
  194. /// <param name="Reference">链接地址</param>
  195. public void AddAnchorReference(string Content, float FontSize, string Reference)
  196. {
  197. SetFont(FontSize);
  198. Anchor auc = new Anchor(Content, font);
  199. auc.Reference = Reference;
  200. document.Add(auc);
  201. }
  202. /// <summary>
  203. /// 添加链接点
  204. /// </summary>
  205. /// <param name="Content">链接文字</param>
  206. /// <param name="FontSize">字体大小</param>
  207. /// <param name="Name">链接点名</param>
  208. public void AddAnchorName(string Content, float FontSize, string Name)
  209. {
  210. SetFont(FontSize);
  211. Anchor auc = new Anchor(Content, font);
  212. auc.Name = Name;
  213. document.Add(auc);
  214. }
  215. #endregion
  216. }
  217. }

发表评论

表情:
评论列表 (有 0 条评论,494人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C# 压缩PDF文档

    PDF文档是我们日常办公中经常用到的文档,有时候PDF文档过大,就不利于传输和发送。这篇文章将介绍如何使用C\和Spire.PDF组件压缩PDF文档。 Spire.PDF中主