Tuesday, 27 October 2020

【Java】Insert or Delete Text Boxes in Word Documents

Text boxes in Word documents are a container that saves editable text or images. You add them to documents for extra visual appeal or to call out sections of text within the document. This article will demonstrate how to insert a text box with an image and text in a Word document and delete it from the document using Java programmatically.

Before typing codes

At first, you need to download a free third-party library Free Spire.Doc for Java from the link, unzip it and then find Spire.Doc.jar in the “lib” folder. Finally, add Spire.Doc.jar to your IDEA. Here is a screenshot of final look in IDEA.

Using the code

Insert a Text Box

Free Spire.Doc for Java provides the method Paragraph.AppendTextBox to enable users to insert a text box in specified paragraph of Word document, and an image, text or table can be added to the text box in the process of inserting it. Here are detailed steps to insert a text box with an image and text in Word.

Step 1: Load a Word document and append a text box with specified size to specified paragraph;

Step 2: Set Format properties, including TextWrappingStyle, HorizontalPosition, VerticalPosition, LineStyle and LineColor.

Step 3: Declare a new Paragraph and insert an image using the appendPicture method. Next, set properties for the image, including Height, Width, HorizontalAlignment.

Step 4: insert text by using the appendText method and set CharacterFormat properties, including FontName and FonSize.

Step 5: Using the saveToFile method to saving the resulting document.

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) {
       
//load a Word document
       
Document doc = new Document();
        doc.loadFromFile(
"C:\\Users\\Test1\\Desktop\\Sample.docx");

       
//append a textbox to paragraph
       
TextBox tb = doc.getSections().get(0).addParagraph().appendTextBox(100f, 320f);

       
//set the text wrapping style
       
tb.getFormat().setTextWrappingStyle(TextWrappingStyle.Square);

       
//set the position of textbox
       
tb.getFormat().setHorizontalOrigin(HorizontalOrigin.Right_Margin_Area);
        tb.getFormat().setHorizontalPosition(-
100f);
        tb.getFormat().setVerticalOrigin(VerticalOrigin.
Page);
        tb.getFormat().setVerticalPosition(
100f);

       
//set the border style of textbox
       
tb.getFormat().setLineStyle(TextBoxLineStyle.Thin_Thick);
        tb.getFormat().setLineColor(
new Color(240,135,152));

       
//insert an image to textbox as the first paragraph
        
Paragraph para = tb.getBody().addParagraph();
        DocPicture picture = para.appendPicture(
"C:\\Users\\Test1\\Desktop\\Image.jpg");
        picture.setHeight(
60f);
        picture.setWidth(
90f);
        para.getFormat().setHorizontalAlignment(HorizontalAlignment.
Center);
        para.getFormat().setAfterSpacing(
15f);

       
//insert text to textbox as the second paragraph
       
para = tb.getBody().addParagraph();
        TextRange textRange = para.appendText(
"A cashless society is one in which financial transactions are handled " +
               
"through the transfer of digital information instead of physical money in the form of banknotes or coins.");
        textRange.getCharacterFormat().setFontName(
"Times New Roman");
        textRange.getCharacterFormat().setFontSize(
12f);
        para.getFormat().setHorizontalAlignment(HorizontalAlignment.
Center);

       
//save to file
       
doc.saveToFile("output/InsertTextbox.docx", FileFormat.Docx_2013);
    }
}

Output


Remove a Text Box

Using several rows of code snippets provided by Free Spire.Doc for Java, you can easily remove a specified text box or all text boxes in Word.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class DeleteTextBox {
   
public static void main(String[] args) {
       
//load the Word document containing textbox
       
Document doc = new Document();
        doc.loadFromFile(
"C:\\Users\\Test1\\Desktop\\InsertTextbox.docx");
       
//remove textbox by index
       
doc.getTextBoxes().removeAt(0);
       
//remove all textboxes
        //doc.getTextBoxes().clear();
        //save to file
       
doc.saveToFile("output/RemoveTextbox.docx", FileFormat.Docx);
    }
}

Conclusion

In addition to inserting a text box with an image and text in Word and deletingit, Free Spire.Doc for Java

supports insert, read and delete table in a text box, and it also can extract text in a text box. In the Free

Spire.Doc for Java package, you can find the “Sample” folder where code demos of all functions are listed. If there is any problem concerning codes, please leave a message on our Forum or directly contact

us through these ways.

 

No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...