|
@@ -26,10 +26,12 @@ import java.util.List;
|
|
|
* 2.aspose设置的页码无法选择从哪一页开始计数,所以页眉页脚与页码需要在模板中提前设置好新增页面的页眉页脚要同上一页页面。无论是拼接上去的word还是插入进去的word都可以顺利继承页眉页脚
|
|
|
* 3.段落模板设置的页眉页脚,用将段落模板插入到某个位置是无效的,页眉页脚带不过去
|
|
|
* 4.由于目录的页码与正文内容多数情况采取的不同样式,如果将目录与正文分开拼接,会导致页码样式丢失,最好是目录和一部分正文内容在一起(如像债权一样将目录和声明作为一个段落模板)。
|
|
|
- * 5.该案例过程大概是 填充封面内容-->将带有不同页码样式的目录与声明拼接到封面后(声明后最好跟上一页带有页眉页脚的空白页面,确保插入的word同步到页眉页脚)-->对段落模板进行值填充-->向声明后的的空白页面插入段落模板-->更新目录页码
|
|
|
+ * 5.该案例过程大概是 填充封面模板内容-->设置主体框架模板的页眉页脚等(框架主要有目录、声明、摘要、正文四部分,这四部分要提前设置好格式)-->分别向框架的四个部分插入替换好值的段落模板-->将框架拼接在封面后-->更新目录页码
|
|
|
*/
|
|
|
public class WordSimpleTests {
|
|
|
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 这里的属性名字要和word设置的域名一致
|
|
|
*/
|
|
@@ -77,13 +79,13 @@ public class WordSimpleTests {
|
|
|
.build();
|
|
|
|
|
|
// 读取模板文件
|
|
|
- byte[] tmplWordByte = Files.readAllBytes(Paths.get("E:\\test\\input.doc"));
|
|
|
+ byte[] tmplWordByte = Files.readAllBytes(Paths.get("E:\\test\\word\\1_cover.doc"));
|
|
|
|
|
|
// 获取填充数据后的文件(AsposeWordUtil是工具类,放在common中)
|
|
|
byte[] resultWordByte = AsposeWordUtil.fillWordDataByDomain(tmplWordByte, wordData);
|
|
|
|
|
|
// 处理该二进制文件并保存
|
|
|
- File resultFile = new File("E:\\test\\output.doc");
|
|
|
+ File resultFile = new File("E:\\test\\word\\1_cover_output.doc");
|
|
|
FileOutputStream fos = new FileOutputStream(resultFile);
|
|
|
fos.write(resultWordByte);
|
|
|
fos.close();
|
|
@@ -97,13 +99,13 @@ public class WordSimpleTests {
|
|
|
void testAppendWord(){
|
|
|
try {
|
|
|
//加载源文档
|
|
|
- Document doc1 = new Document("E:\\test\\output.doc"); // 封面
|
|
|
- Document doc2 = new Document("E:\\test\\catalogue.doc"); // 目录与声明
|
|
|
+ Document doc1 = new Document("E:\\test\\word\\1_cover_output.doc"); // 封面
|
|
|
+ Document doc2 = new Document("E:\\test\\word\\2_framework_output.doc"); // 目录与声明
|
|
|
|
|
|
//将第二个文档的内容附加到第一个文档
|
|
|
doc1.appendDocument(doc2, ImportFormatMode.KEEP_SOURCE_FORMATTING);
|
|
|
//保存合并的文档
|
|
|
- doc1.save("E:\\test\\output.doc");
|
|
|
+ doc1.save("E:\\test\\word\\6_success_output.doc");
|
|
|
} catch (Exception e) {
|
|
|
System.out.println("An error occurred: " + e.getMessage());
|
|
|
e.printStackTrace();
|
|
@@ -111,7 +113,7 @@ public class WordSimpleTests {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 测试向word模板中动态填充数据
|
|
|
+ * 测试向word模板中动态填充数据(带集合的情况)
|
|
|
* 此处案例是设置正文内容
|
|
|
* @throws IOException
|
|
|
*/
|
|
@@ -129,14 +131,16 @@ public class WordSimpleTests {
|
|
|
.contractList(contractList)
|
|
|
.build();
|
|
|
|
|
|
+
|
|
|
+
|
|
|
// 读取模板文件
|
|
|
- byte[] tmplWordByte = Files.readAllBytes(Paths.get("E:\\test\\section.doc"));
|
|
|
+ byte[] tmplWordByte = Files.readAllBytes(Paths.get("E:\\test\\word\\5_main.doc"));
|
|
|
|
|
|
// 获取填充数据后的文件(AsposeWordUtil是工具类,放在common中)
|
|
|
byte[] resultWordByte = AsposeWordUtil.fillWordDataByDomain(tmplWordByte, wordData);
|
|
|
|
|
|
// 处理该二进制文件并保存
|
|
|
- File resultFile = new File("E:\\test\\section_output.doc");
|
|
|
+ File resultFile = new File("E:\\test\\word\\5_main_output.doc");
|
|
|
FileOutputStream fos = new FileOutputStream(resultFile);
|
|
|
fos.write(resultWordByte);
|
|
|
fos.close();
|
|
@@ -153,8 +157,6 @@ public class WordSimpleTests {
|
|
|
datarow.set(0, "Course " + i);
|
|
|
datarow.set(1, "Name " + i);
|
|
|
}
|
|
|
-
|
|
|
- System.out.println(dataTable);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -167,13 +169,13 @@ public class WordSimpleTests {
|
|
|
void testInsertWord() throws Exception {
|
|
|
|
|
|
//设置书签,指定文档拼接的位置(书签需要在模板中提前设置好,相当于标识符)
|
|
|
- String bookmark = "insertSection";
|
|
|
+ String bookmark = "insertMain";
|
|
|
|
|
|
// 主word文件
|
|
|
- Document mainDoc = new Document("E:\\test\\output.doc");
|
|
|
+ Document mainDoc = new Document("E:\\test\\word\\2_framework_output.doc");
|
|
|
|
|
|
// 需要插入的word文件
|
|
|
- Document subDoc = new Document("E:\\test\\section_output.doc");
|
|
|
+ Document subDoc = new Document("E:\\test\\word\\5_main_output.doc");
|
|
|
|
|
|
// 是否纵向纸张
|
|
|
/* boolean isPortrait = true;
|
|
@@ -209,7 +211,7 @@ public class WordSimpleTests {
|
|
|
}*/
|
|
|
mainDoc = AsposeWordUtil.insertDocumentAfterBookMark(mainDoc, subDoc, bookmark, false);
|
|
|
// 保存文件
|
|
|
- mainDoc.save("E:\\test\\success_output.doc");
|
|
|
+ mainDoc.save("E:\\test\\word\\2_framework_output.doc");
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -218,7 +220,7 @@ public class WordSimpleTests {
|
|
|
*/
|
|
|
@Test
|
|
|
void testUpdateCatalogueLink() throws Exception {
|
|
|
- Document doc = new Document("E:\\test\\success_output.doc");
|
|
|
+ Document doc = new Document("E:\\test\\word\\6_success_output.doc");
|
|
|
// 还有一种方式是 doc.updateFields(); 方法,该方法会导致目录样式被更新掉。方便,但是不建议使用
|
|
|
for (Field field : doc.getRange().getFields()){
|
|
|
if (field.getType() == FieldType.FIELD_TOC)
|
|
@@ -229,7 +231,7 @@ public class WordSimpleTests {
|
|
|
field.update();
|
|
|
}
|
|
|
}
|
|
|
- doc.save("E:\\test\\success_output.doc");
|
|
|
+ doc.save("E:\\test\\word\\6_success_output.doc");
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -241,7 +243,7 @@ public class WordSimpleTests {
|
|
|
*/
|
|
|
// @Test
|
|
|
void setHeaderFooter() throws Exception {
|
|
|
- Document mainDoc = new Document("E:\\test\\output.docx");
|
|
|
+ Document mainDoc = new Document("E:\\test\\word\\output.docx");
|
|
|
DocumentBuilder builder = new DocumentBuilder(mainDoc);
|
|
|
// 为true表示文档的奇数页和偶数页有不同的页眉和页脚
|
|
|
builder.getPageSetup().setOddAndEvenPagesHeaderFooter(false);
|
|
@@ -308,7 +310,7 @@ public class WordSimpleTests {
|
|
|
// 设置页脚内容
|
|
|
builder.write("四川大友房地产土地资产评估有限公司");*/
|
|
|
|
|
|
- mainDoc.save("E:\\test\\output.docx");
|
|
|
+ mainDoc.save("E:\\test\\word\\output.docx");
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -317,7 +319,7 @@ public class WordSimpleTests {
|
|
|
*/
|
|
|
// @Test
|
|
|
void setPageNumber() throws Exception {
|
|
|
- Document doc = new Document("E:\\test\\output.docx");
|
|
|
+ Document doc = new Document("E:\\test\\word\\output.docx");
|
|
|
DocumentBuilder builder = new DocumentBuilder(doc);
|
|
|
|
|
|
//创建页脚 页码
|
|
@@ -369,7 +371,7 @@ public class WordSimpleTests {
|
|
|
|
|
|
// builder.insertField("PAGE");
|
|
|
|
|
|
- doc.save("E:\\test\\output.docx");
|
|
|
+ doc.save("E:\\test\\word\\output.docx");
|
|
|
}
|
|
|
|
|
|
}
|