0

i have 4 pdf templates files by using itextsharp i added values and i merged/added 4 pdf files into single document, so all 4 pages are under one single pdf file name.Now i want to add bookmark to my pdf file. is there any way to do in C# ?for better understanding ,please refer below images My pdf looks like this image 1 I want to display like image 2

Hi ,this is what i am trying to do, i am not getting any error but still there is no bookmark in my pdf, i want to add bookmark with 4 sections as showed in the image.after merging i want add bookmark to final pdf.

enter code herepublic string MergePDFs()
    {
        string outPutFilePath = @"D:\jeldsbre.pdf";
        string genereatedpdfs = @"D:\genereatedpdfs";

        using (FileStream stream = new FileStream(outPutFilePath, FileMode.Create))
        {
            Document pdfDoc = new Document(PageSize.A4);
            PdfCopy pdf = new PdfCopy(pdfDoc, stream);
            pdf.SetMergeFields();
            pdfDoc.Open();
            var files = Directory.GetFiles(genereatedpdfs);
            Console.WriteLine("Merging files count: " + files.Length);
            int i = 1;
            foreach (string file in files)
            {
                Console.WriteLine(i + ". Adding: " + file);
                pdf.AddDocument(new PdfReader(file));

                i++;
            }
          List<Dictionary<string, object>> bookmarks = new List<Dictionary<string, object>>();
            IList<Dictionary<string, object>> tempBookmarks =  new List<Dictionary<string, object>>();
            SimpleBookmark.ShiftPageNumbers(tempBookmarks, 1, null);
            bookmarks.AddRange(tempBookmarks);
            SimpleBookmark.ShiftPageNumbers(tempBookmarks, 3, null);
           bookmarks.AddRange(tempBookmarks);
           pdf.Outlines = bookmarks;
           if (pdfDoc != null)
                pdfDoc.Close();
            string base64 = GetBase64(outPutFilePath);
            return base64;
        }               

    }
kiransr
  • 153
  • 3
  • 11
  • This question sounds like possible duplicate of [Merge pdfs and add bookmark with iText in java](http://stackoverflow.com/questions/23688308/merge-pdfs-and-add-bookmark-with-itext-in-java) with the only difference that this question is about iTextSharp / C# (but the answer is almost identical as there is hardly a difference between iText and iTextSharp) – Bruno Lowagie Nov 20 '14 at 13:56
  • Thanks...i will follow this and i will update you. – kiransr Nov 20 '14 at 14:03
  • I've also provided the "full-blown" answer (Java and C#), but that answer assumes that the existing documents already have bookmarks. (It's not clear from your question if this is the case.) – Bruno Lowagie Nov 20 '14 at 14:06
  • hi @BrunoLowagie please see my code and give your comments – kiransr Nov 20 '14 at 15:06
  • i am able to merge , but i am not able to add bookmarks – kiransr Nov 20 '14 at 15:07
  • 1
    If you want me to comment on your code right now, you won't like what I'll say. To avoid this, please throw away your code. Go for a walk. As soon as you have cleared your head, read http://stackoverflow.com/questions/23688308/merge-pdfs-and-add-bookmark-with-itext-in-java and start anew. – Bruno Lowagie Nov 20 '14 at 15:11

1 Answers1

0

Assuming that your original PDFs already have bookmarks, then you should concatenate not only the documents (using the PdfCopy class), you should also concatenate the different bookmarks structures of the different files (using the SimpleBookMark class), not forgetting to take into account that you need to shift the page numbers correctly.

This is done in the ConcatenateBookmarks example in chapter 7 of my book:

// Create a list for the bookmarks
ArrayList<HashMap<String, Object>> bookmarks = new ArrayList<HashMap<String, Object>>();
List<HashMap<String, Object>> tmp;
for (int i  = 0; i < src.length; i++) {
    reader = new PdfReader(src[i]);
    // merge the bookmarks
    tmp = SimpleBookmark.getBookmark(reader);
    SimpleBookmark.shiftPageNumbers(tmp, page_offset, null);
    bookmarks.addAll(tmp);
    // add the pages
    n = reader.getNumberOfPages();
    page_offset += n;
    for (int page = 0; page < n; ) {
        copy.addPage(copy.getImportedPage(reader, ++page));
    }
    copy.freeReader(reader);
    reader.close();
}
// Add the merged bookmarks
copy.setOutlines(bookmarks);

For a C# version of this example, please take a look at http://tinyurl.com/itextsharpIIA2C07 for the corresponding iTextSharp example:

// Create a list for the bookmarks
List<Dictionary<String, Object>> bookmarks = 
    new List<Dictionary<String, Object>>();            
for (int i  = 0; i < src.Count; i++) {
    PdfReader reader = new PdfReader(src[i]);
    // merge the bookmarks
    IList<Dictionary<String, Object>> tmp = 
    SimpleBookmark.GetBookmark(reader);
    SimpleBookmark.ShiftPageNumbers(tmp, page_offset, null);
    foreach (var d in tmp) bookmarks.Add(d);
    // add the pages
    int n = reader.NumberOfPages;
    page_offset += n;
    for (int page = 0; page < n; ) {
        copy.AddPage(copy.GetImportedPage(reader, ++page));
    }
}
// Add the merged bookmarks
copy.Outlines = bookmarks;

If the existing documents don't have any bookmarks (or if you don't want to copy any existing documents), then your question is a duplicate of a question I answered half a year ago: Merge pdfs and add bookmark with iText in java

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165