Java Add Word Text Box

Keywords: Java Programming Maven

In Word, a text box is a portable, resizable text or graphics container. We can add text, pictures, forms and other objects to the text box. Next, we will add the above objects to the Word text box through Java programming.

Use Tools: Free Spire.Doc for Java (Free Edition)

Jar file acquisition and import:

Method 1: Through Download official website Get the jar package. After downloading, extract the file and import the Spire.Doc.jar file under the lib folder into the Java program. (as shown below)

 

 

 

Method 2: Through maven Warehouse Installation Import.

 

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 documents
        Document doc = new Document();

        //Add a text box of specified size
        TextBox tb = doc.addSection().addParagraph().appendTextBox(380, 275);
        //Setting 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);

        //Setting Text Box Border Style
        tb.getFormat().setLineStyle(TextBoxLineStyle.Thin_Thick);
        tb.getFormat().setLineColor(Color.gray);

        //Insert pictures into text boxes
        Paragraph para = tb.getBody().addParagraph();
        DocPicture picture = para.appendPicture("5G.png");
        picture.setHeight(120f);
        picture.setWidth(180f);
        para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        para.getFormat().setAfterSpacing(13f);

        //Insert text into text box
        para = tb.getBody().addParagraph();
        TextRange textRange = para.appendText("Sino-US trade disputes, also known as Sino-US trade wars, also known as Sino-US trade frictions, are important issues in Sino-US economic relations. "
                + "Trade disputes mainly occur in two aspects: first, China has a comparative advantage in the export field;"
                + "The two is China's import and technological knowledge field which has no advantage.");
        textRange.getCharacterFormat().setFontName("Regular script");
        textRange.getCharacterFormat().setFontSize(11f);
        para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        //Add tables to text boxes
        //Declare array content
        String[][] data = new String[][]{
                new String[]{"Sino-US Import and Export Balance"},
                new String[]{"Country", "Particular year", "Exports (US dollars)", "Imports (US dollars)"},
                new String[]{"China", "2017", "125468", "101109"},
                new String[]{"U.S.A", "2017", "86452", "124298"},
        };
        //Add form
        Table table = tb.getBody().addTable();
        //Specify the number of rows and columns in the table
        table.resetCells(4,4);
        //Fill in the table with the contents of the array
        for (int i = 0; i < data.length; i++) {
            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; j++) {
                dataRow.getCells().get(j).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                TextRange range2 = dataRow.getCells().get(j).addParagraph().appendText(data[i][j]);
                range2.getCharacterFormat().setFontName("Regular script");
                range2.getCharacterFormat().setFontSize(11f);
                range2.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
                range2.getCharacterFormat().setBold(true);
            }
        }
        TableRow row = table.getRows().get(1);
        for (int z = 0; z < row.getCells().getCount(); z++) {
            row.getCells().get(z).getCellFormat().setBackColor(new Color(176,224,238));
        }
        //Lateral merge cell
        table.applyHorizontalMerge(0,0,3);
        //Applying Form Style
        table.applyStyle(DefaultTableStyle.Table_Grid_5);

        //Save document
        doc.saveToFile("AddTextbox.docx", FileFormat.Docx_2013);
        doc.dispose();
    }
}

Text box add effect:

 

 

(End of this article)

Posted by DeepakJ on Thu, 03 Oct 2019 01:24:00 -0700