2

My requirement is same as described in this question: Bookmark to specific page using iTextSharp 4.1.6

I tried the answer, but the last line gives my the following error:

Can not implicitly convert ArrayList to IList<Dictionar(strin,object)>

I am not sure how to correct it.

The line that gave error is, in the Answer 1 of above link

wri.Outlines = bookmarks

I modified the example like this:

List<Dictionary<string, object>> testData = new List<Dictionary<string, object>>(100);
// Just Sample data for understanding.
//for (int i = 0; i < 100; i++)
//{
var test = new Dictionary<string, object>
{
    { "Action", "GoTo"},
    { "Title", "Page1 0 H 0" },
    {"Page", "1 XYZ 0 " + h + " 0" }
};
testData.Add(test);
//}
wri.Outlines = testData;

Now after adding the bookmarks, I'm unable to open the PDF because the file has been corrupted.

Community
  • 1
  • 1
user3122606
  • 57
  • 1
  • 3
  • 11

2 Answers2

1

Please consult the official documentation.

Bookmarks are discussed in Chapter 7 where you'll find the BookmarkedTimeTable example. As you're working with iTextSharp (C#), not iText (Java), you'll want to look up the corresponding example in the list of examples ported to C#, more specifically BookmarkedTimeTable.cs.

Update after you updated the question:

Your error indicates that testdata isn't really of type IList<Dictionar(string,object)>; note that I've added a g to strin assuming that this is a typo in your question, not in your actual code.

Please start with the example from my book, and then change that example gradually until you break that code.

Update based on extra comment:

When a PDF is digitally signed, there is a limited number of operations that may (or may not) be allowed. See Which operations are allowed on a digitally signed PDF? Adding bookmarks is not an allowed operation, so you'll have to remove all signature fields before applying the change. Depending on whether or not you want to keep the empty signature field or completely remove the field, you'll use either the method clearSignatureField() or removeField() passing the name of a signature field as parameter. You can get an ArrayList of names using the getSignatureNames() method (see ÀcroFields).

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thank you for the help.... I modified my code with the help of BookmarkedTimeTable.cs , but could not understand the "dest" file ref, In the comment section it is mentioned that -- Manipulates a PDF file src with the file dest as result but in code i could not find any ref abt dest, correct if I am wrong... – user3122606 Dec 20 '13 at 13:21
  • I managed to solve it with the help of your corresponding Java sample, Thanks a lot.... – user3122606 Dec 20 '13 at 13:37
  • Please help me to resolve this... http://stackoverflow.com/questions/20877294/apply-formatting-to-pdf-bookmarks-using-itextsharp – user3122606 Jan 02 '14 at 06:30
  • My program stopped working when it came to Digitally signed PDF, on googling i found that no changes are allowed after a document is digitally signed, my requirement is to add bookmarks for a digitally signed document, but need not be the on the original, instead a copy would do , how to do that ? Pleas help... – user3122606 Jan 03 '14 at 05:45
  • Can you please give your answer to my question http://stackoverflow.com/questions/21308383/how-to-redact-pdf-using-itextsharp – user3122606 Jan 23 '14 at 12:43
  • I voted to close that question as it's too broad. The question isn't suited for StackOverflow (as documented in the StackOverflow FAQ). Please start by reading http://manning.com/lowagie2/samplechapter6.pdf – Bruno Lowagie Jan 23 '14 at 13:10
  • @user3122606 How you managed, is it possible we can import the bookmark via xml to pdf? – Nachiappan R Jul 08 '16 at 14:38
  • @MacDaddy, Did you tried the Itextsharp method? It really worked for you. I am also looking same thing. – Nachiappan R Jul 08 '16 at 14:39
  • 1
    @NachiappanR You are *hijacking* a question dating from 2013. **Do Not Do That!** If you have a question, for instance, if you have tried what is explained in this post, and if it doesn't work, explain what you have tried *in a new question* and explain the problem you are experiencing. Saying "I am also looking *for the* same thing" in a comment, usually doesn't result in an answer. It only annoys people, especially since you can find all the C# examples at the bottom of [this page](http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-7). – Bruno Lowagie Jul 08 '16 at 15:09
  • Thanks @BrunoLowagie, Here my team already posted the tried one (http://stackoverflow.com/questions/37993783/how-to-import-bookmark-from-xml-to-existing-pdf) but did not get any help that`s why I inquired here. Sorry for the trouble. – Nachiappan R Jul 08 '16 at 17:17
  • 1
    I see the question. It's as if you didn't even read the documentation. That's probably why no one cared to answer that question. You have a whole team and no one did an effort visiting the official iText web site. – Bruno Lowagie Jul 09 '16 at 03:40
0

Try making it an actual ArrayList of Hashtables:

            var outlines = firstComponentPages.Select(x => new Hashtable()
            {
                {"Title", x.Value},
                {"Action", "GoTo"},
                {"Page", $"{x.Key} Fit"}
            }).ToList();


            var outlinesArrayList = new ArrayList();
            foreach (var outline in outlines)
            {
                outlinesArrayList.Add(outline);
            }
            copy.Outlines = outlinesArrayList;
Clint
  • 1
  • 2