//
// 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;
namespace DsWordWeb.Demos
{
// This sample shows how to use the REF field options to add
// and customize a reference to a bookmarked paragraph elsewhere
// in the document, either as the paragraph's own list number or
// as a copy of its text.
public class RefFieldOpts
{
public GcWordDocument CreateDocx()
{
var doc = new GcWordDocument();
doc.Body.AddParagraph("A numbered list of requirements:");
// A ListTemplate is used to make paragraphs part of a numbered list:
var listTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.NumberDefault, "reqList");
// The name of the target bookmark:
const string bmkName = "reqBookmark";
Paragraph bookmarkedItem = null;
for (int i = 1; i <= 5; i++)
{
var p = doc.Body.AddParagraph($"Requirement {i}: the system shall do thing #{i}.");
p.ListFormat.Template = listTemplate;
p.ListFormat.LevelNumber = 0;
// This ensures item spacing consistent with MS Word:
p.Style = doc.Styles[BuiltInStyleId.ListParagraph];
if (i == 3)
bookmarkedItem = p;
}
// Bookmark the 3rd requirement so it can be referenced elsewhere:
bookmarkedItem.GetRange().Bookmarks.Add(bmkName);
// The RefFieldOptions class provides a convenient strong-typed access
// to options specific to the 'REF' MS Word field. This one pulls in
// the bookmarked item's own list number:
var refByNumber = new RefFieldOptions(doc, bmkName)
{
DisplayListNumber = DisplayListNumber.AllLevels,
Hyperlink = true
};
doc.Body.AddParagraph("As specified in item ").AddComplexField(refByNumber)
.GetRange().Runs.Add(" above, the system shall comply.");
// This REF pulls in the bookmarked item's text instead, upper-cased:
var refByText = new RefFieldOptions(doc, bmkName)
{
StringStyle = StringStyle.Upper
};
doc.Body.AddParagraph("For emphasis: ").AddComplexField(refByText);
// Update fields using a specific culture:
doc.UpdateFields(new GrapeCity.Documents.Word.Layout.WordLayoutSettings() { FontCollection = Util.FontCollection, Culture = CultureInfo.GetCultureInfo("en-US") });
// Done:
return doc;
}
}
}