Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

Table of Contents
Excerpt

This article covers applying fonts, named styles, paragraph formatting and table formatting.

 

Anchor
font
font

Applying Fonts

...

To define a font and apply it to a paragraph, first create a Font object:

Code Block
c#
c#

WordApplication wwapp = new WordApplication();
Document doc = wwapp.Create();
Font paragraphFont = doc.CreateFont();
paragraphFont.FontName = "Times New Roman";
paragraphFont.FontSize = 10;

Add an empty paragraph to your document:

Code Block
c#
c#

//--- InsertParagraphAfter's style argument may be null.  If it
//--- is null, the Normal style will be applied to the paragraph. The
//--- Normal style is the default style used by Microsoft Word.
Paragraph para = doc.InsertParagraphAfter(null);

To add text to the paragraph call one of the Element class's InsertTextAfter or InsertTextBefore methods. Pass the the text to add and your Font object to the method:

Code Block
c#
c#

//--- The following method takes a string and inserts it
//--- at the end of the paragraph.  The method's second
//--- parameter specifies a Font object to apply to the
//--- text.
para.InsertTextAfter("This document was created with
     OfficeWriter for Word.",
     paragraphFont);

To get a copy of a built-in style's font, use the NamedStyle.Font property.

Code Block
c#
c#

NamedStyle heading1Style = doc.Styles[NamedStyle.BuiltIn.Heading1];
Font heading1Font = heading1Style.Font;

...

To return a document's Styles collection, use the property Document.Styles:

Code Block
c#
c#

WordApplication wwapp = new WordApplication();
Document doc = wwapp.Create();
NamedStyle heading1Style = doc.Styles[NamedStyle.BuiltIn.Heading1];

To apply a style to a paragraph, pass it to InsertParagraphAfter or InsertParagraphBefore when you add the paragraph to your document:

Code Block
c#
c#

Paragraph para =
     doc.InsertParagraphAfter(doc.Styles[NamedStyle.BuiltIn.BodyText]);

...

  • Code Block
    Document.CreateParagraphFormatting

    Returns a ParagraphFormatting object that represents the font used by the document's BodyText style.
    Or

  • Code Block
    NamedStyle.ParagraphFormatting

    Returns a copy of the specified named style's paragraph formatting properties.//--- Return the paragraph formatting of the BodyText style.

    Code Block
    c#
    c#
    
    WordApplication wwapp = new WordApplication();
    Document doc = wwapp.Create();
    ParagraphFormatting bodyTextFormatting = doc.CreateParagraphFormatting();
    
    //--- Return a copy of the paragraph formatting of the BodyText2 Style.
    WordApplication wwapp = new WordApplication();
    Document doc = wwapp.Create();
    ParagraphFormatting bodyText2Format =
         doc.Styles[NamedStyle.BuiltIn.BodyText2].ParagraphFormatting;
    

    To assign the ParagraphFormatting object that you created to a paragraph, pass it to Element.InsertParagraphAfter or Element.InsertParagraphBefore when you create a new paragraph.

    Code Block
    c#
    c#
    
    Paragraph para = doc.InsertParagraphAfter(doc.Styles[NamedStyle.BuiltIn.BodyText],
         bodyTextFormatting);
    

...

Other paragraph formatting can be applied through the Table object or the TableFormatting object. To create a TableFormatting object, call Document.CreateTableFormatting:

Code Block
c#
c#

WordApplication wwapp = new WordApplication();
Document doc = wwapp.Create();
TableFormatting tableFormat = doc.CreateTableFormatting();

Next, set TableFormatting properties.

Code Block
c#
c#

tableFormat.DefaultShading.BackgroundColor = Color.Gray;
tableFormat.SetDefaultPadding(TableCell.Location.Top, 720);

To assign the TableFormatting object that you created to a table, pass it to Element.InsertTableAfter or Element.InsertTableBefore when you create a new table.

Code Block
c#
c#

Table tbl = doc.InsertTableAfter(3, 5, tableFormat);