简体   繁体   中英

How to Import Bookmark from xml to existing pdf?

I am currently working on pdf projects (dotnet/c#[ Itextsharp pdf ]), I want to export and import bookmarks from one pdf to another pdf (both pdf are having same content, only difference are with bookmark/without bookmark and one is normal pdf and another linked pdf). Exporting bookmarks to xml is working fine but I don`t have the idea of importing exported bookmark(xml) to another pdf. Can any body suggest solution.

Here I am attaching my code.

        string inputpdf = "D:\\chapter1.pdf"; string outputbookmark="D:\\chapter1Bookmark.xml";
        PdfReader reader = new PdfReader(inputpdf);
        IList<Dictionary<string, object>> bookmarks = SimpleBookmark.GetBookmark(reader);
        using (StreamWriter Sw = new StreamWriter(outputbookmark))
        {
            SimpleBookmark.ExportToXML(bookmarks, Sw,"ISO8859-1", true);

        }
        reader.Close();

My xml output file is

<?xml version="1.0" encoding="ISO8859-1"?>
<Bookmark>
  <Title Color="0 0 0" Page="1 XYZ 36 806" Action="GoTo" >Introduction</Title>
  <Title Color="0 0 0" Page="1 XYZ 36 410" Action="GoTo" >Getting Started
    <Title Color="0 0 0" Page="1 XYZ 36 364" Action="GoTo" >Printing a test page</Title>
    <Title Color="0 0 0" Page="4 XYZ 36 740" Action="GoTo" >Accessing the novaPDF Printing Preferences &#8211; test the multiline bookmark detection option</Title>
    <Title Color="0 0 0" Page="5 XYZ 36 806" Action="GoTo" >Creating PDF Files</Title>
  </Title>
</Bookmark>

My Pdf file available in

http://www.novapdf.com/uploads/novapdf_en/media_items/pdf-example-bookmarks.original.pdf

You are currently using the exportToXml() method (see also exportToXml() ; we currently have the API docs in two different places).

For some reason, you didn't find the importFromXML() method (see also importFromXML() ). If you have an XML file containing bookmarks, for instance:

<?xml version="1.0" encoding="ISO8859-1"?>
<Bookmark>
  <Title Color="0 0 0" Page="1 XYZ 36 806" Action="GoTo" >Introduction</Title>
  <Title Color="0 0 0" Page="1 XYZ 36 410" Action="GoTo" >Getting Started
    <Title Color="0 0 0" Page="1 XYZ 36 364" Action="GoTo" >Printing a test page</Title>
    <Title Color="0 0 0" Page="4 XYZ 36 740" Action="GoTo" >Accessing the novaPDF Printing Preferences &#8211; test the multiline bookmark detection option</Title>
    <Title Color="0 0 0" Page="5 XYZ 36 806" Action="GoTo" >Creating PDF Files</Title>
  </Title>
</Bookmark>

You can read this XML file (as an input stream or using a reader), and the importFromXML() method will return a List<HashMap<String,Object>> object. You can use this object to add the bookmarks to a PDF document using the setOutlines() method. See for instance the BookmarkedTimeTable example. Or take a look at the answer to this question: Merge pdfs and add bookmark with iText in java

These examples are (of course) in Java, but if you need the Java version, scroll down on the page that bundles the examples of chapter 7 of "iText in Action - Second Edition" and you'll find the C# version of those examples. For instance BookmarkedTimeTable.cs

You'll notice that the method setOutlines() doesn't exist in iTextSharp, but that you need to use the property notation:

stamper.Outlines = outlines;

In this case, outlines is an object of type List<Dictionary<string,object>> (C#) instead of ArrayList<HashMap<String, Object>> . It should be fairly simple for a C# developer to port the Java examples to C#, but when in doubt, check the cs files that are made available on the official web site.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM