Document Solutions for Word
Features / Shapes / Textbox
In This Topic
    Textbox
    In This Topic

    While adding text in a document, certain kind of formatting and placement restrictions are faced. These restrictions can be overcome by using a textbox which provides the flexibility to add text anywhere in the document and add certain styles and formatting to it. The textbox is also very helpful for creating a blockquote or a sidebar.

    DsWord allows you to add textbox in a document which is a combination of text rendered on a shape element. A shape can be added by using the Shape class and the text can be rendered over it by using the TextFrame class.  Additionally, DsWord also provides LinkedTextFrame class which provides additional space for textual content if it does not fit into the original text frame. The LinkedTextFrame is only visible when it accommodates any additional content. Each shape can have no more than one TextFrame or LinkedTextFrame.  

    You can also define the formatting of text frame like its orientation, word wrap, margin, auto-fit shape to text etc. using the TextFrameFormat class.

    Textbox in a Word document

    Add Textbox

    To add textbox in a document:

    1. Create a new Word document by instantiating the GcWordDocument class.
    2. Add a paragraph by using the Add method of ParagraphCollection class.
    3. Add a run to the paragraph and shape to the run by using the Add method of RunCollection and ShapeCollection class respectively.
    4. Add a text frame to the shape by using the AddTextFrame method of the Shape class.
    5. Add a linked text frame which will accommodate the extra text, if any, by using the AddLinkedTextFrame method of Shape class.
      C#
      Copy Code
      GcWordDocument doc = new GcWordDocument();
      Paragraph para = doc.Body.Paragraphs.Add();
      Run run = para.GetRange().Runs.Add();
      
      //Add shape 
      Shape shape = run.GetRange().Shapes.Add(100, 100);
      
      //Add a text frame along with text on shape  
      TextFrame tf = shape.AddTextFrame("Document Solutions for Word library is a part of Document Solutions " +
          "that aims to be a complete solution to program and work with Word documents");
      
      //Add linked text frame to hold any extra text
      LinkedTextFrame ltf = doc.Body.Paragraphs.Add().GetRange().Runs.Add().GetRange().Shapes.Add(100, 100).AddLinkedTextFrame(tf);
      
      doc.Save("Textbox.docx");
    Back to Top

    Modify Textbox

    To modify a textbox:

    1. Create a new Word document by instantiating the GcWordDocument class.
    2. Load the document containing textbox using Load method of GcWordDocument class.
    3. Access the first text frame in the document and set its formatting properties using the Format property of TextFrame class.
      C#
      Copy Code
      GcWordDocument doc = new GcWordDocument();
      doc.Load("Textbox.docx");
      
      //Set formatting properties for Textbox
      doc.Body.TextFrames[1].Format.Orientation = TextOrientation.Vertical;
      doc.Body.TextFrames[1].Format.WordWrap = true;
      doc.Body.TextFrames[1].Format.VerticalAnchor = TextVerticalAnchor.Bottom;
      
      doc.Save("Textbox.docx");

    Back to Top

    Delete Textbox

    To delete a textbox:

    1. Load the document containing textbox using Load method of GcWordDocument class.
    2. Delete the textbox shape by using the Delete method of ContentObject class.
      C#
      Copy Code
      GcWordDocument doc = new GcWordDocument();
      doc.Load("Textbox.docx");
      
      //Delete the textbox shape
      doc.Body.Paragraphs[1].GetRange().Shapes.First.Delete();
      
      doc.Save("Textbox1.docx");

    Back to Top

    Note: You can also export a Word document containing textbox to PDF and image formats. To know more, refer Export topic.