We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Create TOC in an Existing PDF by extracting information from Bookmarks - Text Wrapping issues

Hello Support,
I’m using Aspose licenced version.
I’m trying to collect all bookmarks from my pdf file and make them as TOC(Table of content).
While making them as TOC, Large text was wrapping.
Here I need to give space for wrapped text second line.

Here I am sending my code and actual output and expected output images.

//My Code:pdfBookmarkCode.JPG (68.7 KB)
pdfBookmarkExpectedOutput.jpg (64.8 KB)
pdfBookmarkOutput.JPG (66.2 KB)

Aspose.Pdf.Facades.Bookmarks bookmarks1 = bookmarkEditor.ExtractBookmarks();
foreach (Aspose.Pdf.Facades.Bookmark bookmark1 in bookmarks1)
{
Row row1 = table1.Rows.Add();
row1.Cells.Add();

            TextFragment textFragment1 = new TextFragment();
            TextFragment textFragment2 = new TextFragment();

            string strLevelSeprator = string.Empty;
            for (int i = 1; i < bookmark1.Level; i++)
            {
                strLevelSeprator += "           ";
            }

            textFragment1.TextState.ApplyChangesFrom(new TextState("Times New Roman", 8));
            //textFragment.TextState.Underline = true;
            textFragment1.TextState.ForegroundColor = Color.Blue;
            textFragment1.TextState.StrokingColor = Aspose.Pdf.Color.Blue;
            textFragment1.TextState.DrawTextRectangleBorder = true;
            textFragment1.Text = strLevelSeprator + bookmark1.Title;
            row1.Cells.Add("");
            row1.Cells[0].Paragraphs.Add(textFragment1);

@shivkumars

Would you kindly share your complete code snippet along with sample source PDF document. We will test the scenario in our environment and address it accordingly.

Hello Ali,
Thank you for your support.
Here I’m sending source code and sample pdf.
Please check once.

Thank you.Sample.pdf (312.0 KB)
SamplePDF Code.zip (1.0 KB)

@shivkumars

Instead of adding extra spaces at the start of each TextFragment, you can add left margin to the corresponding table cell in order to obtain better and clean result. Please check following modified code snippet and attached PDF document for your kind reference:

// Create PdfBookmarkEditor
Facades.PdfBookmarkEditor bookmarkEditor = new Facades.PdfBookmarkEditor();
bookmarkEditor.BindPdf(dataDir + "Sample.pdf");

Document doc = new Document(dataDir + "Sample.pdf");
// Get particular page
Page pg = doc.Pages.Insert(1);

Table table1 = new Table();
table1.ColumnWidths = "400 4000";
table1.DefaultColumnWidth = "10000";

TextFragment title = new TextFragment();
title.TextState.ApplyChangesFrom(new TextState("Times New Roman", 14));
title.TextState.FontStyle = FontStyles.Bold;
title.TextState.ForegroundColor = Color.Black;
title.TextState.StrokingColor = Aspose.Pdf.Color.Black;
title.TextState.DrawTextRectangleBorder = true;
title.Position = new Position(220, 800);
title.Text = "TABLE OF CONTENTS";
pg.Paragraphs.Add(title);

// Extract bookmarks
Aspose.Pdf.Facades.Bookmarks bookmarks1 = bookmarkEditor.ExtractBookmarks();
foreach (Aspose.Pdf.Facades.Bookmark bookmark1 in bookmarks1)
{
 Row row1 = table1.Rows.Add();
 row1.Cells.Add();

 TextFragment textFragment1 = new TextFragment();
 TextFragment textFragment2 = new TextFragment();

  int cellMargins = 0;
  for (int i = 1; i < bookmark1.Level; i++)
  {
   cellMargins += 10;
  }
  //Giving title to first column
  textFragment1.TextState.ApplyChangesFrom(new TextState("Times New Roman", 8));
  textFragment1.TextState.ForegroundColor = Color.Blue;
  textFragment1.TextState.StrokingColor = Aspose.Pdf.Color.Blue;
  textFragment1.TextState.DrawTextRectangleBorder = true;
  textFragment1.Text = bookmark1.Title;

  row1.Cells.Add("");
  row1.Cells[0].Margin = new MarginInfo(cellMargins, 0, 0, 0);
  row1.Cells[0].Paragraphs.Add(textFragment1);

  //Giving page number to second column
  textFragment2.TextState.ApplyChangesFrom(new TextState("Times New Roman", 8));
  textFragment2.TextState.ForegroundColor = Color.Blue;
  textFragment2.TextState.StrokingColor = Aspose.Pdf.Color.Blue;
  textFragment2.TextState.DrawTextRectangleBorder = true;
  textFragment2.Text = (bookmark1.PageNumber).ToString();

  row1.Cells.Add("");
  row1.Cells[1].Paragraphs.Add(textFragment2);
}
pg.Paragraphs.Add(table1);
doc.Save(dataDir + "TOC-TOC-Sample.pdf");

TOC-TOC-Sample.pdf (413.4 KB)