Java 添加、读取、修改、删除Word文档属性

Word文档属性包括常规、摘要、统计、内容、自定义等,其中摘要包括标题、主题、作者、经理、单位、类别、关键词、备注等项目,通过设置这些摘要信息或自定义属性可方便对文档的管理。本文中将主要介绍对文档摘要信息的添加,以及读取或者编辑、删除文档中已设置的摘要信息或自定义文档信息。下面将通过Java代码详细介绍。

**使用工具:**Free Spire.Doc for Java (免费版)

Jar文件获取及导入:

方法1:通过官网下载。下载后,解压,将lib文件夹下的Spire.Doc.jar文件导入java程序。

方法2:通过maven安装导入。

【添加Word文档属性】**

import com.spire.doc.*; import java.sql.Date; import java.time.Clock; import java.time.LocalDate; public class AddDocumentProperty { public static void main(String[] args){ //加载测试文档
        Document doc = new Document("test.docx"); //设置内置文档属性
        doc.getBuiltinDocumentProperties().setTitle("操作手册");
        doc.getBuiltinDocumentProperties().setSubject("Word文档");
        doc.getBuiltinDocumentProperties().setCategory("A类");
        doc.getBuiltinDocumentProperties().setCompany("Alibaba");
        doc.getBuiltinDocumentProperties().setManager("Jamy");
        doc.getBuiltinDocumentProperties().setAuthor("LiuHan");
        doc.getBuiltinDocumentProperties().setKeywords("操作手册,说明书,要件");
        doc.getBuiltinDocumentProperties().setComments("此文档仅供内部使用");
        doc.getBuiltinDocumentProperties().setCreateDate(Date.valueOf(LocalDate.of(2019,7,1)));
        doc.getBuiltinDocumentProperties().setLastSaveDate(Date.valueOf(LocalDate.now(Clock.systemUTC())));
        doc.getBuiltinDocumentProperties().setRevisionNumber("2"); //设置自定义文档属性
        doc.getCustomDocumentProperties().add("文档创建级别","B级");
        doc.getCustomDocumentProperties().add("行政文件否","否"); //保存文档
        doc.saveToFile("SetProperty.docx",FileFormat.Docx_2013);
        doc.dispose();
    }
}

属性添加效果:

706090-20191127134810144-898992015.png

706090-20191127134819530-95146380.png

【读取Word文档属性】

import com.spire.doc.*; public class ReadDocumentProperty { public static void main(String[]args){ //加载文档
        Document doc = new Document("SetProperty.docx"); //读取内置文档属性
        System.out.println("标题: " + doc.getBuiltinDocumentProperties().getTitle());
        System.out.println("主题: " + doc.getBuiltinDocumentProperties().getSubject());
        System.out.println("作者: " + doc.getBuiltinDocumentProperties().getAuthor());
        System.out.println("单位: " + doc.getBuiltinDocumentProperties().getCompany());
        System.out.println("主管: " + doc.getBuiltinDocumentProperties().getManager());
        System.out.println("类别: " + doc.getBuiltinDocumentProperties().getCategory());
        System.out.println("关键字:" + doc.getBuiltinDocumentProperties().getKeywords());
        System.out.println("备注: " + doc.getBuiltinDocumentProperties().getComments()); //获取自定义文档属性
        DocumentProperty property = doc.getCustomDocumentProperties().get(0); //读取自定义文档属性的名称和值
        System.out.println("名称: " + property.getName());
        System.out.println("值: " + property.getValue());
    }
}

文档属性读取结果:

706090-20191127134852759-2073406228.png

【修改/删除文档属性】

import com.spire.doc.*; public class RemoveDocumentProperty { public static void main(String[] args) { //加载文档
        Document doc = new Document();
        doc.loadFromFile("SetProperty.docx"); //直接通过为内置属性赋值新的内容,修改原有摘要信息
        doc.getBuiltinDocumentProperties().setTitle("说明书");
        doc.getBuiltinDocumentProperties().setSubject("测试使用");
        doc.getBuiltinDocumentProperties().setCategory("B类");
        doc.getBuiltinDocumentProperties().setCompany("保密"); //设置内置属性值为空,删除原有摘要信息
        doc.getBuiltinDocumentProperties().setManager("");
        doc.getBuiltinDocumentProperties().setAuthor("");
        doc.getBuiltinDocumentProperties().setKeywords("");
        doc.getBuiltinDocumentProperties().setComments("");
        doc.getBuiltinDocumentProperties().setRevisionNumber(""); //通过方法删除指定属性内容
        doc.getCustomDocumentProperties().remove("文档创建级别");
        doc.getCustomDocumentProperties().remove("行政文件否"); //保存文档
        doc.saveToFile("RemoveProperty.docx",FileFormat.Docx_2013);
        doc.dispose();
    }
}

修改/删除结果:

706090-20191127135600999-1220432199.png

706090-20191127135619625-1439211944.png

愿与诸君共进步,大量的面试题及答案还有资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化、分布式架构等这些成为架构师必备的知识体系,可以微信搜索539413949获取,最后祝大家都能拿到自己心仪的offer

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Apache POI库来读取添加删除Word文档中的书签。下面是一个简单的示例代码,用于读取文档中的书签: ```java import java.io.FileInputStream; import java.io.IOException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; public class ReadBookmarks { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("sample.docx"); XWPFDocument document = new XWPFDocument(fis); // Get all paragraphs in the document List<XWPFParagraph> paragraphs = document.getParagraphs(); // Loop through all paragraphs for (XWPFParagraph paragraph : paragraphs) { // Get all runs in the paragraph List<XWPFRun> runs = paragraph.getRuns(); // Loop through all runs for (XWPFRun run : runs) { // Check if the run contains a bookmark if (run.getCTR().getBookmarkStartList() != null) { // Loop through all bookmarks in the run for (CTBookmark bookmark : run.getCTR().getBookmarkStartList()) { // Print the name of the bookmark System.out.println("Bookmark: " + bookmark.getName()); } } } } // Close the document document.close(); fis.close(); } catch (IOException | InvalidFormatException ex) { ex.printStackTrace(); } } } ``` 这个代码将打开名为“sample.docx”的Word文档,并遍历文档中的所有段落和运行。如果运行包含书签,则它将打印书签的名称。 要添加书签,您可以使用以下代码: ```java import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; public class AddBookmark { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("sample.docx"); XWPFDocument document = new XWPFDocument(fis); // Get the first paragraph in the document XWPFParagraph paragraph = document.getParagraphs().get(0); // Create a new run in the paragraph XWPFRun run = paragraph.createRun(); // Add text to the run run.setText("This is a bookmark"); // Add a bookmark to the run run.getCTR().addNewBookmarkStart().setName("bookmark1"); run.getCTR().addNewBookmarkEnd(); // Save the document FileOutputStream fos = new FileOutputStream("sample.docx"); document.write(fos); // Close the document document.close(); fis.close(); fos.close(); } catch (IOException | InvalidFormatException ex) { ex.printStackTrace(); } } } ``` 这个代码将在文档的第一个段落中创建一个新的运行,并将一个名为“bookmark1”的书签添加到该运行中。 要删除书签,您可以使用以下代码: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; public class DeleteBookmark { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("sample.docx"); XWPFDocument document = new XWPFDocument(fis); // Get the first paragraph in the document XWPFParagraph paragraph = document.getParagraphs().get(0); // Loop through all runs in the paragraph for (XWPFRun run : paragraph.getRuns()) { // Check if the run contains a bookmark if (run.getCTR().getBookmarkStartList() != null) { // Loop through all bookmarks in the run for (CTBookmark bookmark : run.getCTR().getBookmarkStartList()) { // Check if the bookmark has the name "bookmark1" if (bookmark.getName().equals("bookmark1")) { // Remove the bookmark from the run run.getCTR().removeBookmark(bookmark); } } } } // Save the document FileOutputStream fos = new FileOutputStream("sample.docx"); document.write(fos); // Close the document document.close(); fis.close(); fos.close(); } catch (IOException | InvalidFormatException ex) { ex.printStackTrace(); } } } ``` 这个代码将遍历文档的第一个段落中的所有运行,并删除名称为“bookmark1”的书签。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值