Dots on a Table of Contents

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF CoreSuite for .NET (v7)  /  Dots on a Table of Contents

DynamicPDF CoreSuite for .NET (v7) Forum

 Feb 06 2015 2:23 PM
Hello! I am creating a table of contents using Generator. It looks great and has bookmarks and links that work throughout the document. Because the document is to be printed, I need to print "..." between the text on the table of contents and the page number, e.g.

Title Page ................. 1
Section Header ............. 3
   Page in a Section ....... 8
Section Header 2 ........... 20

I know how to do this with "leading dots" in an HTML page using CSS, but I'm guessing that technique won't work here. Right now I try to guess the number of dots, but this isn't quite right:

Dim lnkLabel As New Label(sectionName, 20, y, 400, 15, Font.TimesRoman, 12)
Dim tocItem As String = " " + currentPageNumber.ToString()
For t As Integer = sectionName.Length To 85
    tocItem = ". " + tocItem
Next
Dim dots As New Label(tocItem, 0, y, 500, 15, Font.TimesRoman, 12, TextAlign.Right)

Is there an automatic way to generate these kinds of dots? Thanks.
 Feb 09 2015 12:59 PM
Posted by a ceTe Software moderator
Hello,

There is no automatic way to generate dots while adding table of contents. Below is the pseudo code that shows how to keep track of the horizontal width and add a label with a dynamic number of dots for each line in the table of contents.

        Dim document As ceTe.DynamicPDF.Document = New Document()
        document.Pages.Add(New Page())

        Dim titles() As String = {"very long name", "very very very loooooooonnnnggggggg name", "short name", "name", "very very long name"}

        Dim i As Int32 = 0
        Dim y As Int32 = 0
        For Each value As String In titles
            'create a label and set the width exactly to what is needed to display it.
            Dim lbl As Label = New Label(value, 0, y, 100, 15, Font.Courier, 12)
            lbl.Width = lbl.Font.GetTextWidth(value, lbl.FontSize)

            'create a label with dots and dynamically add dots based on the horizontal space left.
            Dim dots As String = "."
            Dim dotsWidth = document.Pages(0).Dimensions.Body.Width - lbl.Width - 20
            While dotsWidth >= Font.Courier.GetTextWidth(dots, 12)
                dots = dots + "."
            End While
            Dim lblDots As Label = New Label(dots, lbl.Width, y, dotsWidth, 15, Font.Courier, 12)

            ' add all labels to the page.
            document.Pages(0).Elements.Add(lblDots)
            document.Pages(0).Elements.Add(lbl)
            document.Pages(0).Elements.Add(New Label(i, lbl.Width + dotsWidth, y, 20, 15, Font.Courier, 12)) 'page number label
            i = i + 1
            y = y + 20
        Next
        'save the document
        document.Draw("out.pdf")


Thanks,
ceTe Software Support Team.
 Feb 09 2015 2:38 PM
That works perfectly! Thanks!

All times are US Eastern Standard time. The time now is 2:28 AM.