// // Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved. // using System; using System.IO; using System.Collections; using pdftron; using pdftron.Common; using pdftron.Filters; using pdftron.SDF; using pdftron.PDF; using pdftron.PDF.Annots; namespace PDFPageTestCS { /// /// Summary description for Class1. /// class Class1 { private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance(); static Class1() {} /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { PDFNet.Initialize(); try { using (PDFDoc sink = new PDFDoc(PATH_TO_MASTER_PDF)) { sink.InitSecurityHandler(); // assume all the external PDF links annots are on the last page only. ArrayList docsToMerge = new ArrayList(); Page sinkLastPage = sink.GetPage(sink.GetPageCount()); for(int i = 0; i < sinkLastPage.GetNumAnnots(); ++i) { Annot annot = sinkLastPage.GetAnnot(i); if (!annot.IsValid() || annot.GetType() != Annot.Type.e_Link) continue; Link lnk = new Link(annot); pdftron.PDF.Action action = lnk.GetAction(); if (!action.IsValid()) continue; if (action.GetType() == pdftron.PDF.Action.Type.e_URI) { string uri = action.GetSDFObj().Get("URI").Value().GetAsPDFText(); //System.Console.WriteLine(" Links to: " + uri); string rootFolder = ROOT_FOLDER_CONTAINING_EXTERNAL_PDFS; try { int index = sink.GetPageCount() + 1; PDFDoc source = new PDFDoc(rootFolder + uri); source.InitSecurityHandler(); ArrayList importList = new ArrayList(); for (PageIterator itr = source.GetPageIterator(); itr.HasNext(); itr.Next()) { importList.Add(itr.Current()); } ArrayList importedPages = sink.ImportPages(importList); foreach(object o in importedPages) { Page page = o as Page; sink.PagePushBack(page); } Page destPage = sink.GetPage(index); pdftron.PDF.Destination newDest = pdftron.PDF.Destination.CreateFit(destPage); pdftron.PDF.Action newAction = pdftron.PDF.Action.CreateGoto(newDest); lnk.SetAction(newAction); Bookmark bookMark = Bookmark.Create(sink, uri); bookMark.SetAction(newAction); sink.AddRootBookmark(bookMark); } catch(Exception e) { Console.WriteLine(e); } } } sink.Save(OUTPUT_PATH, SDFDoc.SaveOptions.e_linearized); } } catch (Exception e) { Console.WriteLine(e); } } } }