0

I want to add multiple bookmarks based on barcode code on a pdf. Barcode reading is okey. There is no problem with it. But while trying to add bookmarks i did some research and find below answers.

Bookmark to specific page using iTextSharp 4.1.6

Add Page bookmarks to an existing PDF using iTextSharp using C# code

These codes are pretty useful. But the problem is these codes are just only one bookmark. I cannot add multiple bookmark in a loop. You can find the relevant code i did for multiple bookmarks below

Dictionary<string, object> bm = new Dictionary<string, object>();
List<Dictionary<string, object>> bookmarks = new List<Dictionary<string, object>>();

foreach (var barcode in barcodes)
{

string title = barcode.Text.Substring(11, barcode.Text.Length - 11);
bm.Add("Title", title);
bm.Add("Action", "GoTo");
bm.Add("Page", barcode.Page.ToString() + " XYZ 0 0 0");

}

PdfStamper stamp = new PdfStamper(source, output);

stamp.Outlines = bookmarks;

stamp.Close();

problem is here PdfStamper.Outline (stamp.Outlines) using List<Dictionary<string, object>> collection. But the key value of the Dictionary is "string". So i cannot add the bookmarks to the List because of key value cannot repeat. There is an exception throwing that is "System.ArgumentException: 'An item with the same key has already been added.'"

But i cannot find any other documents for implanting bookmark to the pdf with itextsharp.

I am sure this code works for just bookmark. You can find example below.

Dictionary<string, object> bm = new Dictionary<string, object>();
List<Dictionary<string, object>> bookmarks = new List<Dictionary<string, object>>();

//foreach (var barcode in barcodes)
//{

string title = barcodes[0].Text.Substring(11, barcodes[0].Text.Length - 11);
bm.Add("Title", title);
bm.Add("Action", "GoTo");
bm.Add("Page", barcodes[0].Page.ToString() + " XYZ 0 0 0");

//}

PdfStamper stamp = new PdfStamper(source, output);

stamp.Outlines = bookmarks;

stamp.Close();

I think there is no way with this code for adding multiple bookmarks due to nature of stamp.Outlines. Is there any other way for implementing multiple bookmarks to PDF with using itextsharp or do you know how to correct this code ?

letham
  • 11
  • 4
  • Tried with adding two other codes
    `stamp.JavaScript ="var root = this.bookmarkRoot;root.createChild("+title+", 'this.pageNum = ' + 5, 5);"`
    `stamp.AddJavaScript(title, "var root = this.bookmarkRoot;root.createChild(" + title + ", 'this.pageNum = ' + 5, 5);");
    ` These codes are seemed worked but the problem is these codes are not impanting bookmark but javascipt, so everytime when i open and close the pdf, bookmarks are doubled. I still need help
    – letham Oct 27 '19 at 10:03

1 Answers1

1

So i missed a easy point.

i define the Dictionary<string, object> bm = new Dictionary<string, object>(); in the loop and i missed a code. Working code is below


List<Dictionary<string, object>> bookmarks = new List<Dictionary<string, object>>();

foreach (var barcode in barcodes)
{
Dictionary<string, object> bm = new Dictionary<string, object>();
string title = barcode.Text.Substring(11, barcode.Text.Length - 11);
bm.Add("Title", title);
bm.Add("Action", "GoTo");
bm.Add("Page", barcode.Page.ToString() + " XYZ 0 0 0");
bookmarks.Add(bm);
}

PdfStamper stamp = new PdfStamper(source, output);

stamp.Outlines = bookmarks;

stamp.Close();
letham
  • 11
  • 4