架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 22806|回复: 2

[插件库] itextsharp生成PDF的一个开源组件

[复制链接]
发表于 2016-4-6 23:26:13 | 显示全部楼层 |阅读模式
  1. /// <summary>
  2.         /// 把图片转换成PDF
  3.         /// </summary>
  4.         /// <param name="pathName">上传的图片路径和文件名</param>
  5.         /// <returns></returns>
  6.         public static string ImageToPDF(List<string> listPath)
  7.         {
  8.             iTextSharp.text.Document document = new iTextSharp.text.Document();
  9.             string newFileName = RandomNum(listPath[0]);
  10.             string tagrt = GetFolder() + GetNewFileName(newFileName) + ".pdf";
  11.             if (tagrt == null)
  12.             {
  13.                 return null;
  14.             }
  15.             try
  16.             {
  17.                 iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(tagrt, FileMode.CreateNew));
  18.                 document.Open();
  19.                 iTextSharp.text.pdf.BaseFont bfChinese = iTextSharp.text.pdf.BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);
  20.                 iTextSharp.text.Font Titlefont = new iTextSharp.text.Font(bfChinese, 16, iTextSharp.text.Font.BOLD, new iTextSharp.text.BaseColor(0, 0, 0));
  21.                 for (int i = 0; i < listPath.Count; i++)
  22.                 {
  23.                     document.Add(new iTextSharp.text.Paragraph("", Titlefont));
  24.                     iTextSharp.text.Image je = iTextSharp.text.Image.GetInstance(listPath[i]);//path就是函数入参,即你要转成PDF的图片路径
  25.                     je.ScalePercent(72f);
  26.                     document.Add(je);
  27.                 }
  28.             }
  29.             catch (Exception de)
  30.             {
  31.                 document.Close();
  32.                 return null;
  33.             }
  34.             document.Close();
  35.             return tagrt;
  36.         }
复制代码






上一篇:Microsoft.Office.Interop.Word把word文档转换成pdf
下一篇:无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”。请改用...
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
 楼主| 发表于 2016-4-7 16:25:46 | 显示全部楼层
  1. /// <summary>
  2.         /// 填充表单文本域
  3.         /// </summary>
  4.         /// <param name="templatePath"></param>
  5.         /// <param name="newFilePath"></param>
  6.         /// <param name="parameters"></param>
  7.         public static void GetEnPdf(string templatePath, string newFilePath, Dictionary<string, string> parameters)
  8.         {
  9.             System.IO.File.Delete(newFilePath);
  10.             PdfReader pdfReader = new PdfReader(templatePath);
  11.             PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFilePath,FileMode.Create));
  12.             //获取域的集合;
  13.             AcroFields pdfFormFields = pdfStamper.AcroFields;
  14.             BaseFont BF_Light = BaseFont.CreateFont(@"C:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  15.             //为需要赋值的域设置值;
  16.             foreach (KeyValuePair<string, string> parameter in parameters)
  17.             {
  18.                 pdfFormFields.SetFieldProperty(parameter.Key, "textfont", BF_Light, null);
  19.                 pdfFormFields.SetField(parameter.Key, parameter.Value);
  20.             }
  21.             //这句很重要,如果为false那么生成的PDF文件还能编辑,一定要设为true;
  22.             pdfStamper.FormFlattening = true;
  23.             pdfStamper.Close();
  24.             pdfReader.Close();
  25.         }
复制代码


码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2021-12-3 13:31:38 | 显示全部楼层
学习12312312
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2026-4-12 19:57

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表