private static void MergePdfsWithBookmarks(List <string> InFiles, string OutFile) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(OutFile)); pdfDoc.InitializeOutlines(); PdfMerger merger = new PdfMerger(pdfDoc, true, true); List <PdfOutline> listItem = new List <PdfOutline>(); InFiles.ForEach(srcFile => { PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(srcFile)); int pageCount = firstSourcePdf.GetNumberOfPages(); merger.Merge(firstSourcePdf, 1, pageCount); firstSourcePdf.GetOutlines(false).GetDestination(); firstSourcePdf.Close(); if (pageCount % 2 == 1) { pdfDoc.AddNewPage(iTextPageSize.A4); } }); PdfOutline rootOutline = pdfDoc.GetOutlines(false); listItem.AddRange(rootOutline.GetAllChildren()); rootOutline.GetAllChildren().Clear(); string parentTitle = Path.GetFileNameWithoutExtension(OutFile); PdfOutline parent = rootOutline.AddOutline(parentTitle); foreach (var item in listItem) { parent.AddOutline(item); } PdfExplicitDestination destToPage3 = PdfExplicitDestination.CreateFit(pdfDoc.GetFirstPage()); string stringDest = Guid.NewGuid().ToString(); pdfDoc.AddNamedDestination(stringDest, destToPage3.GetPdfObject()); parent.AddAction(PdfAction.CreateGoTo(new PdfStringDestination(stringDest))); if (pdfDoc.GetNumberOfPages() % 2 == 1) { pdfDoc.AddNewPage(iTextPageSize.A4); } pdfDoc.Close(); }
public void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC)); // This method returns a complete outline tree of the whole document. // If the flag is false, the method gets cached outline tree (if it was cached // via calling getOutlines method before). PdfOutline outlines = pdfDoc.GetOutlines(false); IList <PdfOutline> bookmarks = outlines.GetAllChildren(); pdfDoc.Close(); List <String> titles = new List <String>(); foreach (PdfOutline bookmark in bookmarks) { AddTitle(bookmark, titles); } // See title's names in the console foreach (String title in titles) { Console.WriteLine(title); } CreateResultTxt(dest, titles); }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); PdfOutline outlines = pdfDoc.GetOutlines(false); IList <PdfOutline> children = outlines.GetAllChildren()[0].GetAllChildren(); ChangeList(children); pdfDoc.Close(); }
private static void MergePdfFiles(string outputFile, List <string> sourceFiles) { using (var writer = new PdfWriter(new FileStream(outputFile, FileMode.OpenOrCreate))) { using (var outputDocument = new PdfDocument(writer)) { outputDocument.InitializeOutlines(); var rootOutline = outputDocument.GetOutlines(false); PdfOutline rootO = rootOutline.AddOutline("Root"); int pages = 1; int count = 1; foreach (var inputFile in sourceFiles) { using (var inputDoc = new PdfDocument(new PdfReader((inputFile)))) { for (int i = 1; i <= inputDoc.GetNumberOfPages(); i++) { var newp = outputDocument.AddNewPage(); var canvas = new PdfCanvas(newp); var origPage = inputDoc.GetPage(i); var copy = origPage.CopyAsFormXObject(outputDocument); canvas.AddXObject(copy, 0, 0); copy.Flush(); origPage = null; canvas.Release(); newp.Flush(); writer.Flush(); canvas = null; newp = null; } var subPages = inputDoc.GetNumberOfPages(); //PdfOutline pdfOutine = inputDoc.GetOutlines(false); /*var link1 = rootO.AddOutline(count.ToString()); * link1.AddDestination(PdfExplicitDestination.CreateFit(outputDocument.GetPage(pages))); * pages += subPages;*/ PdfOutline pdfOutine = inputDoc.GetOutlines(false); //pdfOutine.GetAllChildren().FirstOrDefault().AddOutline; foreach (var aOutline in pdfOutine.GetAllChildren()) { var link1 = rootO.AddOutline(aOutline.GetTitle(), aOutline.pos); link1.AddDestination(PdfExplicitDestination.CreateFit(outputDocument.GetPage(pages))); } pages += subPages; count++; } } } } }
public virtual void RemoveOneOutlineSubtree(String DEST) { String input = sourceFolder + "my_output_pdf.pdf"; PdfReader reader = new PdfReader(input); PdfWriter writer = new PdfWriter(DEST); PdfDocument pdfDocument = new PdfDocument(reader, writer); PdfOutline root = pdfDocument.GetOutlines(true); PdfOutline toRemove = root.GetAllChildren().Get(0); toRemove.RemoveOutline(); pdfDocument.Close(); }
// This recursive method calls itself if an examined bookmark entry has kids. // The method writes bookmark title to the passed list private void AddTitle(PdfOutline outline, List <String> result) { String bookmarkTitle = outline.GetTitle(); result.Add(bookmarkTitle); IList <PdfOutline> kids = outline.GetAllChildren(); if (kids != null) { foreach (PdfOutline kid in kids) { AddTitle(kid, result); } } }
protected void GetBookmark(PdfOutline outline, IDictionary <string, PdfObject> names, PdfDocument pdfDoc) { if (outline.GetDestination() != null) { BookItem bookItem; bookItem.title = outline.GetTitle(); PdfObject pageNumber = outline.GetDestination() .GetDestinationPage(names); bookItem.page = pdfDoc.GetPageNumber(pdfDoc.GetPage((PdfDictionary)pageNumber)).ToString(); _outline.Add(bookItem); } foreach (PdfOutline child in outline.GetAllChildren()) { GetBookmark(child, names, pdfDoc); } }
public void listOutline(PdfDocument pdfDoc, PdfOutline outline) { logMsgFmtln("bookmark| depth", formatBookmark(outline.GetTitle(), depth, outline.GetPageNumber(pdfDoc))); IList <PdfOutline> kids = outline.GetAllChildren(); if (kids.Count != 0) { depth++; for (int i = 0; i < kids.Count; i++) { listOutline(pdfDoc, kids[i]); } depth--; } }
private void addExistBookmark2(PdfOutline destOutline, List <MergeItem> mergeTreeList, int depth) { int pageNumber = destOutline.GetPageNumber(destPdf); List <MergeItem> newMergeTreeList = new List <MergeItem>(1); mergeTreeList.Add(new MergeItem(destOutline.GetTitle(), LEAF, newMergeTreeList, pageNumber, depth, new FileItem())); IList <PdfOutline> children = destOutline.GetAllChildren(); if (children.Count > 0) { foreach (PdfOutline child in children) { addExistBookmark2(child, newMergeTreeList, depth + 1); } } }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest)); int insertPageNumber = 4; // Copier contains the additional logic to copy acroform fields to a new page. // PdfPageFormCopier uses some caching logic which can potentially improve performance // in case of the reusing of the same instance. PdfPageFormCopier formCopier = new PdfPageFormCopier(); PdfDocument insertDoc = new PdfDocument(new PdfReader(INSERT)); insertDoc.CopyPagesTo(1, 1, pdfDoc, insertPageNumber, formCopier); insertDoc.Close(); PdfOutline outlines = pdfDoc.GetOutlines(false); PdfOutline outline = outlines.GetAllChildren()[0].AddOutline("Hello", insertPageNumber - 1); outline.AddDestination(PdfExplicitDestination.CreateFit(pdfDoc.GetPage(insertPageNumber))); pdfDoc.Close(); }
private static void ListBookmarks(PdfOutline outline, IDictionary <String, PdfObject> names, PdfDocument document, List <Bookmark> bookmarks, int level, Guid parentId) { Bookmark mark = null; if (outline.GetDestination() != null) { mark = new Bookmark(outline.GetTitle(), document.GetPageNumber((PdfDictionary)outline.GetDestination().GetDestinationPage(names))); mark.Level = level; mark.ParentId = parentId; bookmarks.Add(mark); parentId = mark.Id; } level++; foreach (PdfOutline child in outline.GetAllChildren()) { ListBookmarks(child, names, document, bookmarks, level, parentId); } }
private static void MergePdfs(List <string> InFiles, string OutFile) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(OutFile)); pdfDoc.InitializeOutlines(); //PdfMerger merger = new PdfMerger(pdfDoc, false, false); PdfOutline rootOutline = pdfDoc.GetOutlines(true); string parentTitle = Path.GetFileNameWithoutExtension(OutFile); PdfOutline parent = rootOutline.AddOutline(parentTitle); //parent.AddAction(PdfAction.CreateGoTo( // PdfExplicitRemoteGoToDestination.CreateFit(1))); int pageNumber = 0; foreach (var srcFile in InFiles) { string title = Path.GetFileNameWithoutExtension(srcFile); PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(srcFile)); int pageCount = firstSourcePdf.GetNumberOfPages(); for (int i = 1; i < pageCount + 1; i++) { var page = firstSourcePdf.GetPage(i); int pageRotate = page.GetRotation(); if (pageRotate != 0) { page.SetRotation(0); } PdfPage newPage = firstSourcePdf.GetPage(i).CopyTo(pdfDoc); pdfDoc.AddPage(newPage); } //merger.Merge(firstSourcePdf, 1, firstSourcePdf.GetNumberOfPages()); //firstSourcePdf.CopyPagesTo(1, firstSourcePdf.GetNumberOfPages(), pdfDoc); //int all = pdfDoc.GetNumberOfPages(); PdfExplicitDestination dd = PdfExplicitDestination.CreateFit(pdfDoc.GetPage(pageNumber + 1)); string tt = Guid.NewGuid().ToString(); pdfDoc.AddNamedDestination(tt, dd.GetPdfObject()); PdfOutline kid = parent.AddOutline(title); kid.AddAction(PdfAction.CreateGoTo(new PdfStringDestination(tt))); //kid.AddAction(PdfAction.CreateGoTo( //PdfExplicitRemoteGoToDestination.CreateFit(pageNumber))); var bb = parent.GetAllChildren(); pageNumber += pageCount; firstSourcePdf.Close(); if (pageCount % 2 == 1) { pdfDoc.AddNewPage(iTextPageSize.A4); pageNumber += 1; } } PdfExplicitDestination destToPage3 = PdfExplicitDestination.CreateFit(pdfDoc.GetFirstPage()); string stringDest = Guid.NewGuid().ToString(); pdfDoc.AddNamedDestination(stringDest, destToPage3.GetPdfObject()); parent.AddAction(PdfAction.CreateGoTo(new PdfStringDestination(stringDest))); if (pdfDoc.GetNumberOfPages() % 2 == 1) { pdfDoc.AddNewPage(pageNumber, iTextPageSize.A4); } pdfDoc.Close(); }