Java實(shí)現(xiàn)向Word文檔添加文檔屬性
Word文檔屬性包括常規(guī)、摘要、統(tǒng)計(jì)、內(nèi)容、自定義。其中摘要包括標(biāo)題、主題、作者、經(jīng)理、單位、類別、關(guān)鍵詞、備注等項(xiàng)目。屬性相當(dāng)于文檔的名片,可以添加你想要的注釋、說明等。還可以標(biāo)注版權(quán)。 今天就為大家介紹如何通過Java代碼向Word文檔添加文檔屬性。詳情請(qǐng)閱讀以下內(nèi)容。
- 將內(nèi)置文檔屬性添加到 Word 文檔
- 將自定義文檔屬性添加到 Word 文檔
程序環(huán)境:
方法1:手動(dòng)引入。
將 ??Free Spire.Doc for Java?? 下載到本地,解壓,找到lib文件夾下的Spire.Doc.jar文件。在IDEA中打開如下界面,將本地路徑中的jar文件引入Java程序
方法2: 如果您想通過 ??Maven??安裝,則可以在 pom.xml 文件中添加以下代碼導(dǎo)入 JAR 文件。
<repositories> <repository> <id>com.e-iceblue</id> <url>https://repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc.free</artifactId> <version>5.2.0</version> </dependency> </dependencies>
將內(nèi)置文檔屬性添加到 Word 文檔
- 初始化Document類的一個(gè)實(shí)例。
- 使用 Document.loadFromFile() 方法加載 Word 文檔。
- 使用 Document.getBuiltinDocumentProperties() 方法訪問文檔的內(nèi)置文檔屬性。
- 使用 BuiltinDocumentProperties 類提供的 setTitle()、setSubject() 和 setAuthor() 方法設(shè)置特定文檔屬性的值,例如標(biāo)題、主題和作者。
- 使用 Document.saveToFile() 方法保存結(jié)果文檔。
完整代碼
import com.spire.doc.BuiltinDocumentProperties; import com.spire.doc.Document; import com.spire.doc.FileFormat; public class AddBuiltinDocumentProperties { public static void main(String []args) throws Exception { //初始化文檔類的一個(gè)實(shí)例 Document document = new Document(); //加載 Word 文檔 document.loadFromFile("示例文檔.docx"); //訪問文檔的內(nèi)置文檔屬性 BuiltinDocumentProperties standardProperties = document.getBuiltinDocumentProperties(); //設(shè)置特定內(nèi)置文檔屬性的值 standardProperties.setTitle("添加文檔屬性"); standardProperties.setSubject("XXX公司員工培訓(xùn)手冊(cè)"); standardProperties.setAuthor("張三"); standardProperties.setCompany("XXX有限公司"); standardProperties.setManager("李四"); standardProperties.setCategory("注意事項(xiàng)"); standardProperties.setKeywords("員工培訓(xùn), 規(guī)章制度, 文檔屬性"); standardProperties.setComments("本手冊(cè)為員工手冊(cè)和規(guī)章制度的相關(guān)介紹"); //保存結(jié)果文檔 document.saveToFile("添加內(nèi)置文檔屬性.docx", FileFormat.Docx_2013); } }
效果圖
將自定義文檔屬性添加到Word文檔l
- 初始化Document類的一個(gè)實(shí)例。
- 使用 Document.loadFromFile() 方法加載 Word 文檔。
- 使用 Document.getCustomDocumentProperties() 方法訪問文檔的自定義文檔屬性。
- 使用 CustomDocumentProperties.add(String, Object) 方法將具有不同數(shù)據(jù)類型的自定義文檔屬性添加到文檔中。
- 使用 Document.saveToFile() 方法保存結(jié)果文檔。
完整代碼
import com.spire.doc.CustomDocumentProperties; import com.spire.doc.Document; import com.spire.doc.FileFormat; import java.util.Date; public class AddCustomDocumentProperties { public static void main(String []args) throws Exception { //初始化Document類的一個(gè)實(shí)例 Document document = new Document(); //加載 Word 文檔 document.loadFromFile("測(cè)試文檔.docx"); //訪問文檔的自定義文檔屬性 CustomDocumentProperties customProperties = document.getCustomDocumentProperties(); //將具有不同數(shù)據(jù)類型的自定義文檔屬性添加到文檔中 customProperties.add("編號(hào)", 10086); customProperties.add("授權(quán)", true); customProperties.add("授權(quán)者", "John Smith"); customProperties.add("授權(quán)日期", new Date().toString()); //保存結(jié)果文檔 document.saveToFile("添加自定義文檔屬性.docx", FileFormat.Docx_2013); } }
效果圖
—本文完—
到此這篇關(guān)于Java實(shí)現(xiàn)向Word文檔添加文檔屬性的文章就介紹到這了,更多相關(guān)Java向Word文檔添加文檔屬性內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java Apache poi 對(duì)word doc文件進(jìn)行讀寫操作
- java導(dǎo)出生成word的簡(jiǎn)單方法
- java實(shí)現(xiàn)word文件轉(zhuǎn)html文件
- Java實(shí)現(xiàn)用Freemarker完美導(dǎo)出word文檔(帶圖片)
- java實(shí)現(xiàn)在線預(yù)覽--poi實(shí)現(xiàn)word、excel、ppt轉(zhuǎn)html的方法
- java使用poi讀取ppt文件和poi讀取excel、word示例
- 實(shí)例講解Java讀取一般文本文件和word文檔的方法
- 使用Java讀取Word文件的簡(jiǎn)單例子分享
- java使用POI實(shí)現(xiàn)html和word相互轉(zhuǎn)換
- Java使用poi將word轉(zhuǎn)換為html
相關(guān)文章
JAVA正則表達(dá)式提取key-value類型字符值代碼實(shí)例
這篇文章主要給大家介紹了關(guān)于JAVA正則表達(dá)式提取key-value類型字符值的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-10-10mybatis條件語句中帶數(shù)組參數(shù)的處理
這篇文章主要介紹了mybatis條件語句中帶數(shù)組參數(shù)的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09mybatis-plus團(tuán)隊(duì)新作mybatis-mate實(shí)現(xiàn)數(shù)據(jù)權(quán)限
本文主要介紹了mybatis-plus 團(tuán)隊(duì)新作 mybatis-mate 輕松搞定數(shù)據(jù)權(quán)限,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09java構(gòu)造器的重載實(shí)現(xiàn)實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于java構(gòu)造器的重載實(shí)現(xiàn)實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-01-01Java 創(chuàng)建動(dòng)態(tài)類和查看方法列表信息的實(shí)例
這篇文章主要介紹了 Java 創(chuàng)建動(dòng)態(tài)類和查看方法列表信息的實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06java通過Excel批量上傳數(shù)據(jù)的實(shí)現(xiàn)示例
Excel批量上傳是常見的一種功能,本文就來介紹一下java通過Excel批量上傳數(shù)據(jù)的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10Springboot 使用 JSR 303 對(duì) Controller 控制層校驗(yàn)及 Service 服務(wù)層 AOP 校驗(yàn)
這篇文章主要介紹了Springboot 使用 JSR 303 對(duì) Controller 控制層校驗(yàn)及 Service 服務(wù)層 AOP 校驗(yàn) 使用消息資源文件對(duì)消息國際化的相關(guān)知識(shí),需要的朋友可以參考下2017-12-12