In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to add Word text box through Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to add Word text box through Java".
Preface
In Word, a text box is a removable, resizable text or graphics container. We can add text, pictures, tables and other objects to the text box. Next, we will add the above objects to the Word text box through Java programming.
Using tools: Free Spire.Doc for Java (free version)
Jar file acquisition and import:
Method 1: download the jar package through the official website. After downloading, extract the file and import the Spire.Doc.jar file under the lib folder into the Java program.
Method 2: install and import through the maven repository.
Java code example
Import com.spire.doc.*;import com.spire.doc.documents.*;import com.spire.doc.fields.DocPicture;import com.spire.doc.fields.TextBox;import com.spire.doc.fields.TextRange;import java.awt.*;public class AddTextbox {public static void main (String [] args) {/ / create the document Document doc = new Document (); / / add a text box of the specified size TextBox tb = doc.addSection () .addParagraph () .appendTextBox (380,280) / / set the text wrapping mode tb.getFormat () .setTextWrappingStyle (TextWrappingStyle.Square); / / set the relative position of the text box tb.getFormat () .setHorizontalOrigin (HorizontalOrigin.Left_Margin_Area); tb.getFormat () .setHorizontalPosition (120f); tb.getFormat () .setVerticalOrigin (VerticalOrigin.Page); tb.getFormat () .setVerticalPosition (100f); / / set the text box border style tb.getFormat () .setLineStyle (TextBoxLineStyle.Thin_Thick) Tb.getFormat (). SetLineColor (Color.gray); / / insert the picture into the text box Paragraph para = tb.getBody (). AddParagraph (); DocPicture picture = para.appendPicture ("tp.png"); picture.setHeight (120f); picture.setWidth (180f); para.getFormat (). SetHorizontalAlignment (HorizontalAlignment.Center); para.getFormat (). SetAfterSpacing (13f); / / insert text into the text box para = tb.getBody (). AddParagraph () TextRange textRange = para.appendText ("Sino-US trade dispute, also known as Sino-US trade war, also known as Sino-US trade friction, is an important issue in Sino-US economic relations." "+" trade disputes mainly occur in two aspects: one is the export areas where China has comparative advantages, and the other is the import and technological knowledge areas in which China does not have advantages. ") ; textRange.getCharacterFormat (). SetFontName ("italics"); textRange.getCharacterFormat (). SetFontSize (11f); para.getFormat (). SetHorizontalAlignment (HorizontalAlignment.Center) / add the table to the text box / / declare the contents of the array String [] [] data = new String [] [] {new String [] {"country", "year", "exports (US dollars)", "imports (US dollars)", new String [] {"China", "2017", "125468", "101109"}, new String [] {"United States", "2017" 86452, 124298},} / / add table Table table = tb.getBody (). AddTable (); / / specify the number of rows and columns of the table table.resetCells (3Power4); / / populate the array contents to the table for (int I = 0; I < data.length; iota +) {TableRow dataRow = table.getRows (). Get (I); dataRow.getCells () .get (I) .setWidth (70); dataRow.setHeight (22) DataRow.setHeightType (TableRowHeightType.Exactly); for (int j = 0; j < data [I] .length; jacks +) {dataRow.getCells (). Get (j). GetCellFormat (). SetVerticalAlignment (VerticalAlignment.Middle); TextRange range2 = dataRow.getCells (). Get (j). AddParagraph (). AppendText (Data [I] [j]); range2.getCharacterFormat (). SetFontName ("italics") Range2.getCharacterFormat () .setFontSize (11f); range2.getOwnerParagraph () .getFormat () .setHorizontalAlignment (HorizontalAlignment.Center);}} / / apply tabular style table.applyStyle (DefaultTableStyle.Colorful_Grid_Accent_3); / / Save document doc.saveToFile ("AddTextbox.docx", FileFormat.Docx_2013) }} Thank you for your reading, the above is the content of "how to add Word text boxes through Java". After the study of this article, I believe you have a deeper understanding of how to add Word text boxes through Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.