Java: Extract or Update Textboxes in Word
Text boxes in Microsoft Word are flexible elements that improve the layout and design of documents. They enable users to place text separately from the main text flow, facilitating the creation of visually attractive documents. At times, you might need to extract text from these text boxes for reuse, or update the content within them to maintain clarity and relevance. This article demonstrates how to extract or update textboxes in a Word document using Java with Spire.Doc for Java.
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>12.11.0</version> </dependency> </dependencies>
Extract Text from a Textbox in Word in Java
With Spire.Doc for Java, you can access a specific text box in a document using the Document.getTextBoxes().get() method. You can then iterate through the child objects of the text box to check if each one is a paragraph or a table. For paragraphs, retrieve the text using the Paragraph.getText() method. For tables, loop through the cells to extract text from each cell.
Here are the steps to extract text from a text box in a Word document:
- Create a Document object.
- Load a Word file using Document.loadFromFile() method.
- Access a specific text box using Document.getTextBoxes().get() method.
- Iterate through the child objects of the text box.
- Check if a child object is a paragraph. If so, use Paragraph.getText() method to get the text.
- Check if a child object is a table. If so, use extractTextFromTable() method to retrieve the text from the table.
- Java
import com.spire.doc.*; import com.spire.doc.documents.DocumentObjectType; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.TextBox; import java.io.FileWriter; import java.io.IOException; public class ExtractTextFromTextbox { public static void main(String[] args) throws IOException { // Create a Document object Document document = new Document(); // Load a Word file document.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx"); // Get a specific textbox TextBox textBox = document.getTextBoxes().get(0); // Create a FileWriter to write extracted text to a txt file FileWriter fileWriter = new FileWriter("Extracted.txt"); // Iterate though child objects of the textbox for (Object object: textBox.getChildObjects()) { // Determine if the child object is a paragraph if (((DocumentObject) object).getDocumentObjectType() == DocumentObjectType.Paragraph) { // Write paragraph text to the txt file fileWriter.write(((Paragraph)object).getText() + "\n"); } // Determine if the child object is a table if (((DocumentObject) object).getDocumentObjectType() == DocumentObjectType.Table) { // Extract text from table to the txt file extractTextFromTable((Table)object, fileWriter); } } // Close the stream fileWriter.close(); } // Extract text from a table static void extractTextFromTable(Table table, FileWriter fileWriter) throws IOException { for (int i = 0; i < table.getRows().getCount(); i++) { TableRow row = table.getRows().get(i); for (int j = 0; j < row.getCells().getCount(); j++) { TableCell cell = row.getCells().get(j); for (Object paragraph: cell.getParagraphs()) { fileWriter.write(((Paragraph) paragraph).getText() + "\n"); } } } } }
Update a Textbox in Word in Java
To modify a text box, first remove its existing content using TextBox.getChildObjects.clear() method. Then, create a new paragraph and assign the desired text to it.
Here are the steps to update a text box in a Word document:
- Create a Document object.
- Load a Word file using Document.loadFromFile() method.
- Get a specific textbox using Document.getTextBoxes().get() method.
- Remove existing content of the textbox using TextBox.getChildObjects().clear() method.
- Add a paragraph to the textbox using TextBox.getBody().addParagraph() method.
- Add text to the paragraph using Paragraph.appendText() method.
- Save the document to a different Word file.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.TextBox; import com.spire.doc.fields.TextRange; public class UpdateTextbox { public static void main(String[] args) { // Create a Document object Document document = new Document(); // Load a Word file document.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx"); // Get a specific textbox TextBox textBox = document.getTextBoxes().get(0); // Remove child objects of the textbox textBox.getChildObjects().clear(); // Add a new paragraph to the textbox Paragraph paragraph = textBox.getBody().addParagraph(); // Set line spacing paragraph.getFormat().setLineSpacing(15f); // Add text to the paragraph TextRange textRange = paragraph.appendText("The text in this textbox has been updated."); // Set font size textRange.getCharacterFormat().setFontSize(15f); // Save the document to a different Word file document.saveToFile("UpdateTextbox.docx", FileFormat.Docx_2019); // Dispose resources document.dispose(); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Insert table to Text Box in Word in Java
We have demonstrated how to insert text and image to textbox in a Word document by using Spire.Doc for Java. This article will demonstrate how to insert table to textbox in Word.
import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.*; import java.awt.*; public class insertTableIntoTextBox { public static void main(String[] args) throws Exception{ //Create a new document; add a section and paragraph Document doc = new Document(); Section section = doc.addSection(); Paragraph paragraph = section.addParagraph(); //Add a textbox to the paragraph TextBox textbox = paragraph.appendTextBox(380, 100); //Set the position of the textbox textbox.getFormat().setHorizontalOrigin(HorizontalOrigin.Page); textbox.getFormat().setHorizontalPosition(140); textbox.getFormat().setVerticalOrigin(VerticalOrigin.Page); textbox.getFormat().setVerticalPosition(50); //Insert table to the textbox Table table = textbox.getBody().addTable(true); //Specify the number of rows and columns of the table table.resetCells(4, 4); //Define the data String[][] data = new String[][] { {"Name", "Age", "Gender", "ID"}, {"John", "28", "Male", "0023"}, {"Steve", "30", "Male", "0024"}, {"Lucy", "26", "female", "0025"} }; //Add data to the table for (int i = 0; i < 4; 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 < 4; j++) { TextRange tableRange = table.getRows().get(i).getCells().get(j).addParagraph().appendText(data[i][j]); tableRange.getCharacterFormat().setFontName("Arial"); tableRange.getCharacterFormat().setFontSize(11f); tableRange.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center); tableRange.getCharacterFormat().setBold(true); } } //Set the background color for the first row TableRow row = table.getRows().get(0); for (int z = 0; z < row.getCells().getCount(); z++) { row.getCells().get(z).getCellFormat().setBackColor(new Color(176,224,238)); } //Apply style to the table table.applyStyle(DefaultTableStyle.Table_Grid_5); //Save the document String output = "output/insertTableIntoTextBox.docx"; doc.saveToFile(output, FileFormat.Docx_2013); } }
The effective screenshot after insert table to Textbox in Word:
Java: Insert or Remove a Text Box in Word
A text box is an element you can insert and position anywhere in a document. MS Word provides several pre-formatted text boxes, and you can also create custom text boxes with unique appearances to grab the reader's attention. In this article, you will learn how to programmatically insert or remove a text box in a Word document using Spire.Doc for Java.
Install Spire.Doc for Java
First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>12.11.0</version> </dependency> </dependencies>
Insert a Text Box in a Word Document
Spire.Doc for Java provides the Paragraph.appendTextBox(float width, float height) method to insert a text box in a specified paragraph. The detailed steps are as follows.
- Create a Document instance, and then load a sample Word document using Document.loadFromFile() method.
- Get the first section using Document.getSections().get() method, and then add a paragraph to the section using Section.addParagraph() method.
- Add a text box to the paragraph using Paragraph.appendTextBox(float width, float height) method.
- Get the format of the text box using TextBox.getFormat() method, and then set the text box's wrapping type, position, border color and fill color using the methods under TextBoxFormat Class.
- Add a paragraph to the text box using TextBox.getBody().addParagraph() method, and then insert an image to the paragraph using Paragraph.appendPicture() method.
- Insert text to the text box using Paragraph.appendText() method, and then set the text font.
- Save the document to another file using Document.saveToFile() method.
- Java
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 InsertTextbox { public static void main(String[] args) { //Create a Document instance Document doc = new Document(); //Load a Word document doc.loadFromFile("E:\\Files\\Ralph.docx"); //Append a text box and set its wrapping style TextBox tb = doc.getSections().get(0).addParagraph().appendTextBox(120f, 320f); tb.getFormat().setTextWrappingStyle(TextWrappingStyle.Square); //Set the position of text box tb.getFormat().setHorizontalOrigin(HorizontalOrigin.Right_Margin_Area); tb.getFormat().setHorizontalPosition(-100f); tb.getFormat().setVerticalOrigin(VerticalOrigin.Page); tb.getFormat().setVerticalPosition(130f); //Set the border color and fill color of the text box tb.getFormat().setLineColor(Color.BLUE); tb.getFormat().setFillColor(new Color(203,234,253) ); //Insert an image to text box as a paragraph Paragraph para = tb.getBody().addParagraph(); DocPicture picture = para.appendPicture("C:\\Users\\Administrator\\Desktop\\Ralph.jpg"); //Set alignment for the paragraph para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); //Set the size of the inserted image picture.setHeight(90f); picture.setWidth(90f); //Insert text to text box as the second paragraph para = tb.getBody().addParagraph(); TextRange textRange = para.appendText("Emerson is truly the center of the American transcendental movement, " +"setting out most of its ideas and values in a little book, Nature, published in 1836, " +"that represented at least ten years of intense study in philosophy, religion, and literature."); //Set alignment for the paragraph para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); //Set the text font textRange.getCharacterFormat().setFontName("Times New Roman"); textRange.getCharacterFormat().setFontSize(12f); textRange.getCharacterFormat().setItalic(true); //Save to file doc.saveToFile("InsertTextBox.docx", FileFormat.Docx_2013); } }
Remove a Text Box from a Word Document
Spire.Doc for Java provides the Document.getTextBoxes().removeAt() method to delete a specified text box by index. If you want to delete all text boxes from the Word document, you can use the Document.getTextBoxes().clear() method. The below example shows how to remove the first text box from a Word document.
- Create a Document instance.
- Load a sample Word document using Document.loadFromFile() method.
- Remove the first text box using Document.getTextBoxes().removeAt() method.
- Save the document to another file using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; public class DeleteTextbox { public static void main(String[] args) { //Create a Document instance Document doc = new Document(); //Load a Word document doc.loadFromFile("E:\\Files\\TextBox.docx"); //Remove text box by index doc.getTextBoxes().removeAt(0); //Remove all text boxes //doc.getTextBoxes().clear(); //Save to file doc.saveToFile("RemoveTextbox.docx", FileFormat.Docx); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.