|
@@ -25,12 +25,15 @@ import org.docx4j.dml.wordprocessingDrawing.Inline;
|
|
import org.docx4j.finders.ClassFinder;
|
|
import org.docx4j.finders.ClassFinder;
|
|
import org.docx4j.jaxb.Context;
|
|
import org.docx4j.jaxb.Context;
|
|
import org.docx4j.math.CTOMath;
|
|
import org.docx4j.math.CTOMath;
|
|
|
|
+import org.docx4j.math.CTOMathPara;
|
|
import org.docx4j.openpackaging.exceptions.Docx4JException;
|
|
import org.docx4j.openpackaging.exceptions.Docx4JException;
|
|
import org.docx4j.openpackaging.io3.stores.PartStore;
|
|
import org.docx4j.openpackaging.io3.stores.PartStore;
|
|
|
|
+import org.docx4j.openpackaging.io3.stores.ZipPartStore;
|
|
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
|
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
|
import org.docx4j.openpackaging.parts.Part;
|
|
import org.docx4j.openpackaging.parts.Part;
|
|
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
|
|
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
|
|
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
|
|
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
|
|
|
|
+import org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart;
|
|
import org.docx4j.openpackaging.parts.relationships.Namespaces;
|
|
import org.docx4j.openpackaging.parts.relationships.Namespaces;
|
|
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
|
|
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
|
|
import org.docx4j.relationships.Relationship;
|
|
import org.docx4j.relationships.Relationship;
|
|
@@ -53,6 +56,8 @@ import javax.xml.transform.stream.StreamResult;
|
|
import javax.xml.transform.stream.StreamSource;
|
|
import javax.xml.transform.stream.StreamSource;
|
|
import java.io.*;
|
|
import java.io.*;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
+import java.util.regex.Matcher;
|
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
|
/**
|
|
/**
|
|
* docx处理工具类 Created by songyue on 17/3/10.
|
|
* docx处理工具类 Created by songyue on 17/3/10.
|
|
@@ -127,13 +132,14 @@ public final class DocxProcessUtil {
|
|
* @param p
|
|
* @param p
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static boolean isNotText(P p) {
|
|
|
|
|
|
+ public static boolean isText(P p) {
|
|
List<Object> mathList = getAllElementFromObject(p, CTOMath.class);
|
|
List<Object> mathList = getAllElementFromObject(p, CTOMath.class);
|
|
|
|
+ List<Object> mathParaList = getAllElementFromObject(p, CTOMathPara.class);
|
|
List<Object> drawList = getAllElementFromObject(p, Drawing.class);
|
|
List<Object> drawList = getAllElementFromObject(p, Drawing.class);
|
|
- if (mathList.size() > 0 || drawList.size() > 0) {
|
|
|
|
- return true;
|
|
|
|
- } else {
|
|
|
|
|
|
+ if (mathParaList.size() > 0 || mathList.size() > 0 || drawList.size() > 0) {
|
|
return false;
|
|
return false;
|
|
|
|
+ } else {
|
|
|
|
+ return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -271,6 +277,11 @@ public final class DocxProcessUtil {
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/math", "oMath", CTOMath.class);
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/math", "oMath", CTOMath.class);
|
|
byte[] imgByte = convertMathml2Img(omml2mml(omml));
|
|
byte[] imgByte = convertMathml2Img(omml2mml(omml));
|
|
pContent.set(index, newImage(wordMLPackage, imgByte, "math" + (index), "math" + (index)));
|
|
pContent.set(index, newImage(wordMLPackage, imgByte, "math" + (index), "math" + (index)));
|
|
|
|
+ }else if(child.getClass().equals(CTOMathPara.class)){
|
|
|
|
+ String omml = XmlUtils.marshaltoString(child, true, true, Context.jc,
|
|
|
|
+ "http://schemas.openxmlformats.org/officeDocument/2006/math", "oMathPara", CTOMathPara.class);
|
|
|
|
+ byte[] imgByte = convertMathml2Img(omml2mml(omml));
|
|
|
|
+ pContent.set(index, newImage(wordMLPackage, imgByte, "math" + (index), "math" + (index)));
|
|
}
|
|
}
|
|
index++;
|
|
index++;
|
|
}
|
|
}
|
|
@@ -335,30 +346,25 @@ public final class DocxProcessUtil {
|
|
* @param omml
|
|
* @param omml
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static String omml2mml(String omml) {
|
|
|
|
|
|
+ public static String omml2mml(String omml)throws Exception {
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
- InputStream stylesheet = ClassLoader.getSystemResourceAsStream(OMML2MML_XSL);
|
|
|
|
|
|
+ InputStream stylesheet = DocxProcessUtil.class.getClassLoader().getResourceAsStream(OMML2MML_XSL);
|
|
StringReader sr = new StringReader(omml);
|
|
StringReader sr = new StringReader(omml);
|
|
InputSource ommldata = new InputSource(sr);
|
|
InputSource ommldata = new InputSource(sr);
|
|
StringWriter writer = new StringWriter();
|
|
StringWriter writer = new StringWriter();
|
|
org.w3c.dom.Document document = null;
|
|
org.w3c.dom.Document document = null;
|
|
DocumentBuilder builder = null;
|
|
DocumentBuilder builder = null;
|
|
Transformer transformer = null;
|
|
Transformer transformer = null;
|
|
- try {
|
|
|
|
- builder = factory.newDocumentBuilder();
|
|
|
|
- document = builder.parse(ommldata);
|
|
|
|
- TransformerFactory tFactory = new TransformerFactoryImpl();
|
|
|
|
- StreamSource stylesource = new StreamSource(stylesheet);
|
|
|
|
- transformer = tFactory.newTransformer(stylesource);
|
|
|
|
- DOMSource source = new DOMSource(document);
|
|
|
|
- StreamResult result = new StreamResult(writer);
|
|
|
|
- transformer.transform(source, result);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- } finally {
|
|
|
|
- IOUtils.closeQuietly(sr);
|
|
|
|
- IOUtils.closeQuietly(writer);
|
|
|
|
- }
|
|
|
|
|
|
+ builder = factory.newDocumentBuilder();
|
|
|
|
+ document = builder.parse(ommldata);
|
|
|
|
+ TransformerFactory tFactory = new TransformerFactoryImpl();
|
|
|
|
+ StreamSource stylesource = new StreamSource(stylesheet);
|
|
|
|
+ transformer = tFactory.newTransformer(stylesource);
|
|
|
|
+ DOMSource source = new DOMSource(document);
|
|
|
|
+ StreamResult result = new StreamResult(writer);
|
|
|
|
+ transformer.transform(source, result);
|
|
|
|
+ IOUtils.closeQuietly(sr);
|
|
|
|
+ IOUtils.closeQuietly(writer);
|
|
String mml = writer.toString();
|
|
String mml = writer.toString();
|
|
mml = ("<mml:math" + StringUtils.substringAfter(mml, "<mml:math")).replace("mml:", "");
|
|
mml = ("<mml:math" + StringUtils.substringAfter(mml, "<mml:math")).replace("mml:", "");
|
|
return mml;
|
|
return mml;
|
|
@@ -370,25 +376,20 @@ public final class DocxProcessUtil {
|
|
* @param htmlStr
|
|
* @param htmlStr
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static String formatHtml(String htmlStr) {
|
|
|
|
- try {
|
|
|
|
- org.jsoup.nodes.Document doc = Jsoup.parse(htmlStr);
|
|
|
|
- Elements divs = doc.select("div[class='document");
|
|
|
|
- doc.select("p").removeAttr("class").removeAttr("style");
|
|
|
|
- doc.select("div").removeAttr("class").removeAttr("style");
|
|
|
|
- doc.select("span").removeAttr("class").removeAttr("style");
|
|
|
|
- doc.select("span").stream().forEach(element -> {
|
|
|
|
- if (!element.hasText())
|
|
|
|
- element.remove();
|
|
|
|
- });
|
|
|
|
- if (divs.size() > 0) {
|
|
|
|
- htmlStr = divs.html();
|
|
|
|
- } else {
|
|
|
|
- htmlStr = doc.html();
|
|
|
|
- }
|
|
|
|
- return htmlStr;
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
|
+ public static String formatHtml(String htmlStr) throws Exception {
|
|
|
|
+ org.jsoup.nodes.Document doc = Jsoup.parse(htmlStr);
|
|
|
|
+ Elements divs = doc.select("div[class='document");
|
|
|
|
+ doc.select("p").removeAttr("class").removeAttr("style");
|
|
|
|
+ doc.select("div").removeAttr("class").removeAttr("style");
|
|
|
|
+ doc.select("span").removeAttr("class").removeAttr("style");
|
|
|
|
+ doc.select("span").stream().forEach(element -> {
|
|
|
|
+ if (!element.hasText())
|
|
|
|
+ element.remove();
|
|
|
|
+ });
|
|
|
|
+ if (divs.size() > 0) {
|
|
|
|
+ htmlStr = divs.html();
|
|
|
|
+ } else {
|
|
|
|
+ htmlStr = doc.html();
|
|
}
|
|
}
|
|
return htmlStr;
|
|
return htmlStr;
|
|
}
|
|
}
|
|
@@ -399,7 +400,7 @@ public final class DocxProcessUtil {
|
|
* @param htmlStr
|
|
* @param htmlStr
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static String getTextInHtml(String htmlStr) {
|
|
|
|
|
|
+ public static String getTextInHtml(String htmlStr){
|
|
try {
|
|
try {
|
|
org.jsoup.nodes.Document doc = Jsoup.parse(htmlStr);
|
|
org.jsoup.nodes.Document doc = Jsoup.parse(htmlStr);
|
|
String textStr = "";
|
|
String textStr = "";
|
|
@@ -420,37 +421,31 @@ public final class DocxProcessUtil {
|
|
* @param htmlPath
|
|
* @param htmlPath
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static String formatHtmlByPath(String htmlPath) {
|
|
|
|
|
|
+ public static String formatHtmlByPath(String htmlPath) throws Exception {
|
|
String htmlStr = "";
|
|
String htmlStr = "";
|
|
- File htmlFile;
|
|
|
|
- try {
|
|
|
|
- htmlFile = new File(htmlPath);
|
|
|
|
- org.jsoup.nodes.Document doc = Jsoup.parse(htmlFile, ENCODING);
|
|
|
|
- Elements divs = doc.select("div[class='document']");
|
|
|
|
- doc.select("p").removeAttr("class").removeAttr("style");
|
|
|
|
- doc.select("div").removeAttr("class").removeAttr("style");
|
|
|
|
- doc.select("span").removeAttr("class").removeAttr("style");
|
|
|
|
- doc.select("span").stream().forEach(element -> {
|
|
|
|
- if (!element.hasText() && element.childNodeSize() == 0) {
|
|
|
|
- element.remove();
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- Elements imgs = doc.select("img");
|
|
|
|
- for (org.jsoup.nodes.Element img : imgs) {
|
|
|
|
- String imgSrc = img.attr("src");
|
|
|
|
- if (!StringUtils.isEmpty(imgSrc)) {
|
|
|
|
- String imgBase64 = new String(getBase64ByPath(htmlFile.getParent() + "/" + imgSrc));
|
|
|
|
- img.attr("src", BASE64_HEADER + imgBase64);
|
|
|
|
- }
|
|
|
|
|
|
+ File htmlFile = new File(htmlPath);
|
|
|
|
+ org.jsoup.nodes.Document doc = Jsoup.parse(htmlFile, ENCODING);
|
|
|
|
+ Elements divs = doc.select("div[class='document']");
|
|
|
|
+ doc.select("p").removeAttr("class").removeAttr("style");
|
|
|
|
+ doc.select("div").removeAttr("class").removeAttr("style");
|
|
|
|
+ doc.select("span").removeAttr("class").removeAttr("style");
|
|
|
|
+ doc.select("span").stream().forEach(element -> {
|
|
|
|
+ if (!element.hasText() && element.childNodeSize() == 0) {
|
|
|
|
+ element.remove();
|
|
}
|
|
}
|
|
- if (divs.size() > 0) {
|
|
|
|
- htmlStr = divs.html();
|
|
|
|
- } else {
|
|
|
|
- htmlStr = doc.html();
|
|
|
|
|
|
+ });
|
|
|
|
+ Elements imgs = doc.select("img");
|
|
|
|
+ for (org.jsoup.nodes.Element img : imgs) {
|
|
|
|
+ String imgSrc = img.attr("src");
|
|
|
|
+ if (!StringUtils.isEmpty(imgSrc)) {
|
|
|
|
+ String imgBase64 = new String(getBase64ByPath(htmlFile.getParent() + "/" + imgSrc));
|
|
|
|
+ img.attr("src", BASE64_HEADER + imgBase64);
|
|
}
|
|
}
|
|
- return htmlStr;
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
|
+ }
|
|
|
|
+ if (divs.size() > 0) {
|
|
|
|
+ htmlStr = divs.html();
|
|
|
|
+ } else {
|
|
|
|
+ htmlStr = doc.html();
|
|
}
|
|
}
|
|
return htmlStr;
|
|
return htmlStr;
|
|
}
|
|
}
|
|
@@ -461,26 +456,21 @@ public final class DocxProcessUtil {
|
|
* @param mathMlStr
|
|
* @param mathMlStr
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static byte[] convertMathml2Img(String mathMlStr) {
|
|
|
|
|
|
+ public static byte[] convertMathml2Img(String mathMlStr) throws Exception {
|
|
File xmlFile = null;
|
|
File xmlFile = null;
|
|
File imgFile = null;
|
|
File imgFile = null;
|
|
FileOutputStream xmlFos = null;
|
|
FileOutputStream xmlFos = null;
|
|
byte[] base64Byte = new byte[0];
|
|
byte[] base64Byte = new byte[0];
|
|
- try {
|
|
|
|
- xmlFile = new File(TEMP_FILE_IMP, UUID.randomUUID().toString());
|
|
|
|
- imgFile = new File(TEMP_FILE_IMP, UUID.randomUUID().toString());
|
|
|
|
- xmlFos = new FileOutputStream(xmlFile);
|
|
|
|
- IOUtils.write(mathMlStr, xmlFos, ENCODING);
|
|
|
|
- xmlFos.flush();
|
|
|
|
- Converter.getInstance().convert(xmlFile, imgFile, IMG_OUT_TYPE, IMG_LAYOUT);
|
|
|
|
- base64Byte = IOUtils.toByteArray(new FileInputStream(imgFile));
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- } finally {
|
|
|
|
- IOUtils.closeQuietly(xmlFos);
|
|
|
|
- FileUtils.deleteQuietly(xmlFile);
|
|
|
|
- FileUtils.deleteQuietly(imgFile);
|
|
|
|
- }
|
|
|
|
|
|
+ xmlFile = new File(TEMP_FILE_IMP, UUID.randomUUID().toString());
|
|
|
|
+ imgFile = new File(TEMP_FILE_IMP, UUID.randomUUID().toString());
|
|
|
|
+ xmlFos = new FileOutputStream(xmlFile);
|
|
|
|
+ IOUtils.write(mathMlStr, xmlFos, ENCODING);
|
|
|
|
+ xmlFos.flush();
|
|
|
|
+ Converter.getInstance().convert(xmlFile, imgFile, IMG_OUT_TYPE, IMG_LAYOUT);
|
|
|
|
+ base64Byte = IOUtils.toByteArray(new FileInputStream(imgFile));
|
|
|
|
+ IOUtils.closeQuietly(xmlFos);
|
|
|
|
+ FileUtils.deleteQuietly(xmlFile);
|
|
|
|
+ FileUtils.deleteQuietly(imgFile);
|
|
return base64Byte;
|
|
return base64Byte;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -490,21 +480,16 @@ public final class DocxProcessUtil {
|
|
* @param imgFilePath
|
|
* @param imgFilePath
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static byte[] getBase64ByPath(String imgFilePath) {
|
|
|
|
|
|
+ public static byte[] getBase64ByPath(String imgFilePath) throws Exception {
|
|
InputStream is = null;
|
|
InputStream is = null;
|
|
byte[] base64Byte = new byte[0];
|
|
byte[] base64Byte = new byte[0];
|
|
byte[] imgByte;
|
|
byte[] imgByte;
|
|
File imgFile = new File(imgFilePath);
|
|
File imgFile = new File(imgFilePath);
|
|
- try {
|
|
|
|
- is = new FileInputStream(imgFile);
|
|
|
|
- imgByte = IOUtils.toByteArray(is);
|
|
|
|
- base64Byte = Base64.encodeBase64(imgByte);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- } finally {
|
|
|
|
- IOUtils.closeQuietly(is);
|
|
|
|
- FileUtils.deleteQuietly(imgFile);
|
|
|
|
- }
|
|
|
|
|
|
+ is = new FileInputStream(imgFile);
|
|
|
|
+ imgByte = IOUtils.toByteArray(is);
|
|
|
|
+ base64Byte = Base64.encodeBase64(imgByte);
|
|
|
|
+ IOUtils.closeQuietly(is);
|
|
|
|
+ FileUtils.deleteQuietly(imgFile);
|
|
return base64Byte;
|
|
return base64Byte;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -514,19 +499,14 @@ public final class DocxProcessUtil {
|
|
* @param imgFile
|
|
* @param imgFile
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static byte[] getBase64ByFile(File imgFile) {
|
|
|
|
|
|
+ public static byte[] getBase64ByFile(File imgFile) throws Exception {
|
|
InputStream is = null;
|
|
InputStream is = null;
|
|
byte[] base64Byte = new byte[0];
|
|
byte[] base64Byte = new byte[0];
|
|
byte[] imgByte;
|
|
byte[] imgByte;
|
|
- try {
|
|
|
|
- is = new FileInputStream(imgFile);
|
|
|
|
- imgByte = IOUtils.toByteArray(is);
|
|
|
|
- base64Byte = Base64.encodeBase64(imgByte);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- } finally {
|
|
|
|
- IOUtils.closeQuietly(is);
|
|
|
|
- }
|
|
|
|
|
|
+ is = new FileInputStream(imgFile);
|
|
|
|
+ imgByte = IOUtils.toByteArray(is);
|
|
|
|
+ base64Byte = Base64.encodeBase64(imgByte);
|
|
|
|
+ IOUtils.closeQuietly(is);
|
|
return base64Byte;
|
|
return base64Byte;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -560,7 +540,6 @@ public final class DocxProcessUtil {
|
|
public static P formatP(P p, QuesUnit quesUnit) {
|
|
public static P formatP(P p, QuesUnit quesUnit) {
|
|
List<Object> pContent = p.getContent();
|
|
List<Object> pContent = p.getContent();
|
|
int index = 0;
|
|
int index = 0;
|
|
-
|
|
|
|
for (Object child : pContent) {
|
|
for (Object child : pContent) {
|
|
if (child.getClass().equals(R.class)) {
|
|
if (child.getClass().equals(R.class)) {
|
|
|
|
|
|
@@ -569,54 +548,60 @@ public final class DocxProcessUtil {
|
|
|
|
|
|
for (Object rChild : rContent) {
|
|
for (Object rChild : rContent) {
|
|
|
|
|
|
- rChild = ((JAXBElement<?>) rChild).getValue();
|
|
|
|
|
|
+ if (rChild instanceof JAXBElement)
|
|
|
|
+ rChild = ((JAXBElement<?>) rChild).getValue();
|
|
|
|
|
|
if (rChild.getClass().equals(Text.class)) {
|
|
if (rChild.getClass().equals(Text.class)) {
|
|
++index;
|
|
++index;
|
|
Text text = (Text) rChild;
|
|
Text text = (Text) rChild;
|
|
String tmpText = text.getValue();
|
|
String tmpText = text.getValue();
|
|
if (quesUnit == QuesUnit.QUES_BODY) {
|
|
if (quesUnit == QuesUnit.QUES_BODY) {
|
|
|
|
+
|
|
// 过滤题干标题
|
|
// 过滤题干标题
|
|
if (tmpText.matches("^\\d{1,}\\.[\\s\\S]*")) {
|
|
if (tmpText.matches("^\\d{1,}\\.[\\s\\S]*")) {
|
|
- tmpText = tmpText.replaceFirst("\\d{1,}\\.", "");
|
|
|
|
- text.setValue(tmpText);
|
|
|
|
- } else {
|
|
|
|
- if (index == 1) {
|
|
|
|
- tmpText = tmpText.replaceFirst("\\d{1,}", "");
|
|
|
|
- } else if (index == 2) {
|
|
|
|
- tmpText = tmpText.replaceFirst("\\.", "");
|
|
|
|
- }
|
|
|
|
- text.setValue(tmpText);
|
|
|
|
if (index == 2) {
|
|
if (index == 2) {
|
|
- break;
|
|
|
|
|
|
+ return p;
|
|
}
|
|
}
|
|
|
|
+ tmpText = tmpText.replaceFirst("\\d{1,}\\.","");
|
|
|
|
+ text.setValue(tmpText);
|
|
|
|
+ } else if(tmpText.matches("^\\d{1,}$")){
|
|
|
|
+ text.setValue("");
|
|
|
|
+ } else if(tmpText.contains(".")){
|
|
|
|
+ text.setValue("");
|
|
|
|
+ return p;
|
|
}
|
|
}
|
|
|
|
|
|
} else if (quesUnit == QuesUnit.QUES_OPTION) {
|
|
} else if (quesUnit == QuesUnit.QUES_OPTION) {
|
|
|
|
+
|
|
// 过滤选项标题
|
|
// 过滤选项标题
|
|
if (tmpText.matches("^[A-Z]\\.[\\s\\S]*")) {
|
|
if (tmpText.matches("^[A-Z]\\.[\\s\\S]*")) {
|
|
- tmpText = tmpText.replaceFirst("[A-Z]\\.", "");
|
|
|
|
- text.setValue(tmpText);
|
|
|
|
- } else {
|
|
|
|
- if (index == 1) {
|
|
|
|
- tmpText = tmpText.replaceFirst("[A-Z]", "");
|
|
|
|
- } else if (index == 2) {
|
|
|
|
- tmpText = tmpText.replaceFirst("\\.", "");
|
|
|
|
- }
|
|
|
|
- text.setValue(tmpText);
|
|
|
|
if (index == 2) {
|
|
if (index == 2) {
|
|
- break;
|
|
|
|
|
|
+ return p;
|
|
}
|
|
}
|
|
|
|
+ tmpText = tmpText.replaceFirst("[A-Z]\\.","");
|
|
|
|
+ text.setValue(tmpText);
|
|
|
|
+ } else if(tmpText.matches("^[A-Z]$")){
|
|
|
|
+ text.setValue("");
|
|
|
|
+ } else if(tmpText.contains(".")){
|
|
|
|
+ text.setValue("");
|
|
|
|
+ return p;
|
|
}
|
|
}
|
|
|
|
|
|
} else if (quesUnit == QuesUnit.QUES_ANSWER) {
|
|
} else if (quesUnit == QuesUnit.QUES_ANSWER) {
|
|
|
|
+
|
|
// 过滤答案标题
|
|
// 过滤答案标题
|
|
- if (index <= 4) {
|
|
|
|
- tmpText = tmpText.replaceFirst("\\[", "").replaceFirst("\\]", "").replaceFirst("答案", "")
|
|
|
|
- .replaceFirst("[:|:]", "");
|
|
|
|
- text.setValue(tmpText);
|
|
|
|
|
|
+ if(tmpText.contains(":") || tmpText.contains(":")){
|
|
|
|
+ if(tmpText.startsWith(":") || tmpText.startsWith(":")){
|
|
|
|
+ tmpText = tmpText.replaceFirst("[:|:]","");
|
|
|
|
+ text.setValue(tmpText);
|
|
|
|
+ }else{
|
|
|
|
+ tmpText = tmpText.replaceFirst("\\[","").replaceFirst("\\]","").replaceFirst("答案","");
|
|
|
|
+ text.setValue(tmpText);
|
|
|
|
+ }
|
|
|
|
+ return p;
|
|
} else {
|
|
} else {
|
|
- break;
|
|
|
|
|
|
+ tmpText = tmpText.replaceFirst("\\[","").replaceFirst("\\]","").replaceFirst("答案","");
|
|
|
|
+ text.setValue(tmpText);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -632,32 +617,27 @@ public final class DocxProcessUtil {
|
|
* @param dataMap
|
|
* @param dataMap
|
|
* @param fileName
|
|
* @param fileName
|
|
*/
|
|
*/
|
|
- public static void exportWord(Map dataMap, String fileName, String templatePath) {
|
|
|
|
|
|
+ public static void exportWord(Map dataMap, String fileName, String templatePath)throws Exception {
|
|
Writer out = null;
|
|
Writer out = null;
|
|
- try {
|
|
|
|
- // 创建配置实例
|
|
|
|
- Configuration configuration = new Configuration(Configuration.VERSION_2_3_25);
|
|
|
|
- // 设置编码
|
|
|
|
- configuration.setDefaultEncoding("UTF-8");
|
|
|
|
- // 设置ftl模板路径
|
|
|
|
- configuration.setClassForTemplateLoading(DocxProcessUtil.class, "/");
|
|
|
|
- // 获取模板
|
|
|
|
- Template template = configuration.getTemplate(templatePath, ENCODING);
|
|
|
|
- // 输出文件
|
|
|
|
- File outFile = new File(TEMP_FILE_EXP + fileName + DOCX_SUFFIX);
|
|
|
|
-
|
|
|
|
- // 将模板和数据模型合并生成文件
|
|
|
|
- out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), ENCODING));
|
|
|
|
- // 生成文件
|
|
|
|
- template.process(dataMap, out);
|
|
|
|
-
|
|
|
|
- out.flush();
|
|
|
|
-
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- } finally {
|
|
|
|
- IOUtils.closeQuietly(out);
|
|
|
|
- }
|
|
|
|
|
|
+ // 创建配置实例
|
|
|
|
+ Configuration configuration = new Configuration(Configuration.VERSION_2_3_25);
|
|
|
|
+ // 设置编码
|
|
|
|
+ configuration.setDefaultEncoding("UTF-8");
|
|
|
|
+ // 设置ftl模板路径
|
|
|
|
+ configuration.setClassForTemplateLoading(DocxProcessUtil.class, "/");
|
|
|
|
+ // 获取模板
|
|
|
|
+ Template template = configuration.getTemplate(templatePath, ENCODING);
|
|
|
|
+ // 输出文件
|
|
|
|
+ File outFile = new File(TEMP_FILE_EXP + fileName + DOCX_SUFFIX);
|
|
|
|
+
|
|
|
|
+ // 将模板和数据模型合并生成文件
|
|
|
|
+ out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), ENCODING));
|
|
|
|
+ // 生成文件
|
|
|
|
+ template.process(dataMap, out);
|
|
|
|
+
|
|
|
|
+ out.flush();
|
|
|
|
+
|
|
|
|
+ IOUtils.closeQuietly(out);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -666,7 +646,7 @@ public final class DocxProcessUtil {
|
|
* @param dataMap
|
|
* @param dataMap
|
|
* @param fileName
|
|
* @param fileName
|
|
*/
|
|
*/
|
|
- public static void exportPaper(Map dataMap, String fileName) {
|
|
|
|
|
|
+ public static void exportPaper(Map dataMap, String fileName) throws Exception {
|
|
exportWord(dataMap, fileName, PAPER_TEMPLATE);
|
|
exportWord(dataMap, fileName, PAPER_TEMPLATE);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -676,7 +656,7 @@ public final class DocxProcessUtil {
|
|
* @param dataMap
|
|
* @param dataMap
|
|
* @param fileName
|
|
* @param fileName
|
|
*/
|
|
*/
|
|
- public static void exportAnswer(Map dataMap, String fileName) {
|
|
|
|
|
|
+ public static void exportAnswer(Map dataMap, String fileName) throws Exception {
|
|
exportWord(dataMap, fileName, ANSWER_TEMPLATE);
|
|
exportWord(dataMap, fileName, ANSWER_TEMPLATE);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -694,10 +674,12 @@ public final class DocxProcessUtil {
|
|
// 以单个wordXml方式解析freemarker导出的文件
|
|
// 以单个wordXml方式解析freemarker导出的文件
|
|
FlatOpcXmlImporter flatOpcXmlImporter = new FlatOpcXmlImporter(mainFile);
|
|
FlatOpcXmlImporter flatOpcXmlImporter = new FlatOpcXmlImporter(mainFile);
|
|
WordprocessingMLPackage wordMLPackage = (WordprocessingMLPackage) flatOpcXmlImporter.get();
|
|
WordprocessingMLPackage wordMLPackage = (WordprocessingMLPackage) flatOpcXmlImporter.get();
|
|
|
|
+ RelationshipsPart relationshipsPart = wordMLPackage.getMainDocumentPart().getRelationshipsPart();
|
|
|
|
|
|
for (WordprocessingMLPackage wp : wordMLPackages) {
|
|
for (WordprocessingMLPackage wp : wordMLPackages) {
|
|
|
|
+
|
|
// 获取资源文件存储
|
|
// 获取资源文件存储
|
|
- PartStore partStore = wp.getSourcePartStore();
|
|
|
|
|
|
+ ZipPartStore partStore = (ZipPartStore) wp.getSourcePartStore();
|
|
// 获取图片资源定义
|
|
// 获取图片资源定义
|
|
RelationshipsPart rp = wp.getMainDocumentPart().getRelationshipsPart();
|
|
RelationshipsPart rp = wp.getMainDocumentPart().getRelationshipsPart();
|
|
List<Relationship> rels = rp.getRelationshipsByType(Namespaces.IMAGE);
|
|
List<Relationship> rels = rp.getRelationshipsByType(Namespaces.IMAGE);
|
|
@@ -706,13 +688,15 @@ public final class DocxProcessUtil {
|
|
for (Relationship relationship : rels) {
|
|
for (Relationship relationship : rels) {
|
|
parts.add(rp.getPart(relationship));
|
|
parts.add(rp.getPart(relationship));
|
|
}
|
|
}
|
|
- // 添加资源文件存储
|
|
|
|
- wordMLPackage.setSourcePartStore(partStore);
|
|
|
|
// 添加资源文件定义
|
|
// 添加资源文件定义
|
|
for (Part p : parts) {
|
|
for (Part p : parts) {
|
|
- wordMLPackage.getMainDocumentPart().addTargetPart(p, RelationshipsPart.AddPartBehaviour.REUSE_EXISTING,
|
|
|
|
- p.getSourceRelationship().getId());
|
|
|
|
|
|
+ String relId = p.getSourceRelationships().get(0).getId();
|
|
|
|
+ if(!relationshipsPart.isRelIdOccupied(relId)){
|
|
|
|
+ copyImage(wordMLPackage,partStore,p);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+ // 添加资源文件存储
|
|
|
|
+// wordMLPackage.setSourcePartStore(partStore);
|
|
}
|
|
}
|
|
// 以word2007标准模式重新保存(zip包)
|
|
// 以word2007标准模式重新保存(zip包)
|
|
OutputStream os = new java.io.FileOutputStream(filePath);
|
|
OutputStream os = new java.io.FileOutputStream(filePath);
|
|
@@ -720,6 +704,19 @@ public final class DocxProcessUtil {
|
|
IOUtils.closeQuietly(os);
|
|
IOUtils.closeQuietly(os);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * word合并时复制图片
|
|
|
|
+ * @param wordMLPackage
|
|
|
|
+ * @param partStore
|
|
|
|
+ * @param p
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public static void copyImage(WordprocessingMLPackage wordMLPackage, ZipPartStore partStore, Part p)throws Exception{
|
|
|
|
+ byte [] bytes = partStore.getByteArray(p.getPartName().getName().substring(1)).getBytes();
|
|
|
|
+ BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
|
|
|
|
+ imagePart.getRelLast().setId(p.getSourceRelationships().get(0).getId());
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 获取word二进制数据(空文档,只有样式和资源)
|
|
* 获取word二进制数据(空文档,只有样式和资源)
|
|
*
|
|
*
|
|
@@ -774,15 +771,14 @@ public final class DocxProcessUtil {
|
|
.getRelationshipsByType(Namespaces.IMAGE);
|
|
.getRelationshipsByType(Namespaces.IMAGE);
|
|
// 同步替换资源ID
|
|
// 同步替换资源ID
|
|
int index = 0;
|
|
int index = 0;
|
|
- String rldHeader = getRldNum();
|
|
|
|
for (Relationship relationship : relationships) {
|
|
for (Relationship relationship : relationships) {
|
|
String tmpId = relationship.getId();
|
|
String tmpId = relationship.getId();
|
|
|
|
+ String tmp = getRldNum() + (++index);
|
|
for (Object obj : blips) {
|
|
for (Object obj : blips) {
|
|
if (obj.getClass().equals(CTBlip.class)) {
|
|
if (obj.getClass().equals(CTBlip.class)) {
|
|
CTBlip ctBlip = (CTBlip) obj;
|
|
CTBlip ctBlip = (CTBlip) obj;
|
|
String tmpEmbed = ctBlip.getEmbed();
|
|
String tmpEmbed = ctBlip.getEmbed();
|
|
if (tmpId.equals(tmpEmbed)) {
|
|
if (tmpId.equals(tmpEmbed)) {
|
|
- String tmp = rldHeader + (++index);
|
|
|
|
relationship.setId(tmp);
|
|
relationship.setId(tmp);
|
|
ctBlip.setEmbed(tmp);
|
|
ctBlip.setEmbed(tmp);
|
|
}
|
|
}
|
|
@@ -824,20 +820,101 @@ public final class DocxProcessUtil {
|
|
* @return
|
|
* @return
|
|
* @throws Exception
|
|
* @throws Exception
|
|
*/
|
|
*/
|
|
- public static String html2Docx(WordprocessingMLPackage wordMLPackage,String html)throws Exception{
|
|
|
|
- initPkgImage(wordMLPackage);
|
|
|
|
|
|
+ public static String html2Docx(WordprocessingMLPackage wordMLPackage,String html)throws Exception {
|
|
|
|
+ initTmpPackage(wordMLPackage);
|
|
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
|
|
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
|
|
String wordMl = "";
|
|
String wordMl = "";
|
|
wordMLPackage.getMainDocumentPart().getContent().addAll(
|
|
wordMLPackage.getMainDocumentPart().getContent().addAll(
|
|
- XHTMLImporter.convert( html, null) );
|
|
|
|
|
|
+ XHTMLImporter.convert( repairHtmlStr(html), null) );
|
|
// 获取word文档中所有段落
|
|
// 获取word文档中所有段落
|
|
List<Object> pList = getAllElementFromObject(wordMLPackage.getMainDocumentPart(), P.class);
|
|
List<Object> pList = getAllElementFromObject(wordMLPackage.getMainDocumentPart(), P.class);
|
|
for(Object p:pList){
|
|
for(Object p:pList){
|
|
wordMl += XmlUtils.marshaltoString(p);
|
|
wordMl += XmlUtils.marshaltoString(p);
|
|
}
|
|
}
|
|
// wordMl = formatPWordMl(wordMl);
|
|
// wordMl = formatPWordMl(wordMl);
|
|
|
|
+ initPkgImage(wordMLPackage);
|
|
return wordMl;
|
|
return wordMl;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static String repairHtmlStr(String htmlStr){
|
|
|
|
+ htmlStr = htmlStr.trim();
|
|
|
|
+ if(htmlStr.toLowerCase().contains("<!doctype html ")){
|
|
|
|
+ int index1 = htmlStr.toLowerCase().indexOf("<!doctype html ");
|
|
|
|
+ int index2 = htmlStr.indexOf('>',index1 + 1);
|
|
|
|
+ htmlStr = htmlStr.substring(0, index1) + htmlStr.substring(index2 + 1);
|
|
|
|
+ }
|
|
|
|
+ while(htmlStr.toLowerCase().contains("<br ")){
|
|
|
|
+ int index1 = htmlStr.toLowerCase().indexOf("<br ");
|
|
|
|
+ int index2 = htmlStr.toLowerCase().indexOf(">",index1 + 1);
|
|
|
|
+ htmlStr = htmlStr.substring(0, index1) + "<br/>" + htmlStr.substring(index2 + 1);
|
|
|
|
+ }
|
|
|
|
+ while(htmlStr.toLowerCase().endsWith("<br>") || htmlStr.toLowerCase().endsWith("<br/>")){
|
|
|
|
+ if(htmlStr.toLowerCase().endsWith("<br>")){
|
|
|
|
+ htmlStr = htmlStr.substring(0, htmlStr.length()-"<br>".length());
|
|
|
|
+ }else if(htmlStr.toLowerCase().endsWith("<br/>")){
|
|
|
|
+ htmlStr = htmlStr.substring(0, htmlStr.length()-"<br/>".length());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ htmlStr = htmlStr.replace("<br>", "<br/>").replace("<BR>", "<br/>");
|
|
|
|
+
|
|
|
|
+ {//补全META标签
|
|
|
|
+ int imgIndex = indexOfRegex(htmlStr,"<((meta)|(META)) ");
|
|
|
|
+ while(imgIndex > 0){
|
|
|
|
+ int flag = htmlStr.indexOf(">", imgIndex);
|
|
|
|
+ if(htmlStr.charAt(flag - 1) != '/'){
|
|
|
|
+ htmlStr = htmlStr.substring(0,flag) + "/" + htmlStr.substring(flag);
|
|
|
|
+ }
|
|
|
|
+ imgIndex = indexOfRegex(htmlStr,"<((meta)|(META)) ",flag);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ {//补全img标签
|
|
|
|
+ int imgIndex = indexOfRegex(htmlStr,"<((img)|(IMG)) ");
|
|
|
|
+ while(imgIndex > 0){
|
|
|
|
+ int flag = htmlStr.indexOf(">", imgIndex);
|
|
|
|
+ if(htmlStr.charAt(flag - 1) != '/'){
|
|
|
|
+ htmlStr = htmlStr.substring(0,flag) + "/" + htmlStr.substring(flag);
|
|
|
|
+ }
|
|
|
|
+ imgIndex = indexOfRegex(htmlStr,"<((img)|(IMG)) ",flag);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return htmlStr;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 从指定的位置开始查找第一个匹配正则表达式的字符串的位置
|
|
|
|
+ * @param str
|
|
|
|
+ * @param regex 正则表达式
|
|
|
|
+ * @param fromIndex 指定的起始位置
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static int indexOfRegex(String str,String regex,int fromIndex){
|
|
|
|
+ int index = indexOfRegex(str.substring(fromIndex),regex);
|
|
|
|
+ if(index < 0){
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+ return fromIndex + index;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 查找第一个匹配正则表达式的字符串的位置
|
|
|
|
+ * @param str
|
|
|
|
+ * @param regex 正则表达式
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static int indexOfRegex(String str,String regex){
|
|
|
|
+ Pattern p = Pattern.compile(regex);
|
|
|
|
+ Matcher m = p.matcher(str);
|
|
|
|
+ if(m.find()){
|
|
|
|
+ return m.start();
|
|
|
|
+ }else{
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ for(QuesUnit quesUnit:QuesUnit.values()){
|
|
|
|
+ System.out.println(quesUnit.name()+" "+quesUnit.getName());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|