文本框是一种可以插入到 Word 文档中任意位置的文本或图像容器。MS Word 提供了几种预置格式的文本框,你也可以自己设计文本框的样式,用自己独特的创意吸引读者。本文将展示如何使用 Spire.Doc for Java 以编程的方式在 Word 文档中添加或删除文本框。
安装 Spire.Doc for Java
首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。您可以从这个链接下载 JAR 文件;如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>13.10.6</version>
    </dependency>
</dependencies>
在 Word 文档中插入文本框
Spire.Doc for Java 提供了 Paragraph.appendTextBox(float width, float height) 方法以插入文本框到指定段落。下面是详细操作步骤:
- 创建 Document 类的对象,并用 Document.loadFromFile() 方法载入 Word 文档。
- 使用 Document.getSections().get() 方法获取文档第一节,然后使用 Section.addParagraph() 方法获取段落。
- 使用 Paragraph.appendTextBox(float width, float height) 方法在该段落中添加一个文本框。
- 使用 TextBox.getFormat() 方法获取添加的文本框的文字格式,然后使用 TextBoxFormat 类下的方法设置文本框的文本环绕方式、位置、边框颜色以及填充颜色。
- 使用 TextBox.getBody().addParagraph() 在文本框中添加一个段落,然后使用 Paragraph.appendPicture() 方法在该段落中添加图片。
- 使用 Paragraph.appendText() 方法在文本框中添加文本,并设置字体格式。
- 使用 Document.saveToFile() 方法保存文档。
- 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) {
        
        //创建 Document 类的对象
        Document doc = new Document();
        //载入Word文档
        doc.loadFromFile("C:/示例.docx");
        //创建文本框,并设置其文本环绕方式
        TextBox tb = doc.getSections().get(0).getParagraphs().get(0).appendTextBox(120f, 230f);
        tb.getFormat().setTextWrappingStyle(TextWrappingStyle.Square);
        //设置文本框的位置
        tb.getFormat().setHorizontalOrigin(HorizontalOrigin.Right_Margin_Area);
        tb.getFormat().setHorizontalPosition(-100f);
        tb.getFormat().setVerticalOrigin(VerticalOrigin.Page);
        tb.getFormat().setVerticalPosition(165f);
        //设置文本框的边框颜色和填充颜色
        tb.getFormat().setLineColor(Color.BLUE);
        tb.getFormat().setFillColor(new Color(203,234,253) );
        //在文本框中插入图片
        Paragraph para = tb.getBody().addParagraph();
        DocPicture picture = para.appendPicture("C:/图片.jpg");
        //设置段落对齐方式
        para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        //设置图片大小
        picture.setHeight(90f);
        picture.setWidth(90f);
        //在文本框中插入文字
        para = tb.getBody().addParagraph();
        TextRange textRange = para.appendText("考古学家忙于挖掘时,伊恩•霍德正构建“后过程”考古学的后现代方法。"
                +"过程考古学是20世纪60年代一场运动的名称,由刘易斯•宾福特领导,旨在使考古学更加科学。");
        //设置段落对齐方式
        para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        //设置字体
        textRange.getCharacterFormat().setFontName("等线");
        textRange.getCharacterFormat().setFontSize(9f);
        textRange.getCharacterFormat().setItalic(true);
        //保存文档
        doc.saveToFile("插入文本框.docx", FileFormat.Docx_2013);
    }
}
从 Word 文档中移除文本框
Spire.Doc for Java 提供了 Document.getTextBoxes().removeAt() 方法用于删除指定文本框。如果想要删除所有文本框,可以使用 Document.getTextBoxes().clear() 方法实现。以下是删除文本框的操作步骤:
- 创建 Document 类的对象。
- 使用 Document.loadFromFile() 方法载入 Word 文档。
- 使用 Document.getTextBoxes().removeAt() 方法删除第一个文本框。
- 使用 Document.saveToFile() 方法保存文档。
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class RemoveTextBox {
    public static void main(String[] args) {
        //创建 Document 类的对象
        Document doc = new Document();
        //载入Word文档
        doc.loadFromFile("C:/示例.docx");
        //根据索引移除文本框
        doc.getTextBoxes().removeAt(0);
        //移除所有文本框
        //doc.getTextBoxes().clear();
        //保存文档
        doc.saveToFile("移除文本框.docx", FileFormat.Docx);
    }
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



