Thetext box (textbox) is one of the tools for Word layout. Insert a text box anywhere in the body of the Word document to add supplemental information, put it in the right place, or affect the continuity of the body. We can set the size of the text box, line style, internal margin, background fill and other effects. text box can be mixed with pictures, set the font, size, pictures and so on. in everyday use, it's easy to ignore these elements and just insert a single black text box with text only. Therefore, I think it is necessary to introduce to you and make a well-designed text box, to stimulate.
This post is mainly about how to use C # in a specific location in a Word document, insert a text box with a picture fill, an inner margin, a graphic mix, and a sleek line. Interested bloggers please download free Spire.doc from E-iceblueand add them as Visual Studio references.
Required Namespaces :
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSpire.doc;
usingSpire.Doc.Fields;
usingSpire.Doc.Documents;
usingSystem.Drawing;
Detailed steps:
Step One: load a Word document that contains only text, such as.
Document document =NewDocument ();
Document. LoadFromFile ("The life of Li Bai. docx");
Step Two: Add a text box to the loaded Word document and set its specific location. Here are two points to consider: the position of the inserted page and the page. That is: on which page the text box is inserted, and the text box is in the position of the page. The position of the text box can be confirmed only if the two points are positioned properly. Also, consider the position relationship of the text box and text, which is the set position and wrap (text wrapping). So, a bit of code, by setting the text box in which paragraph, compared to the position of the page and wrap, to determine its location.
TextBox TB = document. sections[0]. paragraphs[0]. Appendtextbox ( Max, -);
TB. Format.horizontalorigin = Horizontalorigin.page;
TB. Format.horizontalposition =370;
TB. Format.verticalorigin = Verticalorigin.page;
TB. Format.verticalposition =155;
TB. Format.textwrappingstyle = Textwrappingstyle.square;
TB. Format.textwrappingtype = Textwrappingtype.both;
Step Three: set the color of the text box, inner margin, picture fill.
TB. Format.linestyle = textboxlinestyle.double;
TB. Format.linecolor = Color.lightgoldenrodyellow;
TB. format.linedashing = Linedashing.solid;
TB. Format.linewidth =3;
TB. Format.InternalMargin.Top = A;
TB. Format.InternalMargin.Bottom =8;
TB. Format.InternalMargin.Left = A;
TB. Format.InternalMargin.Right = A;
TB. Format.FillEfects.Type = backgroundtype.picture;
TB. Format.FillEfects.Picture = Image.FromFile ("2.jpg");
Step Four: add paragraph text, picture, set font, font color, line spacing, paragraph back distance, alignment, etc. in the text box. Then save the document and open the view effect.
Paragraph para1 = TB. Body.addparagraph ();
Para1. Format.afterspacing =6;
Para1. Format.horizontalalignment = HorizontalAlignment.Center;
TextRange TR1 = para1. AppendText ("Li Bai");
TR1. Characterformat.fontname ="XXFarEastFont-Chinese new Wei";
TR1. Characterformat.fontsize = -;
TR1. Characterformat.bold =true;
Paragraph para2 = TB. Body.addparagraph ();
Image image = Image.FromFile ("li Bai. jpg");
Docpicture picture = Para2. Appendpicture (image);
Picture. Width = -;
Picture. Height = the;
Para2. Format.afterspacing =8;
Para2. Format.horizontalalignment = HorizontalAlignment.Center;
Paragraph para3 = TB. Body.addparagraph ();
TextRange TR2 = para3. AppendText ("The Most outstanding poet of Tang Dynasty, the greatest romantic poet in Chinese history, Du Fu praised his article "the Pen falls to the wind and rain, the poem becomes the Ghost"");
TR2. Characterformat.fontname ="XXFarEastFont-Chinese new Wei";
TR2. Characterformat.fontsize = One;
Para3. Format.linespacing = the;
Para3. Format.horizontalalignment = HorizontalAlignment.Left;
Para3. Format.suppressautohyphens =true;
Document. SaveToFile ("R1.docx");
System.Diagnostics.Process.Start ("R1.docx");
:
Complete code example:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSpire.doc;
usingSpire.Doc.Fields;
usingSpire.Doc.Documents;
usingSystem.Drawing;
namespaceTextbox
{
classProgram
{
StaticvoidMain (string[] args)
{
Document document =NewDocument ();
Document. LoadFromFile ("The life of Li Bai. docx");
TextBox TB = document. sections[0]. paragraphs[0]. Appendtextbox ( Max, -);
TB. Format.horizontalorigin = Horizontalorigin.page;
TB. Format.horizontalposition =370;
TB. Format.verticalorigin = Verticalorigin.page;
TB. Format.verticalposition =155;
TB. Format.textwrappingstyle = Textwrappingstyle.square;
TB. Format.textwrappingtype = Textwrappingtype.both;
TB. Format.linestyle = textboxlinestyle.double;
TB. Format.linecolor = Color.lightgoldenrodyellow;
TB. format.linedashing = Linedashing.solid;
TB. Format.linewidth =3;
TB. Format.InternalMargin.Top = A;
TB. Format.InternalMargin.Bottom =8;
TB. Format.InternalMargin.Left = A;
TB. Format.InternalMargin.Right = A;
TB. Format.FillEfects.Type = backgroundtype.picture;
TB. Format.FillEfects.Picture = Image.FromFile ("2.jpg");
Paragraph para1 = TB. Body.addparagraph ();
Para1. Format.afterspacing =6;
Para1. Format.horizontalalignment = HorizontalAlignment.Center;
TextRange TR1 = para1. AppendText ("Li Bai");
TR1. Characterformat.fontname ="XXFarEastFont-Chinese new Wei";
TR1. Characterformat.fontsize = -;
TR1. Characterformat.bold =true;
Paragraph para2 = TB. Body.addparagraph ();
Image image = Image.FromFile ("li Bai. jpg");
Docpicture picture = Para2. Appendpicture (image);
Picture. Width = -;
Picture. Height = the;
Para2. Format.afterspacing =8;
Para2. Format.horizontalalignment = HorizontalAlignment.Center;
Paragraph para3 = TB. Body.addparagraph ();
TextRange TR2 = para3. AppendText ("The Most outstanding poet of Tang Dynasty, the greatest romantic poet in Chinese history, Du Fu praised his article "the Pen falls to the wind and rain, the poem becomes the Ghost"");
TR2. Characterformat.fontname ="XXFarEastFont-Chinese new Wei";
TR2. Characterformat.fontsize = One;
Para3. Format.linespacing = the;
Para3. Format.horizontalalignment = HorizontalAlignment.Left;
Para3. Format.suppressautohyphens =true;
Document. SaveToFile ("R1.docx");
System.Diagnostics.Process.Start ("R1.docx");
}
}
}
Thanks for reading, Welcome to comment Exchange.
C #: Insert a well-formatted text Box into Word