最近做了一个系统需要把文件转换为pdf然后把转换后的pdf合成一个pdf文件,网上搜索了半天,最终决定使用itestsharp.dll配合Aspose.words和Aspose.cells来做,废话少说,上代码……

 #region 文件转换pdfpublic void ConvertImageToPdf(string inputFileName, string outputFileName){Aspose.Words.Document doc = new Aspose.Words.Document();DocumentBuilder builder = new DocumentBuilder(doc);using (System.Drawing.Image image = System.Drawing.Image.FromFile(inputFileName)){FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);int framesCount = image.GetFrameCount(dimension);for (int frameIdx = 0; frameIdx < framesCount; frameIdx++){if (frameIdx != 0)builder.InsertBreak(BreakType.SectionBreakNewPage);image.SelectActiveFrame(dimension, frameIdx);Aspose.Words.PageSetup ps = builder.PageSetup;if (image.Width > 2000){ps.PageWidth = ConvertUtil.PixelToPoint(image.Width / 2, image.HorizontalResolution);ps.PageHeight = ConvertUtil.PixelToPoint(image.Height / 2, image.VerticalResolution);}else{ps.PageWidth = ConvertUtil.PixelToPoint(image.Width-300, image.HorizontalResolution);ps.PageHeight = ConvertUtil.PixelToPoint(image.Height-100, image.VerticalResolution);}builder.InsertImage(image, RelativeHorizontalPosition.Page,100, RelativeVerticalPosition.Page, 0, ps.PageWidth, ps.PageHeight, WrapType.Inline);}}doc.Save(outputFileName);}//Word转换成pdf/// <summary>/// 把Word文件转换成为PDF格式文件/// </summary>/// <param name="sourcePath">源文件路径</param>/// <param name="targetPath">目标文件路径</param> /// <returns>true=转换成功</returns>public bool DOCConvertToPDF(string sourcePath, string targetPath){try{Aspose.Words.Document doc = new Aspose.Words.Document(sourcePath);doc.Save(targetPath, Aspose.Words.SaveFormat.Pdf);return true;}catch (Exception ex){return false;}}/// <summary>/// 把Excel文件转换成PDF格式文件/// </summary>/// <param name="sourcePath">源文件路径</param>/// <param name="targetPath">目标文件路径</param>/// <returns>true=转换成功</returns>public bool XLSConvertToPDF(string sourcePath, string targetPath){try{Workbook workbook = new Workbook(sourcePath);workbook.CreateStyle();workbook.Save(targetPath, Aspose.Cells.SaveFormat.Pdf);                  return true;}catch (Exception ex){return false;}}/// <summary>/// txt转pdf/// </summary>/// <param name="sourcePath"></param>/// <param name="targetPath"></param>/// <returns></returns>public bool TxtConvertToPDF(string sourcePath, string targetPath){try{StreamReader reader = new StreamReader(sourcePath, Encoding.Default); //使用默认编码,否则转换后乱码string text = reader.ReadToEnd();Aspose.Words.Document doc = new Aspose.Words.Document();Aspose.Words.DocumentBuilder builder = new DocumentBuilder(doc);builder.Write(text);doc.Save(targetPath, Aspose.Words.SaveFormat.Pdf);reader.Close();return true;}catch (Exception ex){return false;}}//Word转换成pdf/// <summary>/// 把Word文件转换成为PDF格式文件/// </summary>/// <param name="sourcePath">源文件路径</param>/// <param name="targetPath">目标文件路径</param> /// <returns>true=转换成功</returns>public bool ConvertCebToPdf(string sourcePath, string targetPath){try{Aspose.Words.Document doc = new Aspose.Words.Document(sourcePath);doc.Save(targetPath, Aspose.Words.SaveFormat.Pdf);return true;}catch (Exception ex){return false;}}#endregion
<p>/// <summary>/// 合并pdf/// </summary>/// <param name="fileList">pdf路径集合</param>/// <param name="outMergeFile"></param>public void mergePDFFiles(List<string> fileList, string outMergeFile, string headerText){iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);  try{PdfWriter instance = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create));document.Open();PdfContentByte directContent = instance.DirectContent;BaseFont baseFont = BaseFont.CreateFont(@"C:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);#region 目录{document.Add(new iTextSharp.text.Paragraph("\r\r\r"));</p><p>                    iTextSharp.text.Paragraph p1 = new iTextSharp.text.Paragraph(new Phrase(headerText, new iTextSharp.text.Font(baseFont)));p1.Alignment = iTextSharp.text.Rectangle.ALIGN_CENTER;  //居中document.Add(p1);document.Add(new iTextSharp.text.Paragraph("\r\r"));int num = 2;for (int i = 0; i < fileList.Count; i++){PdfReader pdfReader = new PdfReader(fileList[i].Split('$')[0]);int numberOfPages = pdfReader.NumberOfPages;string text = string.Format("{0}. {1}{2}\n\n", (i + 1), fileList[i].Split('$')[2].PadRight((80 - fileList[i].Split('$')[2].Length), '.'), num);iTextSharp.text.Anchor anchor = new iTextSharp.text.Anchor(text, new iTextSharp.text.Font(baseFont));anchor.Reference = "#link" + i;document.Add(anchor);num += numberOfPages;}/*for (int i = 0; i < fileList.Count; i++){PdfReader pdfReader = new PdfReader(fileList[i].Split('$')[0]);string text = string.Format("{0}. {1}\n\n", (i + 1), fileList[i].Split('$')[2]);iTextSharp.text.Anchor anchor = new iTextSharp.text.Anchor(text, new iTextSharp.text.Font(baseFont));anchor.Reference = "#link" + i;document.Add(anchor);}*/</p><p>                }#endregion</p><p>                #region 页码{iTextSharp.text.HeaderFooter footer = new iTextSharp.text.HeaderFooter(new iTextSharp.text.Phrase("Page:"), true);footer.Border = iTextSharp.text.Rectangle.TITLE;document.Footer = footer;}#endregion</p><p>                #region 文件合并int nn = 0;for (int i = 0; i < fileList.Count; i++){document.NewPage();PdfReader pdfReader = new PdfReader(fileList[i].Split('$')[0]);//添加锚点Anchor anchor = new Anchor((i + 1) + "、 " + fileList[i].Split('$')[2] + "\n\n", new iTextSharp.text.Font(baseFont));anchor.Name = "link" + i;document.Add(anchor);</p><p>                    int numberOfPages = pdfReader.NumberOfPages;for (int j = 1; j <= numberOfPages; j++){if (nn == numberOfPages)document.NewPage();PdfImportedPage importedPage = instance.GetImportedPage(pdfReader, j);directContent.AddTemplate(importedPage, 0f, 0f);nn = numberOfPages;}</p><p>                }#endregiondocument.Close();}catch (Exception ex){document.Close();}}</p>

转载于:https://www.cnblogs.com/seanchang/p/5202751.html

itextsharp、Aspose.Words、Aspose.Cells联合使用相关推荐

  1. EpPlus读取生成Excel帮助类+读取csv帮助类+Aspose.Cells生成Excel帮助类

    大部分功能逻辑都在,少量自定义异常类和扩展方法 ,可用类似代码自己替换 //EpPlus读取生成Excel帮助类+读取csv帮助类,epplus只支持开放的Excel文件格式:xlsx,不支持 xls ...

  2. 用于制图、写电子邮件、创建条形码控件Aspose.Total

    2019独角兽企业重金招聘Python工程师标准>>> Aspose.Total 提供了 Aspose 可以使用的任何组件,以及你每年订阅中发布的所有新组件.通过它,你能够有计划地操 ...

  3. 文档管理套包Aspose.Total 5月更新 | 附下载

    2019独角兽企业重金招聘Python工程师标准>>> Aspose.Total 是一个文档管理套包,能为.NET和JAVA应用程序增加图表.电子邮件.拼写检查.条码.流程.文件格式 ...

  4. 关于Aspose对于Word操作的一些扩展及思考

    Aspose.word Aspose.Words是一款先进的类库,通过它可以直接在各个应用程序中执行各种文档处理任务.Aspose.Words支持DOC,OOXML,RTF,HTML,OpenDocu ...

  5. Aspose实现word转图片、pdf

    原文地址:http://www.jiajiajia.club/blog/artical/3z995nkc4c39/529 Aspose   Aspose.Total是Aspose公司旗下的最全的一套o ...

  6. java通过aspose实现文档间格式转换

    aspose简述 Aspose.Total是Aspose公司旗下的最全的一套office文档管理方案,主要提供.net跟java两个开发语言的控件套包,通过它,我们可以有计划地操纵一些商业中最流行的文 ...

  7. Java使用Aspose组件进行多文档间的转换操作

    什么是Aspose? Aspose.Total是Aspose公司旗下的最全的一套office文档管理方案,主要提供.net跟java两个开发语言的控件套包,通过它,我们可以有计划地操纵一些商业中最流行 ...

  8. 可预览文件转PDF-使用Aspose

    Word.Excel.TXT.PDF文件等转PDF--使用Aspose 需求背景 Word.Excel文档转PDF PPT转PDF 图片转PDF 需求背景 支持各种文件上传,并支持部分文件的预览. 由 ...

  9. Aspose实现word、excel、ppt转pdf

    1. 工具类 AsposeUtil @Component @Slf4j public class AsposeUtil {private static final String[] WORD = {& ...

  10. Aspose.Total for C++是一个完整的C++文件格式自动化库包

    用于 Microsoft Office 文件格式.PDF 和条形码的 C++ API 开发本机 C++ 应用程序来处理 Microsoft Project.Excel.Word.PowerPoint. ...

最新文章

  1. VS远程调试(Visual Studio)远程连接到linux cmake(跨平台开发)(适用于VS2019,且远程目标平台cmake版本大于等于3.8)
  2. jzoj4216-[NOIP2015模拟9.12]平方和【Splay】
  3. 编程实战:如何管理代码里的常量
  4. 5.5.35 - mysql 同步_MySQL 5.6.35主从同步配置案例
  5. Java开发技术有哪些?
  6. Java下一代高并发技术——虚拟线程“JEP 425: Virtual Threads (Preview)”
  7. GIS与CAD在村庄规划中的图框位置一致处理
  8. 2019最新黑马视频教程分享给大家十次方乐优商城项目下载
  9. css 动态设定div背景颜色
  10. python计算机视觉:创建缩略图
  11. 视频号账号定位怎么做?如何做微信视频号定位
  12. mysql 表改名_MySQL库改名、表改名
  13. 微博数据分析工具限时福利!购买西瓜微数加送会员时长 ,最多加赠1个月!
  14. 使用Umi SSR 报错 Warning: Did not expect server HTML to contain a <div> in <div>.
  15. 5000元投影仪性能排行榜,当贝F5遥遥领先其他投影仪
  16. 杨辉三角c语言程序jian,杨辉三角C语言程序队列实现(带源码+解析)
  17. ANSI编码文件批量转换为UTF-8编码小tips
  18. 抽象代数----Sn可由两个元素生成
  19. CTFHub----RCE
  20. 基因序列分析(生物信息学论坛)

热门文章

  1. windows系统误删引导分区后如何重装系统
  2. 计算机管理里找不到固态硬盘,教大家电脑插上固态硬盘提示找不到设备怎么办...
  3. 最新搜狗收录提交工具分享
  4. Java练习10:输入两个正整数m和n,求其最大公约数和最小公倍数
  5. 网络地址转换—NAT——总结
  6. [Android稳定性] Android Fd Leak问题分析方法
  7. 2020年全国城市信息模型(CIM)新政策汇总
  8. 【测试】使用xtts V4迁移数据
  9. GPS定位原理、系统组成及工作频段
  10. windows删除文件时需要管理员权限的方法