|
@@ -65,7 +65,9 @@ import java.io.ByteArrayInputStream;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.OutputStream;
|
|
|
import java.io.OutputStreamWriter;
|
|
@@ -701,30 +703,31 @@ public final class DocxProcessUtil {
|
|
|
logger.info("试卷:“"+fileName+"”导出处理图片开始...");
|
|
|
String filePath = TEMP_FILE_EXP + fileName;
|
|
|
InputStream mainFile = new FileInputStream(filePath);
|
|
|
- // 以单个wordXml方式解析freemarker导出的文件
|
|
|
+ // 取到当前生成的主Word
|
|
|
FlatOpcXmlImporter flatOpcXmlImporter = new FlatOpcXmlImporter(mainFile);
|
|
|
WordprocessingMLPackage wordMLPackage = (WordprocessingMLPackage) flatOpcXmlImporter.get();
|
|
|
+ //构建relationships
|
|
|
RelationshipsPart relationshipsPart = wordMLPackage.getMainDocumentPart().getRelationshipsPart();
|
|
|
-
|
|
|
for (WordprocessingMLPackage wp : wordMLPackages) {
|
|
|
if(wp==null){
|
|
|
continue;
|
|
|
}
|
|
|
// 获取资源文件存储
|
|
|
ZipPartStore partStore = (ZipPartStore) wp.getSourcePartStore();
|
|
|
- // 获取图片资源定义
|
|
|
+ // 获取图片资源定义、只取图片
|
|
|
RelationshipsPart rp = wp.getMainDocumentPart().getRelationshipsPart();
|
|
|
- List<Relationship> rels = rp.getRelationshipsByType(Namespaces.IMAGE);
|
|
|
+ List<Relationship> relationships = rp.getRelationshipsByType(Namespaces.IMAGE);
|
|
|
|
|
|
List<Part> parts = new ArrayList<Part>();
|
|
|
- for (Relationship relationship : rels) {
|
|
|
+ for (Relationship relationship : relationships) {
|
|
|
parts.add(rp.getPart(relationship));
|
|
|
}
|
|
|
// 添加资源文件定义
|
|
|
for (Part p : parts) {
|
|
|
String relId = p.getSourceRelationships().get(0).getId();
|
|
|
+ //如果不存在该relId,拷贝图片
|
|
|
if(!relationshipsPart.isRelIdOccupied(relId)){
|
|
|
- copyImage(wordMLPackage,partStore,p);
|
|
|
+ copyImage(wordMLPackage,partStore,p,relId);
|
|
|
}
|
|
|
}
|
|
|
// 添加资源文件存储
|
|
@@ -739,20 +742,20 @@ public final class DocxProcessUtil {
|
|
|
|
|
|
/**
|
|
|
* word合并时复制图片
|
|
|
+ * 将partStore中的Relationship复制到wordMLPackage中
|
|
|
* @param wordMLPackage
|
|
|
* @param partStore
|
|
|
* @param p
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static void copyImage(WordprocessingMLPackage wordMLPackage, ZipPartStore partStore, Part p)throws Exception{
|
|
|
+ public static void copyImage(WordprocessingMLPackage wordMLPackage, ZipPartStore partStore, Part p,String relId)throws Exception{
|
|
|
byte [] bytes = partStore.getByteArray(p.getPartName().getName().substring(1)).getBytes();
|
|
|
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
|
|
|
BufferedImage bi1 = ImageIO.read(bais);
|
|
|
if(bi1!=null){
|
|
|
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
|
|
|
- imagePart.getRelLast().setId(p.getSourceRelationships().get(0).getId());
|
|
|
+ imagePart.getRelLast().setId(relId);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -961,6 +964,62 @@ public final class DocxProcessUtil {
|
|
|
return -1;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public static byte[] getWordBytesByQuestion(WordprocessingMLPackage wordMLPackage,List<String> wordxmls) throws Exception{
|
|
|
+ try {
|
|
|
+ RelationshipsPart mainRelationshipsPart = wordMLPackage.getMainDocumentPart().getRelationshipsPart();
|
|
|
+ // 获取总的资源文件存储
|
|
|
+ ZipPartStore partStore = (ZipPartStore) wordMLPackage.getSourcePartStore();
|
|
|
+ //创建子Word
|
|
|
+ WordprocessingMLPackage subWordPackage = WordprocessingMLPackage.createPackage();
|
|
|
+ MainDocumentPart subMainDocumentPart = subWordPackage.getMainDocumentPart();
|
|
|
+ //向子Word中添加内容
|
|
|
+ for(String xml:wordxmls){
|
|
|
+ Object obj = XmlUtils.unmarshalString(xml);
|
|
|
+ subMainDocumentPart.addObject(obj);
|
|
|
+ }
|
|
|
+ //向子Word中添加图片的关联关系
|
|
|
+ RelationshipsPart subRelationshipsPart = subMainDocumentPart.getRelationshipsPart();
|
|
|
+ List<Relationship> subRelationships = subRelationshipsPart.getRelationshipsByType(Namespaces.IMAGE);
|
|
|
+ List<Object> blips = subMainDocumentPart.getJAXBNodesViaXPath("//a:blip",false);
|
|
|
+ for(Object blip:blips){
|
|
|
+ if (blip.getClass().equals(CTBlip.class)) {
|
|
|
+ CTBlip cTBlip = (CTBlip)blip;
|
|
|
+ Relationship relationship = mainRelationshipsPart.getRelationshipByID(cTBlip.getEmbed());
|
|
|
+ if(relationship!=null){
|
|
|
+ subRelationships.add(relationship);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //向子Word中添加资源
|
|
|
+ List<Part> parts = new ArrayList<Part>();
|
|
|
+ for (Relationship relationship : subRelationships) {
|
|
|
+ if(mainRelationshipsPart.getPart(relationship)!=null){
|
|
|
+ parts.add(mainRelationshipsPart.getPart(relationship));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (Part p : parts) {
|
|
|
+ if(p.getSourceRelationships()!=null&&
|
|
|
+ p.getSourceRelationships().size()>0){
|
|
|
+ String relId = p.getSourceRelationships().get(0).getId();
|
|
|
+ if(mainRelationshipsPart.isRelIdOccupied(relId)){
|
|
|
+ DocxProcessUtil.copyImage(subWordPackage,partStore,p,relId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return DocxProcessUtil.getPkgByte(subWordPackage);
|
|
|
+ } catch (Docx4JException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
System.out.println(formatPWordMl("<p>A</p>"));
|