private void GetSubFolders(DirectoryInfo sourceDirectoryInfo) { DirectoryInfo[] directories = sourceDirectoryInfo.GetDirectories(); for (int i = 0; i < (int)directories.Length; i++) { this.GetSubFolders(directories[i]); } this.mergeUtility = new PDFMergerUtility(); FileInfo[] files = sourceDirectoryInfo.GetFiles("*.pdf"); for (int j = 0; j < (int)files.Length; j++) { FileInfo fileInfo = files[j]; this.mergeUtility.addSource(fileInfo.FullName); } if (!PDFHelper.AddStamp) { this.mergeUtility.setDestinationFileName(Path.Combine(this.outputFolder, string.Concat(sourceDirectoryInfo.Name, ".pdf"))); this.mergeUtility.mergeDocuments(); } else { string str = Path.Combine(Path.GetTempPath(), string.Concat("aquaforest\\pdftoolkit\\", Path.GetRandomFileName(), ".pdf")); this.mergeUtility.setDestinationFileName(str); this.mergeUtility.mergeDocuments(); PDDocument pDDocument = PDDocument.load(new java.io.File(str)); pDDocument = PDFHelper.AddTrialStampIfNecessary(pDDocument); pDDocument.save(Path.Combine(this.outputFolder, string.Concat(sourceDirectoryInfo.Name, ".pdf"))); if (System.IO.File.Exists(str)) { System.IO.File.Delete(str); } } }
static void CollatePDF(int compnumber, InvoiceStruct capture) { fileSetup(compnumber); bool isMiddle; PDDocument child;//the newly addeed pdf, and the masterpdf PDDocument master; child = PDDocument.load(@"..\..\collate\child.pdf"); if (!File.Exists(Path.GetFullPath(@"..\..\collate\master.pdf"))) { child.save(@"..\..\collate\newmaster.pdf");//if the master doesn't exist, the child is the master writeMaster(capture); child.close(); return; } master = PDDocument.load(@"..\..\collate\master.pdf");//if exists, load master PDFTextStripper strip = new PDFTextStripper(); Splitter split = new Splitter(); PDFMergerUtility merge = new PDFMergerUtility(); int pageNumber = master.getNumberOfPages() + 1; isMiddle = false; for (int x = 1; x <= master.getNumberOfPages(); x++) { strip.setStartPage(x); strip.setEndPage(x);//only extracting the specified page string text = strip.getText(master); string markerS = text.Substring(text.IndexOf("Invoice #") + 11, 6); int idNo = Int32.Parse(markerS); if (compnumber < idNo) //get the invoice number. If it's greater than the new imported one, then that's where it is spliced in. { isMiddle = true; pageNumber = x; break; } } java.util.List splittedDocuments = split.split(master); if (!isMiddle) { merge.appendDocument(master, child);//if the page number goes at the end, the master is collated on the child, no issues. master.save(@"..\..\collate\newmaster.pdf"); } else { PDDocument result; result = PDDocument.load(@"..\..\blank.pdf"); for (int y = 1; y < master.getNumberOfPages(); y++) { if (pageNumber == y) { merge.appendDocument(result, child);//once we reach the right page number, the child is appended there } merge.appendDocument(result, (PDDocument)splittedDocuments.get(y - 1)); //we have to cast to PDDocument because of Java and .NET have clashes in syntax and we can't initialize them that way. } result.removePage(0);//A blank page is used at the beginning because of issues with IKVM and empty PDFs. result.save(@"..\..\collate\newmaster.pdf"); result.close(); } //} child.close(); master.close(); writeMaster(capture); }
public PDFMerger() { this.mergeUtility = new PDFMergerUtility(); }