这篇文章给大家分享的是有关如何通过Java添加Word文本框的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
前言
在Word中,文本框是指一种可移动、可调节大小的文字或图形容器。我们可以向文本框中添加文字、图片、表格等对象,下面,将通过Java编程来实现添加以上对象到Word文本框。
使用工具:Free Spire.Doc for Java (免费版)
Jar文件获取及导入:
方法1:通过官网下载获取jar包。下载后,解压文件,并将lib文件夹下的Spire.Doc.jar文件导入Java程序。(如下图)
方法2:通过maven仓库安装导入。
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 AddTextbox {
public static void main(String[] args) {
//创建文档
Document doc = new Document();
//添加指定大小的文本框
TextBox tb = doc.addSection().addParagraph().appendTextBox(380, 280);
//设置文字环绕方式
tb.getFormat().setTextWrappingStyle(TextWrappingStyle.Square);
//设置文本框的相对位置
tb.getFormat().setHorizontalOrigin(HorizontalOrigin.Left_Margin_Area);
tb.getFormat().setHorizontalPosition(120f);
tb.getFormat().setVerticalOrigin(VerticalOrigin.Page);
tb.getFormat().setVerticalPosition(100f);
//设置文本框边框样式
tb.getFormat().setLineStyle(TextBoxLineStyle.Thin_Thick);
tb.getFormat().setLineColor(Color.gray);
//插入图片到文本框
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);
//插入文字到文本框
para = tb.getBody().addParagraph();
TextRange textRange = para.appendText("中美贸易争端,又称中美贸易战,也叫中美贸易摩擦,是中美经济关系中的重要问题。 "
+ "贸易争端主要发生在两个方面:一是中国具有比较优势的出口领域;"
+ "二是中国没有优势的进口和技术知识领域。");
textRange.getCharacterFormat().setFontName("楷体");
textRange.getCharacterFormat().setFontSize(11f);
para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//添加表格到文本框
//声明数组内容
String[][] data = new String[][]{
new String[]{"国家", "年份", "出口额(美元)", "进口额(美元)"},
new String[]{"中国", "2017", "125468", "101109"},
new String[]{"美国", "2017", "86452", "124298"},
};
//添加表格
Table table = tb.getBody().addTable();
//指定表格行数、列数
table.resetCells(3,4);
//将数组内容填充到表格
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("楷体");
range2.getCharacterFormat().setFontSize(11f);
range2.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
}
}
//应用表格样式
table.applyStyle(DefaultTableStyle.Colorful_Grid_Accent_3);
//保存文档
doc.saveToFile("AddTextbox.docx", FileFormat.Docx_2013);
}
}
文本框添加效果:
感谢各位的阅读!关于“如何通过Java添加Word文本框”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。