Search code examples
pdf-generationitextbookmarksacrobat-sdk

Best free alternative to Acrobat SDK in order to manipulate PDF document in c# .net


I need to add bookmarks to pdf documet without using Acrobat SDK (that needs the full Acrobat professional installed). I'm using iTextSharp but is was made for Java and the porting to .net it is not complete. Do you know an free alternative or documentation to do it?


Solution

  • You're claim that "iTextSharp but is was made for Java and the porting to .net it is not complete" is quite mistaken. The main differences are listed here.

    Adding bookmarks with iTextsharp is simple. See the API for PdfOutline and PdfDestination. Here is a simple example to get you started:

    using (Document document = new Document()) {
      PdfWriter writer = PdfWriter.GetInstance(
        document, Response.OutputStream
      );
      document.Open();
      PdfOutline root = writer.RootOutline;
      string section = "Section {0}";
      string paragraph = "Paragraph {0}";
      for (int i = 0; i < 10;) {
        PdfOutline sectionBookmark = new PdfOutline(
          root, 
          new PdfDestination(
            PdfDestination.FITH, writer.GetVerticalPosition(true)
          ),
          string.Format(section, ++i)
        );
        document.Add(new Paragraph(string.Format(section, i)));
        for (int j = 0; j < 4;) {
          PdfOutline subSectionBookmark = new PdfOutline(
            sectionBookmark,
            new PdfDestination(
              PdfDestination.FITH, writer.GetVerticalPosition(true)
            ),
            string.Format(paragraph, ++j)
          );
          document.Add(new Paragraph(string.Format(paragraph, j)));
        }
        document.NewPage();
      }
    }
    

    Above example tested in a web environment with 5.1.3. If your devlopment environment is different, replace Response.OutputStream above with the Stream of your choice.