通過(guò)Java添加Word文本框過(guò)程詳解
前言
在Word中,文本框是指一種可移動(dòng)、可調(diào)節(jié)大小的文字或圖形容器。我們可以向文本框中添加文字、圖片、表格等對(duì)象,下面,將通過(guò)Java編程來(lái)實(shí)現(xiàn)添加以上對(duì)象到Word文本框。
使用工具:Free Spire.Doc for Java (免費(fèi)版)
Jar文件獲取及導(dǎo)入:
方法1:通過(guò)官網(wǎng)下載獲取jar包。下載后,解壓文件,并將lib文件夾下的Spire.Doc.jar文件導(dǎo)入Java程序。(如下圖)
方法2:通過(guò)maven倉(cāng)庫(kù)安裝導(dǎo)入。
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) { //創(chuàng)建文檔 Document doc = new Document(); //添加指定大小的文本框 TextBox tb = doc.addSection().addParagraph().appendTextBox(380, 280); //設(shè)置文字環(huán)繞方式 tb.getFormat().setTextWrappingStyle(TextWrappingStyle.Square); //設(shè)置文本框的相對(duì)位置 tb.getFormat().setHorizontalOrigin(HorizontalOrigin.Left_Margin_Area); tb.getFormat().setHorizontalPosition(120f); tb.getFormat().setVerticalOrigin(VerticalOrigin.Page); tb.getFormat().setVerticalPosition(100f); //設(shè)置文本框邊框樣式 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("中美貿(mào)易爭(zhēng)端,又稱中美貿(mào)易戰(zhàn),也叫中美貿(mào)易摩擦,是中美經(jīng)濟(jì)關(guān)系中的重要問(wèn)題。 " + "貿(mào)易爭(zhēng)端主要發(fā)生在兩個(gè)方面:一是中國(guó)具有比較優(yōu)勢(shì)的出口領(lǐng)域;" + "二是中國(guó)沒(méi)有優(yōu)勢(shì)的進(jìn)口和技術(shù)知識(shí)領(lǐng)域。"); textRange.getCharacterFormat().setFontName("楷體"); textRange.getCharacterFormat().setFontSize(11f); para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); //添加表格到文本框 //聲明數(shù)組內(nèi)容 String[][] data = new String[][]{ new String[]{"國(guó)家", "年份", "出口額(美元)", "進(jìn)口額(美元)"}, new String[]{"中國(guó)", "2017", "125468", "101109"}, new String[]{"美國(guó)", "2017", "86452", "124298"}, }; //添加表格 Table table = tb.getBody().addTable(); //指定表格行數(shù)、列數(shù) table.resetCells(3,4); //將數(shù)組內(nèi)容填充到表格 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); } } //應(yīng)用表格樣式 table.applyStyle(DefaultTableStyle.Colorful_Grid_Accent_3); //保存文檔 doc.saveToFile("AddTextbox.docx", FileFormat.Docx_2013); } }
文本框添加效果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
dockerfile-maven-plugin極簡(jiǎn)教程(推薦)
這篇文章主要介紹了dockerfile-maven-plugin極簡(jiǎn)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10java二叉查找樹(shù)的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了java二叉查找樹(shù)的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08spring使用Filter過(guò)濾器對(duì)Response返回值進(jìn)行修改的方法
這篇文章主要介紹了spring使用Filter過(guò)濾器對(duì)Response返回值進(jìn)行修改,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09springboot mybatis-plus實(shí)現(xiàn)登錄接口
本文主要介紹了springboot mybatis-plus實(shí)現(xiàn)登錄接口,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11SpringBoot利用切面注解及反射實(shí)現(xiàn)事件監(jiān)聽(tīng)功能
這篇文章主要介紹了springboot事件監(jiān)聽(tīng),通過(guò)利用切面、注解、反射實(shí)現(xiàn),接下來(lái)將對(duì)這幾種方式逐一說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2022-07-07基于spring mvc請(qǐng)求controller訪問(wèn)方式
這篇文章主要介紹了spring mvc請(qǐng)求controller訪問(wèn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09IDEA使用技巧之如何將本地項(xiàng)目和git遠(yuǎn)程項(xiàng)目關(guān)聯(lián)
這篇文章主要介紹了IDEA使用技巧之如何將本地項(xiàng)目和git遠(yuǎn)程項(xiàng)目關(guān)聯(lián)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02解決mybatis-plus自動(dòng)配置的mapper.xml與java接口映射問(wèn)題
這篇文章主要介紹了解決mybatis-plus自動(dòng)配置的mapper.xml與java接口映射問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08