c# - import named destinations in pdf -


i m developing application in word document converted in pdf. problem complicated please me out.

my word doc has toc, bookmarks, endnotes , hyperlinks. when save doc pdf, bookmarks converted. after long research found pdf documents not support bookmark bookmark hyperlinks, needs either page number or named destinations.

so choose named destinations purpose, stuck again , because simple "save as" cannot generate named destinations in pdf doc. print word doc on adobe pdf printer , got named destination required, again document neither have bookmarks in nor hyperlinks. decided generate 2 pdf word, first save option , second 1 printing.

  1. test.pdf (by save as) (contains bookmarks, hyperlinks)
  2. test_p.pdf( printing) (only contains named destination)

then research ones again , found way extract named destination test_p.pdf xml function of itextsharp.but unfortunately dont way import xml in test.pdf.. thats why came here.

guide me next if approach ok. else suggest me ohter approach accomplish mission.

i wrote class replace urls in pdf files times ago:

using system; using system.collections.generic; using system.io; using system.linq; using itextsharp.text.pdf;  namespace replacelinks {     public class replacepdflinks     {         dictionary<string, pdfobject> _nameddestinations;         pdfreader _reader;          public string inputpdf { set; get; }         public string outputpdf { set; get; }         public func<uri, string> uritonameddestination { set; get; }          public void start()         {             updatepdflinks();             savechanges();         }          private pdfarray getannotationsofcurrentpage(int pagenumber)         {             var pagedictionary = _reader.getpagen(pagenumber);             var annotations = pagedictionary.getasarray(pdfname.annots);             return annotations;         }          private static bool hasaction(pdfdictionary annotationdictionary)         {             return annotationdictionary.get(pdfname.subtype).equals(pdfname.link);         }          private static bool isuriaction(pdfdictionary annotationaction)         {             return annotationaction.get(pdfname.s).equals(pdfname.uri);         }          private void replaceuriwithlocaldestination(pdfdictionary annotationaction)         {             var uri = annotationaction.get(pdfname.uri) pdfstring;             if (uri == null)                 return;              if (string.isnullorwhitespace(uri.tostring()))                 return;              var nameddestination = uritonameddestination(new uri(uri.tostring()));             if (string.isnullorwhitespace(nameddestination))                 return;              pdfobject entry;             if (!_nameddestinations.trygetvalue(nameddestination, out entry))                 return;              annotationaction.remove(pdfname.s);             annotationaction.remove(pdfname.uri);              var newlocaldestination = new pdfarray();             annotationaction.put(pdfname.s, pdfname.goto);             var xref = ((pdfarray)entry).first(x => x pdfindirectreference);             newlocaldestination.add(xref);             newlocaldestination.add(pdfname.fith);             annotationaction.put(pdfname.d, newlocaldestination);         }          private void savechanges()         {             using (var filestream = new filestream(outputpdf, filemode.create, fileaccess.write, fileshare.none))             using (var stamper = new pdfstamper(_reader, filestream))             {                 stamper.close();             }         }          private void updatepdflinks()         {             _reader = new pdfreader(inputpdf);             _nameddestinations = _reader.getnameddestinationfromstrings();              var pagecount = _reader.numberofpages;             (var = 1; <= pagecount; i++)             {                 var annotations = getannotationsofcurrentpage(i);                 if (annotations == null || !annotations.any())                     continue;                  foreach (var annotation in annotations.arraylist)                 {                     var annotationdictionary = (pdfdictionary)pdfreader.getpdfobject(annotation);                      if (!hasaction(annotationdictionary))                         continue;                      var annotationaction = annotationdictionary.get(pdfname.a) pdfdictionary;                     if (annotationaction == null)                         continue;                      if (!isuriaction(annotationaction))                         continue;                      replaceuriwithlocaldestination(annotationaction);                 }             }         }     }     } 

to use it:

    new replacepdflinks     {         inputpdf = @"test.pdf",         outputpdf = "mod.pdf",         uritonameddestination = uri =>         {             if (uri.host.tolowerinvariant().contains("google.com"))             {                 return "entry1";             }              return string.empty;         }     }.start(); 

this sample modify of urls containing google.com point specific named destination "entry1". , sample file test above class:

void writefile() {     using (var doc = new document(pagesize.letter))     {         using (var fs = new filestream("test.pdf", filemode.create))         {             using (var writer = pdfwriter.getinstance(doc, fs))             {                 doc.open();                 var bluefont = fontfactory.getfont("arial", 12, font.normal, basecolor.blue);                 doc.add(new chunk("go url", bluefont).setaction(new pdfaction("http://www.google.com/", false)));                  doc.newpage();                 doc.add(new chunk("go test", bluefont).setlocalgoto("entry1"));                  doc.newpage();                 doc.add(new chunk("test").setlocaldestination("entry1"));                  doc.close();             }         }     } } 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -