IndexFieldOpts.cs
C# VB
//// This code is part of Document Solutions for Word demos.// Copyright (c) MESCIUS inc. All rights reserved.//using System;using System.Globalization;using GrapeCity.Documents.Word;using GrapeCity.Documents.Word.Fields;using GrapeCity.Documents.Word.Layout;
namespace DsWordWeb.Demos{ // This sample shows how to use the INDEX and XE field options // to add an index to a DOCX Word document. public class IndexFieldOpts { // For this demo, we arbitrarily pick some of the 'lorem ipsum' words to include in the index: private static string[] s_indexWords = { "lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit", "mauris", "id", "volutpat", "tellus", "ullamcorper", "sem", "praesent", "et", "ante", "laoreet", "lobortis", "nunc", "congue", };
public GcWordDocument CreateDocx() { var doc = new GcWordDocument();
// Arbitrary number of sample paragraphs in the document: const int NPARS = 30; // We add some random "lorem ipsum" paragraphs to the document, // and build the index on the arbitrarily chosen subset of words, // including pages where the indexed word appears as the first // word in a paragraph: for (int i = 0; i < NPARS; ++i) { var par = doc.Body.Paragraphs.Add(); var txt = Util.LoremIpsumPar();
var words = txt.Split([' ', ',', '.'], 2); if (words.Length != 2) throw new Exception("Unexpected");
var word = words[0]; var xe = new XeFieldOptions(doc); xe.Entry.Content.Text = word; par.AddComplexField(xe); par.AddRun(txt); } // Add new section for the index: doc.Body.Paragraphs.Last.AddSectionBreak(); var p = doc.Body.Paragraphs.Add(doc.Styles[BuiltInStyleId.Index1]); // Set up INDEX field options: var index = new IndexFieldOptions(doc); // Two column layout: index.Columns = 2; // Use headings for index entries: index.Heading = "A"; // Set any additional options as needed, // then create the INDEX field with the specified options: ComplexField field = p.AddComplexField(index);
// Update the index field using a specific culture: field.Update(new WordLayoutSettings() { FontCollection = Util.FontCollection, Culture = CultureInfo.GetCultureInfo("en-US") });
// Done: return doc; } }}