|
@@ -31,6 +31,7 @@ import org.docx4j.openpackaging.exceptions.Docx4JException;
|
|
import org.docx4j.openpackaging.io3.stores.ZipPartStore;
|
|
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.BinaryPart;
|
|
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.relationships.Namespaces;
|
|
import org.docx4j.openpackaging.parts.relationships.Namespaces;
|
|
@@ -691,6 +692,19 @@ public final class DocxProcessUtil {
|
|
IOUtils.closeQuietly(out);
|
|
IOUtils.closeQuietly(out);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static boolean hasImage(String html) throws Exception {
|
|
|
|
+ if (html == null) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ String reg = "<\\s*img\\s+([^>]*)\\s*>";
|
|
|
|
+ Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
|
|
|
|
+ Matcher matcher = pattern.matcher(html);
|
|
|
|
+ if (matcher.find()) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 处理导出word中的图片
|
|
* 处理导出word中的图片
|
|
*
|
|
*
|
|
@@ -726,7 +740,16 @@ public final class DocxProcessUtil {
|
|
String relId = p.getSourceRelationships().get(0).getId();
|
|
String relId = p.getSourceRelationships().get(0).getId();
|
|
//如果不存在该relId,拷贝图片
|
|
//如果不存在该relId,拷贝图片
|
|
if (!relationshipsPart.isRelIdOccupied(relId)) {
|
|
if (!relationshipsPart.isRelIdOccupied(relId)) {
|
|
- copyImage(wordMLPackage, partStore, p, relId);
|
|
|
|
|
|
+ byte[] bytes = null;
|
|
|
|
+ if (partStore != null) {
|
|
|
|
+ bytes = partStore.getByteArray(p.getPartName().getName().substring(1)).getBytes();
|
|
|
|
+ } else {
|
|
|
|
+ if (p instanceof BinaryPart) {
|
|
|
|
+ BinaryPart imagePart = (BinaryPart) p;
|
|
|
|
+ bytes = imagePart.getBytes();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ copyImage(wordMLPackage, bytes, relId);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// 添加资源文件存储
|
|
// 添加资源文件存储
|
|
@@ -739,6 +762,18 @@ public final class DocxProcessUtil {
|
|
logger.info("试卷:“" + fileName + "”导出处理图片结束");
|
|
logger.info("试卷:“" + fileName + "”导出处理图片结束");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static void copyImage(WordprocessingMLPackage wordMLPackage, byte[] bytes, String relId) throws Exception {
|
|
|
|
+ if (bytes == null || bytes.length == 0) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
|
|
|
|
+ BufferedImage bi1 = ImageIO.read(bais);
|
|
|
|
+ if (bi1 != null) {
|
|
|
|
+ BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
|
|
|
|
+ imagePart.getRelLast().setId(relId);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* word合并时复制图片
|
|
* word合并时复制图片
|
|
* 将partStore中的Relationship复制到wordMLPackage中
|
|
* 将partStore中的Relationship复制到wordMLPackage中
|