0

I have found the number of bookmarks in the pdf file using the following code.

var reader = new PdfReader(System.Windows.Forms.Application.StartupPath + "\\zoom.pdf", new System.Text.ASCIIEncoding().GetBytes(""));
IList<Dictionary<string, object>> bookmarks = SimpleBookmark.GetBookmark(reader);
foreach (Dictionary<string, object> bk in bookmarks)
{
    string bjj = bk.Values.ToArray().GetValue(0).ToString();
}

But i need to set the inherit zoom action property for book bookmark in the pdf file.

please advise me to do from above code or any other sample code in C#

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
mail2vguna
  • 25
  • 2
  • 9

1 Answers1

1

This question is a variation on the following questions by the same OP:

The previous versions of this were very unclear. Based on the comments on the previous questions, the OP wants to take the Outline tree using the SimpleBookmark class, change the Destinations similar to the way he changed the destinations of annotations (as shown in C# - Set inherit zoom action for all the bookmarks in the PDF file) and then persist these changes.

When using SimpleBookmark, you get the outlines in a form that is similar to this XML-file:

<Bookmark>
    <Title Action="GoTo" Style="bold" Page="1 FitH 572">Akira</Title>
<Bookmark>

You can change this XML file (or in the case of the OP the contents of the Dictionary<string, object> obtained from SimpleBookmark), for instance, you could change 1 Fith 572 into 1 Fith 580. This is outside the scope of iText: it's a matter of changing XML or changing Strings in a Dictionary.

Once you've made the change, you need to persist the change in the PDF. For instance, you have a bookmarks object:

List<Dictionary<String, Object>> bookmarks;

and all the changes are reflected in this bookmarks object.

Now you can use PdfStamper's setOutline() method (Java) to change the outlines of the document. In C#, it would be something like this:

stamper.Outlines = bookmarks;

This is only one way to approach it. Obviously, it's also possible to walk through the outline objects and change the destinations at the lowest PDF level.

If you want an example, see the ChangeBookmarks sample:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    List<HashMap<String, Object>> list = SimpleBookmark.getBookmark(reader);
    changeList(list);
    stamper.setOutlines(list);
    stamper.close();
    reader.close();
}

public void changeList(List<HashMap<String, Object>> list) {
    for (HashMap<String, Object> entry : list) {
        for (String key : entry.keySet()) {
            if ("Kids".equals(key)) {
                Object o = entry.get(key);
                changeList((List<HashMap<String, Object>>)o);
            }
            else if ("Page".equals(key)) {
                String dest = (String)entry.get(key);
                entry.put("Page", dest.replaceAll("Fit", "FitV 60"));
            }
        }
    }
}

As already explained, you get all the bookmarks as a List<HashMap<String, Object>>. You write a recursive method that goes through the outline tree ("Kids") and changes all the values of the Page entry. In my case, the bookmarks in bookmarks.pdf are all of type "Fit". I replace "Fit" with "FitV 60". Take a look at changed_bookmarks.pdf to see the difference:

enter image description here

Suppose that you want an XYZ destination where you inherit the zoom factor, you can take do something that is similar to what you did in a previous question you posted: How to set zoom level to pdf using iTextSharp?

Instead of replacing "Fit" with "FitV 60", you can replace "Fit" by "XYZ 30 100 0". In this case, 30 and 100 are X,Y coordinates. The third number is the zoom level, but if you choose 0, you get "Inherit zoom".

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Hi, i can't post full code, its says too long how can i show it? Thanks. – mail2vguna Jun 14 '14 at 09:37
  • Why would you post the full code? I just gave you the answer to your question! – Bruno Lowagie Jun 14 '14 at 10:26
  • Sorry to ask you. i can able to get values from bookmarks object but i don't how to set the value for bookmarks object.kindly help me.Thanks. – mail2vguna Jun 14 '14 at 10:41
  • I'm a Java developer and you're asking me how to change the value in a C# Dictionary or Array? That's... very strange. It feels as if you're pulling my leg. – Bruno Lowagie Jun 14 '14 at 12:21
  • Sorry bruno. I will try and get back to you if i get any error. – mail2vguna Jun 14 '14 at 13:02
  • I have written a Proof of Concept (POC) in Java. If you're a C# developer, you shouldn't have any problem to convert it to .NET. Note that the code of this POC fits perfectly in an answer. If you have a problem, **NEVER** paste your complete code, **ALWAYS** create a small POC that demonstrates the problem, aka a SSCCE (see http://sscce.org ) – Bruno Lowagie Jun 14 '14 at 13:08
  • i have converted your java code to C# and compile successfully but while running i did not get Fit to FitV 60(Right Click bookmark->Properties->Action Tab->Zoom Level - No change.I checked your pdf file(changed_bookmarks.pdf) that also no change in Zoom Level.If anything wrong just correct me.Thanks. – mail2vguna Jun 14 '14 at 18:04
  • I've attached a screen shot that shows that the `/Dest` array was changed as expected. – Bruno Lowagie Jun 15 '14 at 04:55
  • Hi Bruno, Can i get like this [http://stackoverflow.com/questions/22754774/itext-change-bookmarks-zoom-level-to-inherit-zoom-in-existing-pdf]. Thanks. – mail2vguna Jun 15 '14 at 15:29
  • I'll update my answer and I'll add an answer to the question you're referring to. – Bruno Lowagie Jun 16 '14 at 06:35
  • Hi Bruno, as per butani vijay said , your code is not working for me. its still custom or actual size(zoom level).If you want see my code i will psot it. Thanks. – mail2vguna Jun 16 '14 at 09:48
  • I want to see your PDF. – Bruno Lowagie Jun 16 '14 at 14:14
  • How can i attach a file?since i am new to this – mail2vguna Jun 16 '14 at 14:42
  • I solved the problem. made some changes in the code. used dest = "1 XYZ 30 100 0.0"; entry["Page"]=dest; instead of entry["Page"] = dest.Replace("FitR", "XYZ 30 100 0"); now inherit zoom set for all the bookmarks. – mail2vguna Jun 16 '14 at 16:49
  • OK, now all you have to do, is to accept the answer. That is: if you want people to keep answering questions. – Bruno Lowagie Jun 16 '14 at 20:38
  • By the way: your *solution* is wrong. You now have an entry with way too many parameters. You should do this. `entry["Page"] = "XYZ 30 100 0"`. If you replace `FitR` then you have the X Y Zoom parameters you add *as well as* the original parameters that were present after `FitR`. – Bruno Lowagie Jun 17 '14 at 04:24
  • Hello mail2vguna. if i use like entry.put("Page", dest.replaceAll("FitR", "XYZ 30 100 0"));. it set inherit zoom but when i click on bookmark unable to reach at desired location. – Butani Vijay Jun 17 '14 at 04:24
  • That's because 30 100 represent a coordinate of a location that is *chosen arbitrarily*. The values you need, will depend on the page size. Isn't that obvious? – Bruno Lowagie Jun 17 '14 at 04:27
  • What are replacement value for 30 and 100, its depend on page size mean shall i give the page size value?Thanks. – mail2vguna Jun 17 '14 at 10:58
  • I am using following code to reach at desired location in the bookmark. replace dest = "1 XYZ 30 100 0.0"; using dest = "1 XYZ 0 792 0.0"; – mail2vguna Jun 20 '14 at 10:54
  • First you can read bookmark...parse x and y if present and use it... i have solved this problem with the help of Bruno – Butani Vijay Jun 20 '14 at 11:11
  • Hi Vijay, I agreed. How can i get X and Y value from PDF? help me.Thanks. – mail2vguna Jul 06 '14 at 19:14
  • Hi Bruno, I am getting value 1 XYZ -28 795 1.0 then my value for setting inherit zoom is dest="XYZ -28 1.0 0.0" am i correct or wrong? Thanks. – mail2vguna Jul 07 '14 at 17:18
  • No, the value should be XYZ -28 795 0 (you were changing the Y coordinate and the Z-factor; you only need to change the Z-factor). – Bruno Lowagie Jul 08 '14 at 06:16
  • Thanks bruno. i will check and get back to you. – mail2vguna Jul 08 '14 at 08:59
  • Bruno, One of the pdf file, i am getting dest = "2 Fit" from dest = (string)entry[key]; in this case how can i set the value for inherit zoom. Thanks. – mail2vguna Jul 08 '14 at 10:05
  • Fit doesn't require parameters. It just makes sure the page fits the viewer window. Use your common sense to find an X and Y value and change the Fit into XYZ with three values (of which the Z-factor is 0). – Bruno Lowagie Jul 08 '14 at 12:03