PrintDocument for WinForms | ComponentOne
PrintDocument Library / Word Index
In This Topic
    Word Index
    In This Topic

    PrintDocument lets you automatically generate indexes, which consists of an alphabetized sorted list of letter headings with entries followed by lists of page numbers where the entry occurs.

    An index is represented by an instance of the RenderIndex class, a render object derived from RenderArea. You can add that object to the document as you would any other render object, but such that the index must appear in the document after all occurrences of entries (terms) contained in it.

    A simple one-level index in a document can be created programmatically as shown in the code snippet below:

    C#
    Copy Code
    // Create the common index:
    RenderIndex index = new RenderIndex();
    // Init keywords:
    Dictionary<string, IndexEntry> indexEntries = new Dictionary<string, IndexEntry>();
    // For this sample, we will build an index of all KnownColor names:
    string[] colorNames = Enum.GetNames(typeof(KnownColor));
    foreach (string keyword in colorNames)
        indexEntries.Add(keyword, new IndexEntry(keyword));
    // Add an index entry for each key word in line:
    LineAdded += (lineRo, fileName, line, lineNo) =>
    {
        var words = line.Split(s_delims, StringSplitOptions.RemoveEmptyEntries);
        C1Anchor a = null;
        foreach (string word in words)
        {
            if (indexEntries.ContainsKey(word))
            {
    if (a == null)
    {
        a = new C1Anchor(string.Format("k{0}{1}", fileName.GetHashCode(), lineNo));
        lineRo.Anchors.Add(a);
    }
    indexEntries[word].Occurrences.Add(new IndexEntryOccurrence(new C1LinkTargetAnchor(a.Name)));
            }
        }
    };
    

    As observed from the code snippet above, initially an instance of the RenderIndex class is created and stored in a local variable. As content (render objects) is added to the document, some program logic should identify strings that are to become entries (terms) in the index. Each such string should be already added to the Entries collection of the index object. If this is a new entry, a new IndexEntry object should be created for it, and added to the index. Further, an entry occurrence (IndexEntryOccurrence) should be added to the existing or newly created entry, to point to the location of the occurrence in the document. Usually the location would be identified by the RenderObject that contains it and is being added to the document. Once all the occurrences of the entries have been added to the document, the RenderIndex object can be added to the document's body. When the document is generated, the RenderIndex object produces a hyperlinked index of the entries that have been added to it. The entries are automatically sorted, split into groups corresponding to each letter.