//In the interest of time i will copy past the code for Itext7 Implementation. //idea is a thought process. //so code is almost looks same- //1.create a stream, //2.create a pdf document object, //3.read pdf file from specified location and //4.use Merge Method to copy pdf files. public string MergePDF(List <string> sourceFileList, string outputFilePath) { using (var stream = new FileStream(outputFilePath, FileMode.Create)) { var Outputpdf = new PdfDocument(new PdfWriter(stream)); var pdfMerger = new PdfMerger(Outputpdf); foreach (string file in sourceFileList) { var pdf = new PdfDocument(new PdfReader(file)); int numberofPages = pdf.GetNumberOfPages(); pdfMerger.SetCloseSourceDocuments(true).Merge(pdf, 1, numberofPages); } Outputpdf.Close(); } return(outputFilePath); }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); PdfDocument srcDoc1 = new PdfDocument(new PdfReader(SRC1)); PdfDocument srcDoc2 = new PdfDocument(new PdfReader(SRC2)); PdfDocument srcDoc3 = new PdfDocument(new PdfReader(SRC3)); int numberOfPages1 = srcDoc1.GetNumberOfPages(); int numberOfPages2 = srcDoc2.GetNumberOfPages(); int numberOfPages3 = srcDoc3.GetNumberOfPages(); PdfMerger merger = new PdfMerger(pdfDoc); merger.SetCloseSourceDocuments(true) .Merge(srcDoc1, 1, numberOfPages1) .Merge(srcDoc2, 1, numberOfPages2) .Merge(srcDoc3, 1, numberOfPages3); PdfOutline rootOutline = pdfDoc.GetOutlines(false); int page = 1; PdfOutline helloWorld = rootOutline.AddOutline("Hello World"); helloWorld.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(page))); page += numberOfPages1; PdfOutline link1 = helloWorld.AddOutline("link1"); link1.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(page))); page += numberOfPages2; PdfOutline link2 = rootOutline.AddOutline("Link 2"); link2.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(page))); pdfDoc.Close(); }
internal PdfDocument Merge(string outputFile, PdfMergeTree mergeTree) { Form1.configProgressBar(mergeTree.count); listOptions(); if (!VerifyOutputFile(outputFile)) { throw new IOException("Invalid Output File"); } PdfWriter writer = new PdfWriter(outputFile); destPdf = new PdfDocument(writer); destPdfOutlines = destPdf.GetOutlines(false); PdfMerger merger = new PdfMerger(destPdf, MERGE_TAGS, MERGE_BOOKMARKS); merger.SetCloseSourceDocuments(false); logMsgDbLn2(""); int pageCount = merge(merger, mergeTree.GetMergeItems, 1) - 1; logMsgFmtln("total page count", pageCount); if (pageCount < 1) { destPdf.Close(); return(null); } if (KEEP_IMPORT_BOOKMARKS && ADD_BOOKMARK_FOR_EACH_FILE) { // process and adjust bookmarks MergeOutlines(destPdf.GetOutlines(true).GetAllChildren(), mergeTree.GetMergeItems); } if (!(KEEP_IMPORT_BOOKMARKS && !ADD_BOOKMARK_FOR_EACH_FILE)) { // eliminate all of the current bookmarks destPdfOutlines = clearOutline(destPdf); } if (ADD_BOOKMARK_FOR_EACH_FILE) { // add the new bookmarks addOutline(mergeTree.GetMergeItems, destPdfOutlines, 0); } // all complete // leave open for further processing // destPdf.Close(); // return the final PDF document PdfCatalog destPdfCatalog = destPdf.GetCatalog(); destPdfCatalog.SetPageMode(PdfName.UseOutlines); destPdfCatalog.SetPageLayout(PdfName.SinglePage); destPdfCatalog.SetOpenAction(PdfExplicitDestination.CreateFit(1)); return(destPdf); }