经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
【转载】 C#工具类:使用iTextSharp操作PDF文档
来源:cnblogs  作者:江湖逍遥  时间:2019/3/11 9:11:34  对本文有异议

iTextSharp是一个用于操作PDF文件的组件DLL程序,在C#程序中可以引用iTextSharp组件,用于开发与PDF文件相关的报表等功能,利用iTextSharp组件提供出来的方法接口,我们可以实现很多与PDF文档有关的操作,如打开PDF文档对象、往PDF文档中添加段落、添加图片链接等等,功能非常的强大。这边简单对iTextSharp类进行了封装,提供一些常用的PDF操作方法。

iTextSharp官网:http://www.itextpdf.com/  (英文好的建议直接查看原始文档)。

在Visual Studio开发的项目的过程中,你可以手动引入iTextSharp的两个DLL文件到你的项目中,引入成功后即可在项目中使用。如果你的Visual Studio安装有NuGet  工具,可以通过NuGet工具来自动安装,如下图:

依托iTextSharp组件封装好的PDF操作帮助类如下,只包含一些简单的操作,如果读者有更复杂的需求,请查阅官方文档后熟悉后自行封装编写:

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

 

扩展阅读:C#工具类:使用SharpZipLib进行压缩、解压文件

备注:此文章转载自博主个人技术站点:IT技术小趣屋。原文链接:查看原文

 

原文链接:http://www.cnblogs.com/xu-yi/p/10501908.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号