Combining mode common cause output pdf file embedded in a large number of duplicate font. Resulting in volume expansion file.
Stream-based memory mode, read the file bytes, you can solve the problem of duplicate embedded fonts;
. 1 public static String MergeFiles ( String targetPdfFilesDir) 2 { . 3 String outPath = String .Empty; . 4 // verify that the file exists . 5 IF (! Directory.Exists (targetPdfFilesDir)) . 6 { . 7 the throw new new a FileNotFoundException ( " specified directory does not exist : " + targetPdfFilesDir); . 8 } . 9 10 var filePathList = Directory.EnumerateFiles (targetPdfFilesDir, " * of.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 is } 42 is the catch (Exception E) 43 is { 44 is String STROB = e.Message; 45 } 46 is } 47 /// <Summary> 48 /// merge multiple pdf file, and returns the combined file bytes 49 /// </ Summary> 50 /// <param name = "pdfFiles"> </ param> 51 is /// <Returns> </ Returns> 52 is 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 new PdfReader (pdfFiles [K]); 81 // the sub-page file are appended to the end 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 is document.close (); 92 } 93 } 94 95 return msFinalPdf.ToArray (); 96 }
The following code is generated to increase the volume of combined pdf. Duplicate fonts are embedded in question
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, 1f