Tutorials > VSPrinter Examples > Build an Index |
This example shows how you can build an index for a VSPrinter document based on a list of words loaded from a text file. The example is based on the FindText property.
To create the Indexexample, start by adding the following controls to a new VB form:
VSPrinter control (name it vp)
CommandButton control (name it btnDoIt).
Next, you will need two external files:
An RTF file that will be loaded into the VSPrintercontrol and indexed. Copy this file to the sample's directory and name it text.rtf.
A plain text file containing a list of words to be indexed. The file should have one word (or expression) per line. Copy this file to the sample's directory and name it words.txt.
Add the following code to the btnDoIt_Click event handler:
Example Title |
Copy Code
|
---|---|
Private Sub btnDoIt_Click() ' user interface btnDoIt.Enabled = False vp.NavBarText = "Loading..." ' set up to create document vp.Footer = "TEXT.RTF||Page %d" vp.PageBorder = pbColBottom vp.Styles.Add "Scratch", vpsAll ' load RTF file into document vp.StartDoc vp.PrintFile App.Path & "\text.rtf" ' start building index vp.NewPage ' start index on a new page vp.FontBold = True ' print title vp.FontSize = 18 vp = "Index" vp.Columns = 2 ' index body on two columns vp.FontName = "Times New Roman" vp.FontBold = False ' index body font vp.FontSize = 9 vp.MarginTop = vp.CurrentY vp.IndentLeft = "1in" ' hanging indent vp.IndentFirst = -vp.IndentLeft vp.IndentTab = 200 ' build index vp.Tag = vp.PageCount Open App.Path & "\words.txt" For Input As #1 Dim sWord$ While Not EOF(1) Line Input #1, sWord AddToIndex vp, sWord Wend Close #1 ' done vp.EndDoc vp.NavBarText = "" btnDoIt.Enabled = True vp.Styles.Apply "Scratch" ' show index page vp.PreviewPage = vp.Tag End Sub |
This routine loads the RTF file into the VSPrinter control, then opens the file containing the words to be indexed and calls the AddToIndex routine for each word. The AddToIndex routine is the one that does the interesting part of the work, and here it is:
Example Title |
Copy Code
|
---|---|
Sub AddToIndex(vp As VSPrinter, sWord$) ' user feedback vp.NavBarText = "Indexing " & sWord DoEvents ' look for word in document, build list of pages where it was found Dim pg%, sPages$ Do pg = vp.FindText(sWord, , pg + 1) If pg <= 0 Then Exit Do If Len(sPages) > 0 Then sPages = sPages & ", " sPages = sPages & pg Loop ' if not found, no entry If Len(sPages) = 0 Then Exit Sub ' compress page list ranges sPages = CompressPageRange(sPages) ' add word and page list to index vp.FontItalic = True vp.Text = sWord & vbTab vp.FontItalic = False vp.Paragraph = sPages End Sub |
The AddToIndex routine uses the FindText property to build a list of pages where the word (or expression) was found in the document, compresses the page list to make it easier to read, then writes the index entry into the main document.
The CompressPageRange routine is optional. It changes lists such as "1, 2, 3, 4, 5, 10, 11, 12, 13" into "1-5, 10-13", which is easier to read and makes the index look less cluttered. Here is the code:
Example Title |
Copy Code
|
---|---|
Function CompressPageRange(s$) As String Dim v, i%, iLast%, iRun%, sRet$ ' split page list into vector and initialize list v = Split(s, ", ") iLast = v(0) sRet = iLast ' process vector ' (iRun keeps a count of consecutive pages) For i = 1 To UBound(v) If Val(v(i)) = iLast + 1 Then iRun = iRun + 1 Else If iRun = 1 Then sRet = sRet & ", " & iLast If iRun > 1 Then sRet = sRet & "-" & iLast sRet = sRet & ", " & v(i) iRun = 0 End If iLast = v(i) Next ' don't forget the end of the last run If iRun = 1 Then sRet = sRet & ", " & iLast If iRun > 1 Then sRet = sRet & "-" & iLast ' done CompressPageRange = sRet End Function |
That's it. The following image shows what a typical index looks like: