Get the App
SLTechnology News&Howtos  ›  Development  › 

How to add a Word text box through Java

Shulou Source: shulou.com Published: 2022-06-03 14:18:06 09月21日 Update

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!

Tags: Text tables content documents text China and the United States trade China learning disputes advantages pictures sizes objects arrays documents methods styles italics knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information NVidia macOS Redmi Shulou Technology