Merge a list of pdfs and create new bookmarks (C#)

Viewed 2227

The project is in C# and use iTextSharp.

I have a dictionary with a title (string) and file content (byte array). I loop through this dictionary and merge all files together. What I need now is to add bookmarks to the start of the first page in each file, but I should not add any new pages or text to the final document. I have tried different solutions, but all seem to add a table of contents page, a new page before each page or some text at the start of the page.

None of the files have bookmarks originally.

I am looking for a bookmarks structure that looks something like this:

  • File1
  • File2
  • SomeCategory
    • File3
    • File4

I would very much appreciate it if anyone could point me in the right direction.

My function for merging the files looks like this:

/// <summary>
/// Merge PDF files, and stamp certificates. This is a modified version of the example in the link below.
/// See: http://www.codeproject.com/Articles/28283/Simple-NET-PDF-Merger for more information.            
/// </summary>
/// <param name="sourceFiles">Files to be merged</param>
/// <returns>Byte array with the combined files.</returns>
public static byte[] MergeFiles(Dictionary<string, byte[]> sourceFiles)
    {
        var document = new Document();
        var output = new MemoryStream();

        try
        {
            // Initialize pdf writer
            var writer = PdfWriter.GetInstance(document, output);
            writer.PageEvent = new PdfPageEvents();

            // Open document to write
            document.Open();
            var content = writer.DirectContent;

            // Iterate through all pdf documents
            foreach (var sourceFile in sourceFiles)
            {
                // Create pdf reader
                var reader = new PdfReader(sourceFile.Value);
                var numberOfPages = reader.NumberOfPages;

                // Iterate through all pages
                for (var currentPageIndex = 1; currentPageIndex <=
                                   numberOfPages; currentPageIndex++)
                {
                    // Determine page size for the current page
                    document.SetPageSize(
                       reader.GetPageSizeWithRotation(currentPageIndex));

                    // Create page
                    document.NewPage();

                    var importedPage =
                      writer.GetImportedPage(reader, currentPageIndex);

                    // Determine page orientation
                    var pageOrientation = reader.GetPageRotation(currentPageIndex);
                    if ((pageOrientation == 90) || (pageOrientation == 270))
                    {
                        content.AddTemplate(importedPage, 0, -1f, 1f, 0, 0,
                           reader.GetPageSizeWithRotation(currentPageIndex).Height);
                    }
                    else
                    {
                        content.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0);
                    }
                    // Add stamp to certificates
                    if (sourceFile.Key.IsValidDocumentReference())
                        AddStamp(content, document, sourceFile.Key, currentPageIndex, numberOfPages);

                }
            }
        }
        catch (Exception exception)
        {
            throw new Exception("An unexpected exception occured during the merging process", exception);
        }
        finally
        {
            document.Close();
        }
        return output.GetBuffer();
    }
1 Answers
Related