使用ItextSharop合并pdf文件,体积变大的解决
通用的合并方式导致输出的pdf 文件中嵌入了大量的重复字体。导致文件体积膨胀。
使用基于内存流的方式,读取文件字节,可以解决重复字体的嵌入问题;
1 public static string MergeFiles(string targetPdfFilesDir)
2 {
3 string outPath = string.Empty;
4 //验证文件是否存在
5 if (!Directory.Exists(targetPdfFilesDir))
6 {
7 throw new FileNotFoundException("指定的目录不存在:" + targetPdfFilesDir);
8 }
9
10 var filePathList = Directory.EnumerateFiles(targetPdfFilesDir, "*.pdf");
11 if (filePathList.IsEmpty())
12 {
13 return outPath;
14 }
15
16 //合并pdf文件
17
18 string runningDir = AppDomainTypeFinder.Instance.GetBinDirectory();
19
20 outPath = Path.Combine(runningDir, "temp", Guid.NewGuid().ToString() + ".pdf");
21
22 MergeFiles(outPath, filePathList.ToArray());
23
24 return outPath;
25 }
26
27
28 public static void MergeFiles(string destinationFile, string[] sourceFiles)
29 {
30
31 try
32 {
33
34 byte[] bs = MergePDFs(sourceFiles);
35 using (var fsm = new FileStream(destinationFile, FileMode.Create))
36 {
37 fsm.Write(bs, 0, bs.Length);
38 fsm.Flush();
39 }
40
41 }
42 catch (Exception e)
43 {
44 string strOb = e.Message;
45 }
46 }
47 /// <summary>
48 /// 合并多个pdf文件,并返回合并后的文件字节
49 /// </summary>
50 /// <param name="pdfFiles"></param>
51 /// <returns></returns>
52 private static byte[] MergePDFs(string[] pdfFiles)
53 {
54 if (pdfFiles == null || pdfFiles.Length <= 0)
55 {
56 return null;
57 }
58 if (pdfFiles.Length == 1)
59 {
60 return File.ReadAllBytes(pdfFiles[0]);
61 }
62
63
64 PdfReader reader;
65 Document document;
66 PdfWriter writer;
67 MemoryStream msFinalPdf;
68 using (msFinalPdf = new MemoryStream())
69 {
70
71 reader = new PdfReader(pdfFiles[0]);
72 using (document = new Document())
73 {
74 //一个PdfSmartCopy基类
75 writer = new PdfSmartCopy(document, msFinalPdf);
76 document.Open();
77
78 for (int k = 0; k < pdfFiles.Length; k++)
79 {
80 reader = new PdfReader(pdfFiles[k]);
81 //将子文件中的页都追加到尾部
82 for (int i = 1; i < reader.NumberOfPages + 1; i++)
83 {
84 ((PdfSmartCopy)writer).AddPage(writer.GetImportedPage(reader, i));
85 }
86 writer.FreeReader(reader);
87
88 }
89 reader.Close();
90 writer.Close();
91 document.Close();
92 }
93 }
94
95 return msFinalPdf.ToArray();
96 }
下面的代码合并pdf 就会产生体积增加。字体重复被嵌入的问题
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
转载于//www.cnblogs.com/micro-chen/p/11093364.html
还没有评论,来说两句吧...