Message-ID: <1368303024.8509.1711648032238.JavaMail.web05$@web05> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_8508_1026355504.1711648032238" ------=_Part_8508_1026355504.1711648032238 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html Creating a Paragraph

Creating a Paragraph

Table of Contents

=20 =20
To insert a paragraph in a section of your document, use one of the f= ollowing methods. All of the methods insert a parapraph at a specified poin= t in an Element. An Element object represents an editable region in a docum= ent, such as a section or a table cell. The document itself is an Element (= the Document class extends Element). Later we will add a paragraph to a Doc= ument object.

Before adding a paragraph to your file, you must create a content region= in which to insert the paragraph. Each editable region in a Word file is r= epresented by an Element object, or an object that extends Element, such as= a Document object.

=20
WordApplication wordApp =3D new WordApplication();
Document doc =3D wordApp.Create();
=20

To insert an empty paragraph, use one of the methods listed at the begin= ning of this section. All of the methods take a style argument, which speci= fies a named = style to apply to the inserted paragraph. The style argument may be nul= l. If it is null, the Normal style will be applied to the paragraph. The No= rmal style is the default style used by Microsoft Word. The following inser= ts an empty paragraph at the end of the document and applies the BodyText s= tyle to the paragraph:

=20
Paragraph para =3D doc.InsertParagraphAfter(doc.Styles[NamedStyl=
e.BuiltIn.BodyText]);
=20

To add text to the paragraph, call one of the Element class's InsertText= After or InsertTextBefore methods. You can apply a font to the text by passing a Font= object to InsertTextAfter or InsertTextBefore. The following lines create = a Font object that we will use later when adding text to the paragraph:

=20
Font paragraphFont =3D doc.CreateFont();
paragraphFont.FontName =3D "Times New Roman";
paragraphFont.FontSize =3D 10;
=20

The method InsertTextAfter takes a string and inserts it at the end of t= he paragraph. The method's second parameter specifies a Font object (create= d above) to apply to the text.

=20
para.InsertTextAfter("This document was created with Office=
Writer for Word. It
     contains a single, left-justified paragraph.",
     paragraphFont);
=20

The Paragraph class provides numerous formatting properties. The followi= ng left-justifies the paragraph:

=20
//--- If para is a Paragraph object
para.Formatting.TextJustification =3D ParagraphFormatting.Justification.Lef=
t;

//--- If myFormat is a ParagraphFormatting object
myFormat.TextJustification =3D ParagraphFormatting.Justification.Left;
=20

You can access existing paragraphs through the Element.Elements property= , for example:

=20
//--- Open an existing Word file and get the first
//--- paragraph.
WordApplication wordApp =3D new WordApplication();
Document doc =3D wordApp.Open(@"C:\sample.doc");
Paragraph firstParagraph =3D (Paragraph)doc.GetElements(Element.Type.Paragr=
aph)[0];
=20

Paragraph Formatting

To apply a font to text in a paragraph, create a Font object and pass it= to Paragraph.InsertTextAfter or Paragraph.InsertTextBefore, as demonstrate= d above.

Other paragraph formatting can be applied through the Paragraph object o= r the ParagraphFormatting object. To create a ParagraphFormatting object, c= all Document.CreateParagraphFormatting:

=20
WordApplication wordApp =3D new WordApplication();
Document doc =3D wordApp.Create();
ParagraphFormatting bodyTextFormatting =3D doc.CreateParagraphFormatting();
=20

Next, set ParagraphFormatting properties.

=20
//--- Set the amount of whitespace that should be
//--- placed above a paragraph in twips (1/1440 in).
bodyTextFormatting.SpaceBefore =3D 240;

//--- Display line numbers.
bodyTextFormatting.LineNumberingAllowed =3D true;
=20

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.

=20
Paragraph para =3D doc.InsertParagraphAfter(doc.Styles[NamedStyl=
e.BuiltIn.BodyText],
     bodyTextFormatting);
=20
------=_Part_8508_1026355504.1711648032238--