Aspose.Words for Java是一个功能强大的Java Word文档处理组件,支持文档的生成、修改、转换、渲染等功能。本文将为您全面介绍Aspose.Words的主要功能与用法。
一、文档的生成
我们可以通过Aspose.Words完全通过Java代码生成Word文档,而无需使用Microsoft Word。
// 创建空白文档
Document doc = new Document();
// 添加段落
Paragraph para = doc.getFirstSection().addParagraph();
para.appendChild("Hello world!");
// 保存文档
doc.save("D:\\test.docx");
二、打开和修改现有Word文档
Aspose.Words读取和更新现有的word文档:
// 读取文档
Document doc = new Document("D:\\test.docx");
// 访问各个元素
Paragraph firstPara = doc.getFirstSection().getBody().getFirstParagraph();
firstPara.getRuns().get(0).setText("Hi there!");
// 插入&删除元素
Paragraph newPara = doc.getFirstSection().addParagraph("New paragraph");
doc.getFirstSection().getBody().removeChild(newPara);
// 保存更新
doc.save("D:\\test.docx");
三、文档格式转换
Aspose.Words for Java支持将Word文档转换为多种格式,如PDF、HTML、EPUB、XML等。
// 加载word文档
Document doc = new Document("D:\\test.docx");
// 转换为PDF
doc.save("D:\\test.pdf", SaveFormat.PDF);
// 转换为HTML
doc.save("D:\\test.html", SaveFormat.HTML);
四、管理样式
Aspose.Words可以方便地创建、访问和修改文档中的样式:
/ 创建样式
Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle");
// 设置样式属性
style.getFont().setBold(true);
style.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
// 应用样式
Paragraph para = doc.getFirstSection().getBody().addParagraph();
para.getParagraphFormat().setStyle(style);
// 修改样式
style.getFont().setColor(Color.RED);
五、使用书签、页眉页脚
我们可以在文档中插入书签、页眉和页脚,并进行更新:
// 插入书签
Bookmark bookmark = doc.getBookmarks().add("MyBookmark");
// 添加页眉
HeaderFooter header = doc.getFirstSection().getHeadersFooters().getHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
header.addParagraph("Page header");
// 更新页脚中的页码
doc.getSections().get_Item(0).getHeadersFooters().getFooter(HeaderFooterType.FOOTER_PRIMARY).getPageNumbers().setNumberFormat("Page {PAGE} of {NUMPAGES}");
六、文档元素与Word对应关系
Aspose.Words几乎包含了Word文档的全部元素,不同元素对应不同的类,我们通过这些类来操作文档。例如:
- Document -> Word文档
// 创建空白文档
Document doc = new Document();
// 添加内容
Paragraph para = doc.getFirstSection().addParagraph();
para.appendChild("Hello world!");
// 保存文档
doc.save("D:\\test.docx");
- Section -> Word文档中的节
// 读取文档
Document doc = new Document("D:\\test.docx");
// 插入新节
Section newSection = doc.insertSection(doc.getSections().getCount());
newSection.getPageSetup().setOrientation(Orientation.LANDSCAPE);
// 添加内容到新节中
Paragraph para = newSection.getBody().addParagraph();
para.appendChild("This is a new section.");
// 保存更新
doc.save("D:\\test.docx");
- Body -> Word文档主体内容
// 读取文档
Document doc = new Document("D:\\test.docx");
// 获取文档主体部分
Body body = doc.getFirstSection().getBody();
// 在文档末尾添加段落
Paragraph para = body.addParagraph();
para.appendChild("This is a new paragraph.");
// 保存更新
doc.save("D:\\test.docx");
- Paragraph -> Word中的段落
// 读取文档
Document doc = new Document("D:\\test.docx");
// 获取文档主体部分
Body body = doc.getFirstSection().getBody();
// 在文档末尾添加段落
Paragraph para = body.addParagraph();
para.appendChild("This is a new paragraph.");
// 保存更新
doc.save("D:\\test.docx");
- Table -> Word中的表格
// 创建新文档
Document doc = new Document();
// 添加表格
Table table = doc.getFirstSection().getBody().addTable();
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_CONTENTS);
for (int i = 0; i < 3; i++) {
Row row = table.getRows().addRow();
for (int j = 0; j < 3; j++) {
Cell cell = row.getCells().add(String.format("Cell %d, %d", i+1, j+1));
}
}
// 保存文档
doc.save("D:\\test.docx");
- Style -> Word中的样式
// 读取文档
Document doc = new Document("D:\\test.docx");
// 创建新样式
Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle");
// 设置样式属性
style.getFont().setBold(true);
style.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
// 应用样式
Paragraph para = doc.getFirstSection().getBody().addParagraph();
para.getParagraphFormat().setStyle(style);
// 保存更新
doc.save("D:\\test.docx");
- Bookmark -> Word中的书签
// 读取文档
Document doc = new Document("D:\\test.docx");
// 创建新样式
Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle");
// 设置样式属性
style.getFont().setBold(true);
style.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
// 应用样式
Paragraph para = doc.getFirstSection().getBody().addParagraph();
para.getParagraphFormat().setStyle(style);
// 保存更新
doc.save("D:\\test.docx");
- Image -> Word中的图片
// 读取文档
Document doc = new Document("D:\\test.docx");
// 创建新样式
Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle");
// 设置样式属性
style.getFont().setBold(true);
style.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
// 应用样式
Paragraph para = doc.getFirstSection().getBody().addParagraph();
para.getParagraphFormat().setStyle(style);
// 保存更新
doc.save("D:\\test.docx");