protected void ManipulatePdf(String dest) { // Creates directory and new pdf file by content of the read pdf FileInfo file = new FileInfo(PDF); file.Directory.Create(); PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(PDF)); for (int i = 1; i < pdfDoc.GetNumberOfPages();) { if (GetFactors(++i).Count > 1) { continue; } // Adding named destinations for further usage depending on the needs PdfPage pdfPage = pdfDoc.GetPage(i); Rectangle pageRect = pdfPage.GetPageSize(); float getLeft = pageRect.GetLeft(); float getTop = pageRect.GetTop(); PdfExplicitDestination destObj = PdfExplicitDestination.CreateXYZ(pdfPage, getLeft, getTop, 1); pdfDoc.AddNamedDestination("prime" + i, destObj.GetPdfObject()); } pdfDoc.Close(); CreateXml(PDF, dest); }
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 virtual void DestTest01() { String srcFile = sourceFolder + "simpleNoLinks.pdf"; String outFile = destinationFolder + "destTest01.pdf"; String cmpFile = sourceFolder + "cmp_destTest01.pdf"; PdfDocument document = new PdfDocument(new PdfReader(srcFile), new PdfWriter(outFile)); PdfPage firstPage = document.GetPage(1); PdfLinkAnnotation linkExplicitDest = new PdfLinkAnnotation(new Rectangle(35, 785, 160, 15)); linkExplicitDest.SetAction(PdfAction.CreateGoTo(PdfExplicitDestination.CreateFit(document.GetPage(2)))); firstPage.AddAnnotation(linkExplicitDest); PdfLinkAnnotation linkStringDest = new PdfLinkAnnotation(new Rectangle(35, 760, 160, 15)); PdfExplicitDestination destToPage3 = PdfExplicitDestination.CreateFit(document.GetPage(3)); String stringDest = "thirdPageDest"; document.AddNamedDestination(stringDest, destToPage3.GetPdfObject()); linkStringDest.SetAction(PdfAction.CreateGoTo(new PdfStringDestination(stringDest))); firstPage.AddAnnotation(linkStringDest); document.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFile, cmpFile, destinationFolder, "diff_" )); }
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(); }