使用ItextSharop合并pdf文件,体积变大的解决

阳光穿透心脏的1/2处 2021-11-10 02:24 329阅读 0赞

通用的合并方式导致输出的pdf 文件中嵌入了大量的重复字体。导致文件体积膨胀。

使用基于内存流的方式,读取文件字节,可以解决重复字体的嵌入问题;

  1. 1 public static string MergeFiles(string targetPdfFilesDir)
  2. 2 {
  3. 3 string outPath = string.Empty;
  4. 4 //验证文件是否存在
  5. 5 if (!Directory.Exists(targetPdfFilesDir))
  6. 6 {
  7. 7 throw new FileNotFoundException("指定的目录不存在:" + targetPdfFilesDir);
  8. 8 }
  9. 9
  10. 10 var filePathList = Directory.EnumerateFiles(targetPdfFilesDir, "*.pdf");
  11. 11 if (filePathList.IsEmpty())
  12. 12 {
  13. 13 return outPath;
  14. 14 }
  15. 15
  16. 16 //合并pdf文件
  17. 17
  18. 18 string runningDir = AppDomainTypeFinder.Instance.GetBinDirectory();
  19. 19
  20. 20 outPath = Path.Combine(runningDir, "temp", Guid.NewGuid().ToString() + ".pdf");
  21. 21
  22. 22 MergeFiles(outPath, filePathList.ToArray());
  23. 23
  24. 24 return outPath;
  25. 25 }
  26. 26
  27. 27
  28. 28 public static void MergeFiles(string destinationFile, string[] sourceFiles)
  29. 29 {
  30. 30
  31. 31 try
  32. 32 {
  33. 33
  34. 34 byte[] bs = MergePDFs(sourceFiles);
  35. 35 using (var fsm = new FileStream(destinationFile, FileMode.Create))
  36. 36 {
  37. 37 fsm.Write(bs, 0, bs.Length);
  38. 38 fsm.Flush();
  39. 39 }
  40. 40
  41. 41 }
  42. 42 catch (Exception e)
  43. 43 {
  44. 44 string strOb = e.Message;
  45. 45 }
  46. 46 }
  47. 47 /// <summary>
  48. 48 /// 合并多个pdf文件,并返回合并后的文件字节
  49. 49 /// </summary>
  50. 50 /// <param name="pdfFiles"></param>
  51. 51 /// <returns></returns>
  52. 52 private static byte[] MergePDFs(string[] pdfFiles)
  53. 53 {
  54. 54 if (pdfFiles == null || pdfFiles.Length <= 0)
  55. 55 {
  56. 56 return null;
  57. 57 }
  58. 58 if (pdfFiles.Length == 1)
  59. 59 {
  60. 60 return File.ReadAllBytes(pdfFiles[0]);
  61. 61 }
  62. 62
  63. 63
  64. 64 PdfReader reader;
  65. 65 Document document;
  66. 66 PdfWriter writer;
  67. 67 MemoryStream msFinalPdf;
  68. 68 using (msFinalPdf = new MemoryStream())
  69. 69 {
  70. 70
  71. 71 reader = new PdfReader(pdfFiles[0]);
  72. 72 using (document = new Document())
  73. 73 {
  74. 74 //一个PdfSmartCopy基类
  75. 75 writer = new PdfSmartCopy(document, msFinalPdf);
  76. 76 document.Open();
  77. 77
  78. 78 for (int k = 0; k < pdfFiles.Length; k++)
  79. 79 {
  80. 80 reader = new PdfReader(pdfFiles[k]);
  81. 81 //将子文件中的页都追加到尾部
  82. 82 for (int i = 1; i < reader.NumberOfPages + 1; i++)
  83. 83 {
  84. 84 ((PdfSmartCopy)writer).AddPage(writer.GetImportedPage(reader, i));
  85. 85 }
  86. 86 writer.FreeReader(reader);
  87. 87
  88. 88 }
  89. 89 reader.Close();
  90. 90 writer.Close();
  91. 91 document.Close();
  92. 92 }
  93. 93 }
  94. 94
  95. 95 return msFinalPdf.ToArray();
  96. 96 }
  97. 下面的代码合并pdf 就会产生体积增加。字体重复被嵌入的问题
  98. public static void MergeFiles(string destinationFile, string[] sourceFiles) { try { int f = 0; // we create a reader for a certain document PdfReader reader = new PdfReader(sourceFiles[f]); // we retrieve the total number of pages int n = reader.NumberOfPages; //Console.WriteLine("There are " + n + " pages in the original file."); // step 1: creation of a document-object Document document = new Document(reader.GetPageSizeWithRotation(1)); // step 2: we create a writer that listens to the document PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create)); // step 3: we open the document document.Open(); PdfContentByte cb = writer.DirectContent; PdfImportedPage page; int rotation; // step 4: we add content while (f < sourceFiles.Length) { int i = 0; while (i < n) { i++; document.SetPageSize(reader.GetPageSizeWithRotation(i)); document.NewPage(); page = writer.GetImportedPage(reader, i); rotation = reader.GetPageRotation(i); if (rotation == 90 || rotation == 270) { cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height); } else { cb.AddTemplate(page

转载于:https://www.cnblogs.com/micro-chen/p/11093364.html

发表评论

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

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

相关阅读

    相关 Java 合并PDF文件

    处理PDF文档时,我们可以通过合并的方式,来任意合并几个不同的PDF文件,使我们方便的存储和管理文档。例如,在做毕业设计的时候,封面和论文正文往往是两个PDF文档,但是,上交电

    相关 Java合并pdf文件

    Java合并pdf文件 今天帮老师整理资料需要合并pdf文件,下了许多软件发现都需要VIP才行,所以写了个程序来帮助合并,直接在主程序中修改文件路径即可,如下图: !