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 - Connect title and page number with dots

Hello Support,
I’m using Aspose licence version.
Here I am adding title and page no in first page of my pdf file using “row.Cells[0].Paragraphs.Add(title)” and “row.Cells[1].Paragraphs.Add(PageNo)”

I need to connect my title and page no with dotted lines.
I’m requesting you to solve my issue asap.

Here I am adding my sample code and Images.

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;
            int c = textFragment1.Text.Count();

            if (c < 88)
            {
                for (int j = c; j <= 88; j++)
                {
                    textFragment1.Text = textFragment1.Text + "-";
                }
            }

            row1.Cells.Add("");
            row1.Cells[0].Paragraphs.Add(textFragment1);

            textFragment2.TextState.ApplyChangesFrom(new TextState("Times New Roman", 8));
            //textFragment.TextState.Underline = true;
            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);<a class="attachment" href="/uploads/discourse_instance3/42146">pdf page No Conn.JPG</a> (34.7 KB)

pdf title and page no.JPG (53.8 KB)pdf page No Conn.JPG (34.7 KB)
pdf title and page no.JPG (53.8 KB)

@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 response.
Here I’m sending sample pdf document and source code.

Thank You.
Sample PDF Code.zip (1.2 KB)
Sample.pdf (312.0 KB)

@shivkumars

You can add a table cell between title and page number and fill it with dots as in following code snippet:

// 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 = "60% 30% 10%";
table1.DefaultColumnWidth = "100%";

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

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

  int cellMargins = 0;
  string strLevelSeprator = "............................................";
  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);

  row1.Cells.Add(strLevelSeprator);

  //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[2].Paragraphs.Add(textFragment2);
}
pg.Paragraphs.Add(table1);
doc.Save(dataDir + "TOC-TOC-Sample.pdf");

TOC-TOC-Sample.pdf (413.8 KB)

Hello Ali,

Thank you for your code.

Unfortunately the code you sent is not producing the expected output.
Here I’m sending expected output file. Please check once.

Thank you.

Sample PDF (Dotted lines).pdf (427.0 KB)

@shivkumars

You are currently adding table in an existing PDF document to make it look like TOC and with this approach, we are afraid that expected PDF output will be quite difficult to achieve. The PDF you have shared recently has proper TOC. We suggest you to try to add TOC in existing PDF document instead of table. In case you face any issue, please feel free to let us know.