在合并docx与doc的时候报错

在合并docx与doc的时候报错,请问怎么办,怎么更改,代码如下:


import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

 public  class FileCopyUtil{

    public static void FileCopy(String[] inPath,String outPath) {
        File newFile = new File( outPath);
        List<File> srcfile = new ArrayList<>();
        for (String s : inPath) {
            File file = new File(s);
            srcfile.add( file );
        }
        try {
            OutputStream dest = new FileOutputStream( newFile );
            ArrayList<XWPFDocument> documentList = new ArrayList<>();
            XWPFDocument doc = null;
            FileInputStream in=null;
            for( int i = 0; i < srcfile.size(); i++ ) {
                in = new FileInputStream( srcfile.get( i ).getPath() );
                OPCPackage open = OPCPackage.open( in );
                XWPFDocument document = new XWPFDocument( open );
                documentList.add( document );
            }
            in.close();
            for( int i = 0; i < documentList.size(); i++ ) {
                doc = documentList.get( 0 );
                if( i != 0 ) {
                    // 分页
                    documentList.get( i ).createParagraph().setPageBreak( true );
                    appendBody( doc, documentList.get( i ) );
                }
            }
            // 分页
            doc.createParagraph().setPageBreak( true );
            doc.write( dest );

        } catch( Exception e ) {
            e.printStackTrace();
        }
    }

    /*public static void main(String[] args) {
        File newFile = new File( "F:\\outs.docx");
        List<File> srcfile = new ArrayList<>();
        File file1 = new File( "F:\\out.docx");
        File file2 = new File( "F:\\outt.docx");
        srcfile.add( file1 );
        srcfile.add( file2 );
        try {
            OutputStream dest = new FileOutputStream( newFile );
            ArrayList<XWPFDocument> documentList = new ArrayList<>();
            XWPFDocument doc = null;
            for( int i = 0; i < srcfile.size(); i++ ) {
                FileInputStream in = new FileInputStream( srcfile.get( i ).getPath() );
                OPCPackage open = OPCPackage.open( in );
                XWPFDocument document = new XWPFDocument( open );
                documentList.add( document );
            }
            for( int i = 0; i < documentList.size(); i++ ) {
                doc = documentList.get( 0 );
                if( i != 0 ) {
                    // 分页
                    documentList.get( i ).createParagraph().setPageBreak( true );
                    appendBody( doc, documentList.get( i ) );
                }
            }
            // 分页
            doc.createParagraph().setPageBreak( true );
            doc.write( dest );
        } catch( Exception e ) {
            e.printStackTrace();
        }
    }*/

    public static void appendBody( XWPFDocument src, XWPFDocument append ) throws Exception {
        CTBody src1Body = src.getDocument().getBody();
        CTBody src2Body = append.getDocument().getBody();
        List<XWPFPictureData> allPictures = append.getAllPictures();
        // 记录图片合并前及合并后的ID
        Map<String, String> map = new HashMap<>();
        for( XWPFPictureData picture : allPictures ) {
            String before = append.getRelationId( picture );
            // 将原文档中的图片加入到目标文档中
            String after = src.addPictureData( picture.getData(), Document.PICTURE_TYPE_PNG );
            map.put( before, after );
        }
        appendBody( src1Body, src2Body, map );
    }

    private static  void appendBody( CTBody src, CTBody append, Map<String, String> map ) throws Exception {
        XmlOptions optionsOuter = new XmlOptions();
        optionsOuter.setSaveOuter();
        String appendString = append.xmlText( optionsOuter );
        String rgex = "<[\\s]*?w:sectPr[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?w:sectPr[\\s]*?>";
        appendString = appendString.replaceAll( rgex, "" );
        // 去除分页符
        String srcString = src.xmlText().replaceAll( "<w:p><w:r><w:br w:type=\"page\"/></w:r></w:p>", "" ).replaceAll( "<w:r><w:br w:type=\"page\"/></w:r>", "" );
        String prefix = srcString.substring( 0, srcString.indexOf( ">" ) + 1 );
        String mainPart = srcString.substring( srcString.indexOf( ">" ) + 1, srcString.lastIndexOf( "<" ) );
        String sufix = srcString.substring( srcString.lastIndexOf( "<" ) );
        String addPart = appendString.substring( appendString.indexOf( ">" ) + 1, appendString.lastIndexOf( "<" ) );
        if( map != null && !map.isEmpty() ) {
            // 对xml字符串中图片ID进行替换
            for( Map.Entry<String, String> set : map.entrySet() ) {
                addPart = addPart.replace( set.getKey(), set.getValue() );
            }
        }
        // 将两个文档的xml内容进行拼接
        CTBody makeBody = CTBody.Factory.parse( prefix + mainPart + addPart + sufix );
        src.set( makeBody );
    }

}


你用的什么哦模块进行合并的

从错误提示来看,很有可能是docx和doc版本不同导致的兼容性问题。可以尝试以下几种解决方法:

  1. 确保合并的文件版本一致,都使用docx或者都使用doc格式。不同版本的Word文档结构和内容都可能不同,直接合并容易出现问题。
  2. 使用docx作为输出格式。docx是Office Open XML标准格式,更加规范和开放,受到更广泛支持,合并效果更好。
  3. 调用Word自动合并功能。你可以试试使用Java调用Word COM对象或Windows API来自动合并文档,让Word内部处理兼容性问题。
  4. 调整代码,增加错误处理和容错机制。比如:
  • 处理文件未找到等IO异常
  • 处理合并过程中的异常,不让程序直接崩溃
  • 对XML内容进行校验和清理,移除不兼容的元素和内容
  • 等等
    增加试探和容错机制,可以让代码更加健壮,即使出现不兼容情况也可以尽量避免崩溃和错误。
    除此之外,你还可以检查一下代码中其他可能的问题,比如:
  • XML处理是否正确,元素嵌套和引用是否有误
  • 图片、页眉页脚等内容是否合并完整
  • 等等
    总之,处理文件合并这种操作,增加异常检查和容错机制是很有必要的