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.
To add textbox in a document:
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"); |
To modify a textbox:
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"); |
To delete a textbox:
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"); |