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 ?
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();