Document Solutions for Word
Features / Helper Methods for Adding Content
In This Topic
    Helper Methods for Adding Content
    In This Topic

    DsWord provides various Add methods in the classes to add different content elements directly, thus making the code shorter, clearer, and the content elements easily accessible. The following table lists the various Add methods added to the classes:

    Class Method Description
    Body AddTable Adds a Table to the end of the body.
    AddParagraph Adds a Paragraph to the end of the body.
    AddContentControl Adds a ContentControl to the end of the body.
    CanAdd Gets whether a ContentObject type can be added to the end of the body.
    CanAddContentControl Gets whether a ContentControl with a specified type can be added to the end of the body.
    Section AddTable Adds a Table to the end of the section.
    AddParagraph Adds a Paragraph to the end of the section.
    AddContentControl Adds a ContentControl to the end of the section.
    CanAdd Gets whether a ContentObject type can be added to the end of the section.
    CanAddContentControl Gets whether a ContentControl with a specified type can be added to the end of the section.
    Table AddContentControl Adds a ContentControl to the end of the table.
    Row AddContentControl Adds a ContentControl to the end of the row.
    Cell AddTable Adds a Table to the end of the cell.
    AddParagraph Adds a Paragraph to the end of the cell.
    AddContentControl Adds a ContentControl to the end of the cell.
    Paragraph AddSimpleField  Adds a SimpleField to the end of the paragraph.
    AddHyperlink Adds a Hyperlink to the end of the paragraph.
    AddBidirectionalOverride Adds a BidirectionalOverride to the end of the paragraph.
    AddOMathParagraph Adds an OMathParagraph to the end of the paragraph.
    AddOMath Adds an OMath to the end of the paragraph.
    AddRun Adds a Run to the end of the paragraph.
    AddFootnote Adds a Footnote to the end of the paragraph.
    AddEndnote Adds an Endnote to the end of the paragraph.
    AddComplexField Adds a ComplexField to the end of the paragraph.
    AddContentControl Adds a ContentControl to the end of the paragraph.
    AddSectionBreak Breaks the parent section right after this paragraph.
    SimpleField AddSimpleField  Adds a SimpleField to the end of the simple field.
    AddHyperlink Adds a Hyperlink to the end of the simple field.
    AddBidirectionalOverride Adds a BidirectionalOverride to the end of the simple field.
    AddOMathParagraph Adds an OMathParagraph to the end of the simple field.
    AddOMath Adds an OMath to the end of the simple field.
    AddRun Adds a Run to the end of the simple field.
    AddFootnote Adds a Footnote to the end of the simple field.
    AddEndnote Adds an Endnote to the end of the simple field.
    AddComplexField Adds a ComplexField to the end of the simple field.
    AddContentControl Adds a ContentControl to the end of the simple field.
    Hyperlink AddSimpleField  Adds a SimpleField to the end of the hyperlink.
    AddHyperlink Adds a Hyperlink to the end of the hyperlink.
    AddBidirectionalOverride Adds a BidirectionalOverride to the end of the hyperlink.
    AddOMathParagraph Adds an OMathParagraph to the end of the hyperlink.
    AddOMath Adds an OMath to the end of the hyperlink.
    AddRun Adds a Run to the end of the hyperlink.
    AddFootnote Adds a Footnote to the end of the hyperlink.
    AddEndnote Adds an Endnote to the end of the hyperlink.
    AddComplexField Adds a ComplexField to the end of the hyperlink.
    AddContentControl Adds a ContentControl to the end of the hyperlink.
    BidirectionalOverride AddSimpleField  Adds a SimpleField to the end of the bidirectional override.
    AddHyperlink Adds a Hyperlink to the end of the bidirectional override.
    AddBidirectionalOverride Adds a BidirectionalOverride to the end of the bidirectional override.
    AddOMathParagraph Adds an OMathParagraph to the end of the bidirectional override.
    AddOMath Adds an OMath to the end of the bidirectional override.
    AddRun Adds a Run to the end of the bidirectional override.
    AddFootnote Adds a Footnote to the end of the bidirectional override.
    AddEndnote Adds an Endnote to the end of the bidirectional override.
    AddComplexField Adds a ComplexField to the end of the bidirectional override.
    AddContentControl Adds a ContentControl to the end of the bidirectional override.
    OMathParagraph AddOMath  Adds an OMath to the end of the Office Math paragraph.
    AddRun Adds a Run to the end of the Office Math paragraph.
    AddFootnote Adds a Footnote to the end of the Office Math paragraph.
    AddEndnote Adds an Endnote to the end of the Office Math paragraph.
    AddComplexField Adds a ComplexField to the end of the Office Math paragraph.
    OMath AddSimpleField Adds a SimpleField to the end of the Office Math zone.
    AddHyperlink Adds a Hyperlink to the end of the Office Math zone.
    AddRun Adds a Run to the end of the Office Math zone.
    AddFootnote Adds a Footnote to the end of the Office Math zone.
    AddEndnote Adds an Endnote to the end of the Office Math zone.
    AddComplexField Adds a ComplexField to the end of the Office Math zone.
    AddOMathStruct Adds a new OMathStruct to the end of the Office Math zone.
    AddContentControl Adds a ContentControl to the end of the Office Math zone.
    AddAccent Adds a new OMathAccent to the end of the Office Math zone.
    AddBar Adds a new OMathBar to the end of the Office Math zone.
    AddBorderBox Adds a new OMathBorderBox to the end of the Office Math zone.
    AddBox Adds a new OMathBox to the end of the Office Math zone.
    AddDelimiter Adds a new OMathDelimiter to the end of the Office Math zone.
    AddEquationArray Adds a new OMathEquationArray to the end of the Office Math zone.
    AddFraction Adds a new OMathFraction to the end of the Office Math zone.
    AddFunction Adds a new OMathFunction to the end of the Office Math zone.
    AddGroupCharacter Adds a new OMathGroupCharacter to the end of the Office Math zone.
    AddLimitLower Adds a new OMathLimitLower to the end of the Office Math zone.
    AddLimitUpper Adds a new OMathLimitUpper to the end of the Office Math zone.
    AddMatrix Adds a new OMathMatrix to the end of the Office Math zone.
    AddNary Adds a new OMathNary to the end of the Office Math zone.
    AddOMathStruct Adds a new OMathStruct to the end of the Office Math zone.
    AddPhantom Adds a new OMathPhantom to the end of the Office Math zone.
    AddPreSubSuperscript Adds a new OMathPreSubSuperscript to the end of the Office Math zone.
    AddRadical Adds a new OMathRadical to the end of the Office Math zone.
    AddSubscript Adds a new OMathSubscript to the end of the Office Math zone.
    AddSubSuperscript Adds a new OMathSubSuperscript to the end of the Office Math zone.
    AddSuperscript Adds a new OMathSuperscript to the end of the Office Math zone.
    OMathElement AddSimpleField Adds a SimpleField to the end of the Office Math element.
    AddHyperlink Adds a Hyperlink to the end of the Office Math element.
    AddRun Adds a Run to the end of the Office Math element.
    AddFootnote Adds a Footnote to the end of the Office Math element.
    AddEndnote Adds an Endnote to the end of the Office Math element.
    AddComplexField Adds a ComplexField to the end of the Office Math element.
    AddOMathStruct Adds a new OMathStruct to the end of the Office Math element.
    AddContentControl Adds a ContentControl to the end of the Office Math element.
    AddAccent Adds a new OMathAccent to the end of the Office Math element.
    AddBar Adds a new OMathBar to the end of the Office Math element.
    AddBorderBox Adds a new OMathBorderBox to the end of the Office Math element.
    AddBox Adds a new OMathBox to the end of the Office Math element.
    AddDelimiter Adds a new OMathDelimiter to the end of the Office Math element.
    AddEquationArray Adds a new OMathEquationArray to the end of the Office Math element.
    AddFraction Adds a new OMathFraction to the end of the Office Math element.
    AddFunction Adds a new OMathFunction to the end of the Office Math element.
    AddGroupCharacter Adds a new OMathGroupCharacter to the end of the Office Math element.
    AddLimitLower Adds a new OMathLimitLower to the end of the Office Math element.
    AddLimitUpper Adds a new OMathLimitUpper to the end of the Office Math element.
    AddMatrix Adds a new OMathMatrix to the end of the Office Math element.
    AddNary Adds a new OMathNary to the end of the Office Math element.
    AddOMathStruct Adds a new OMathStruct to the end of the Office Math element.
    AddPhantom Adds a new OMathPhantom to the end of the Office Math element.
    AddPreSubSuperscript Adds a new OMathPreSubSuperscript to the end of the Office Math element.
    AddRadical Adds a new OMathRadical to the end of the Office Math element.
    AddSubscript Adds a new OMathSubscript to the end of the Office Math element.
    AddSubSuperscript Adds a new OMathSubSuperscript to the end of the Office Math element.
    AddSuperscript Adds a new OMathSuperscript to the end of the Office Math element.
    Run AddGroupShape Adds a GroupShape to the end of the run.
    AddCanvasShape Adds a CanvasShape to the end of the run.
    AddShape Adds a new GeometryType.Rectangle 100 x 100 points Shape to the end of the run.
    AddPicture Adds a new Picture to the end of the run.
    AddInkShape Adds an InkShape to the end of the run.
    AddText Adds a Text to the end of the run.
    AddTab Adds a Tab to the end of the run.
    AddBreak Adds a Break to the end of the run.
    AddSymbol Adds a Symbol to the end of the run.
    GroupShape AddGroupShape Adds a GroupShape to the end of the group shape.
    AddShape Adds a new GeometryType.Rectangle 100 x 100 points Shape to the end of the group shape.
    AddPicture Adds a new Picture to the end of the group shape.
    AddInkShape Adds an InkShape to the end of the group shape.
    CanvasShape AddGroupShape Adds a GroupShape to the end of the canvas shape.
    AddShape Adds a new GeometryType.Rectangle 100 x 100 points Shape to the end of the canvas shape.
    AddPicture Adds a new Picture to the end of the canvas shape.
    AddInkShape Adds an InkShape to the end of the canvas shape.
    TextFrame AddTable Adds a Table to the end of the text frame.
    AddParagraph Adds a Paragraph to the end of the text frame.
    AddContentControl Adds a ContentControl to the end of the text frame.
    ControlContent AddTable Adds a Table to the end of the control content.
    AddParagraph Adds a Paragraph to the end of the control content.
    AddSimpleField Adds a SimpleField to the end of the control content.
    AddHyperlink Adds a Hyperlink to the end of the control content.
    AddBidirectionalOverride Adds a BidirectionalOverride to the end of the control content.
    AddOMathParagraph Adds an OMathParagraph to the end of the control content.
    AddOMath Adds an OMath to the end of the control content.
    AddRun Adds a Run to the end of the control content.
    AddFootnote Adds a Footnote to the end of the control content.
    AddEndnote Adds an Endnote to the end of the control content.
    AddComplexField Adds a ComplexField to the end of the control content.
    AddContentControl Adds a ContentControl to the end of the control content.
    ContentObject CanAdd Gets whether a ContentObject type can be added to this content object.
    CanAddContentControl Gets whether a ContentControl type can be added to this content object.

    Refer to the following example code to add run and hyperlink directly to the paragraph:

    C#
    Copy Code
    // Initialize GcWordDocument.
    GcWordDocument doc = new GcWordDocument();
    
    // Add paragraph with default formatted text.
    var p = doc.Body.AddParagraph("AddRun and AddHyperlink methods will add the run and hyperlink directly to the paragraph. ");
    
    // Add another text and a hyperlink into the paragraph formatted with "Heading1" style.
    /* Earlier, the code would have looked like this:
       p.GetRange().Runs.Add("Follow this link to open Google. ", doc.Styles[BuiltInStyleId.Heading1]);
       Hyperlink link1 = p.GetRange().Hyperlinks.Add(new Uri("http://www.google.com"),"", "Click here.");
       Now the code is shorter and more clear about what content can be added to the object. */
    p.AddRun("Follow this link to open Google. ", doc.Styles[BuiltInStyleId.Heading1]);
    p.AddHyperlink(new Uri("http://www.google.com"),"", "Click here.");
    
    // Save the Word document.
    doc.Save("HelperMethods.docx", DocumentType.Document);

    Refer to the following example code to check if a paragraph can be added after content control using CanAdd method:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var doc = new GcWordDocument();
    
    // Add a paragraph.
    var p = doc.Body.Paragraphs.Add();
    
    // Add content control.
    var cc = p.AddContentControl(ContentControlType.RichText).Content;
    
    // Check if a paragraph can be added or not and add a paragraph; otherwise, add a run.
    if (cc.CanAdd(typeof(Paragraph))) // Returns false since the content control was created on inline level.
        cc.AddParagraph("rich text");
    else
        cc.AddRun("rich text");
    
    // Save the document.
    doc.Save("CanAdd.docx");