CharacterFormatting.cs
- //
- // This code is part of Document Solutions for Word demos.
- // Copyright (c) MESCIUS inc. All rights reserved.
- //
- using GrapeCity.Documents.Word;
-
- namespace DsWordWeb.Demos
- {
- // This sample demonstrates the basics of working with text content
- // and character formatting in DsWord.
- // It creates a document and adds a paragraph of text to it.
- // It then splits the paragraph into two runs, and assigns different
- // font styles to the two halves of the text.
- // The GetRange() method enables easy pinpointing of locations in the document.
- public class CharacterFormatting
- {
- public GcWordDocument CreateDocx()
- {
- // Get a sample paragraph of text:
- var lorem = Util.LoremIpsumPar();
- // Create a new document:
- GcWordDocument doc = new GcWordDocument();
- // Add the paragraph to the document:
- Paragraph p = doc.Body.Paragraphs.Add(lorem);
- // The primary way of manipulating objects in DsWord is via ranges.
- // Get the range on the paragraph that we've just added:
- Range r = p.GetRange();
- // A Run is a contiguous fragment of a document with uniform formatting.
- // In our case, we have only one run so far, get it:
- Run run = r.Runs.First;
- // Set the size of the font on the whole run:
- run.Font.Size = 16;
- // A Text represents a contiguous fragment of text. It also belongs to a Run,
- // and cannot span multiple runs (a run though can contain several Text's).
- // Get the text object:
- Text text = run.GetRange().Texts.First;
- // Split the text into two halves (Split() returns the 2nd part):
- Text tIpsum = text.Split(lorem.Length / 2);
- // At this point, our run contains two Texts (two halves of the original text).
- // We now split the run into two corresponding runs so that we can
- // apply different formatting to the two texts:
- Run rIpsum = run.Split(tIpsum, InsertLocation.Before);
- // The 'text' was split into two halves, but the first half can still
- // be accessed via the 'text' variable.
- // A text's containing run can always be accessed via ParentRun.
- // We set the font of the first half to italic:
- text.ParentRun.Font.Italic = true;
- // and the font of the second half to bold:
- rIpsum.Font.Bold = true;
- // Done:
- return doc;
- }
- }
- }
-