Skip to content

Commit

Permalink
API修改
Browse files Browse the repository at this point in the history
  • Loading branch information
izhangzhihao committed Nov 16, 2016
1 parent 999cc56 commit d0e587a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,20 @@ public class DocxProducer {
*/
private static WordprocessingMLPackage CreateWordprocessingMLPackageFromTemplate(String templatePath,
HashMap<String, String> parameters,
HashMap<DataFieldName, String> bookMarkParameters,
HashMap<String, String> imageParameters)
throws Exception {
@Cleanup InputStream docxStream = DocxProducer.class.getResourceAsStream(templatePath);
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(docxStream);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

//第一步 替换字符参数
replaceParameters(documentPart, parameters);
if (parameters != null) {
replaceParameters(documentPart, parameters);
} else {
//或者替换书签为文字
replaceBookmarkContents(documentPart, bookMarkParameters);
}

//第二步 插入图片
replaceBookMarkWithImage(wordMLPackage, documentPart, imageParameters);
Expand All @@ -70,10 +76,11 @@ private static WordprocessingMLPackage CreateWordprocessingMLPackageFromTemplate
*/
public static void CreateDocxFromTemplate(String templatePath,
HashMap<String, String> parameters,
HashMap<DataFieldName, String> bookMarkParameters,
HashMap<String, String> imageParameters,
String savePath)
throws Exception {
WordprocessingMLPackage wordMLPackage = CreateWordprocessingMLPackageFromTemplate(templatePath, parameters, imageParameters);
WordprocessingMLPackage wordMLPackage = CreateWordprocessingMLPackageFromTemplate(templatePath, parameters, bookMarkParameters, imageParameters);

//保存
saveDocx(wordMLPackage, savePath);
Expand All @@ -91,11 +98,12 @@ public static void CreateDocxFromTemplate(String templatePath,
*/
public static void CreateEncryptDocxFromTemplate(String templatePath,
HashMap<String, String> parameters,
HashMap<DataFieldName, String> bookMarkParameters,
HashMap<String, String> imageParameters,
String savePath,
String passWord)
throws Exception {
WordprocessingMLPackage wordMLPackage = CreateWordprocessingMLPackageFromTemplate(templatePath, parameters, imageParameters);
WordprocessingMLPackage wordMLPackage = CreateWordprocessingMLPackageFromTemplate(templatePath, parameters, bookMarkParameters, imageParameters);

//加密
ProtectDocument protection = new ProtectDocument(wordMLPackage);
Expand All @@ -116,10 +124,11 @@ public static void CreateEncryptDocxFromTemplate(String templatePath,
*/
public static void CreatePDFFromDocxTemplate(String templatePath,
HashMap<String, String> parameters,
HashMap<DataFieldName, String> bookMarkParameters,
HashMap<String, String> imageParameters,
String savePath)
throws Exception {
WordprocessingMLPackage wordMLPackage = CreateWordprocessingMLPackageFromTemplate(templatePath, parameters, imageParameters);
WordprocessingMLPackage wordMLPackage = CreateWordprocessingMLPackageFromTemplate(templatePath, parameters, bookMarkParameters, imageParameters);

//转化成PDF
convertDocxToPDF(wordMLPackage, savePath);
Expand All @@ -146,7 +155,17 @@ private static void saveDocx(WordprocessingMLPackage wordMLPackage,
private static void replaceParameters(MainDocumentPart documentPart,
HashMap<String, String> parameters)
throws JAXBException, Docx4JException {
// Approach 1 (from 3.0.0; faster if you haven't yet caused unmarshalling to occur):
documentPart.variableReplace(parameters);

// Approach 2 (original)

// unmarshallFromTemplate requires string input
/*String xml = XmlUtils.marshaltoString(documentPart.getContents(), true);
// Do it...
Object obj = XmlUtils.unmarshallFromTemplate(xml, parameters);
// Inject result into docx
documentPart.setJaxbElement((Document) obj);*/
}

/**
Expand Down Expand Up @@ -250,7 +269,6 @@ private static void replaceBookmarkContents(MainDocumentPart documentPart, Map<D
// since in the parent, it may be wrapped in a JAXBElement
List<Object> theList = null;
if (bm.getParent() instanceof P) {
System.out.println("OK!");
theList = ((ContentAccessor) (bm.getParent())).getContent();
} else {
continue;
Expand Down Expand Up @@ -300,8 +318,5 @@ private static void replaceBookmarkContents(MainDocumentPart documentPart, Map<D
log.error(cce.getMessage(), cce);
}
}


}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.izhangzhihao.OfficeProducer;

import org.docx4j.model.fields.merge.DataFieldName;
import org.junit.Test;

import java.util.HashMap;
Expand All @@ -21,6 +22,11 @@ public void CreateEncryptDocxFromTemplateTest() throws Exception {
HashMap<String, String> imageParameters = new HashMap<>();
String prefix = "D:/头像/";
imageParameters.put("bookmark", prefix + "/33.png");
CreateEncryptDocxFromTemplate(templatePath, parameters, imageParameters, "D:/Desktop/test.docx", UUID.randomUUID().toString());

HashMap<DataFieldName, String> map = new HashMap<>();
map.put(new DataFieldName("projectName"), "校级项目");


CreateEncryptDocxFromTemplate(templatePath, parameters, null, imageParameters, "D:/Desktop/test.docx", UUID.randomUUID().toString());
}
}

0 comments on commit d0e587a

Please sign in to comment.