如何使用docx4j合并两个docx文件?

时间:2016-03-29 13:04:40

标签: java docx4j

我有两个docx文件要合并,我在docx4j中有以下代码,但有一些错误! docx文件都用于跟踪更改。因此输出文件应该包含两个docx文件的详细信息。

import java.io.File;    
import java.util.ArrayList;          
import java.util.List;             
import org.docx4j.openpackaging.exceptions.Docx4JException;   
import org.docx4j.openpackaging.io.SaveToZipFile;     
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;   
import com.plutext.merge.BlockRange;  
import com.plutext.merge.BlockRange.HfBehaviour;          
import com.plutext.merge.BlockRange.SectionBreakBefore;         
import com.plutext.merge.BlockRange.NumberingHandler;          
import com.plutext.merge.BlockRange.StyleHandler;        
import com.plutext.merge.DocumentBuilder;         
public class MergeWholeDocumentsUsingBlockRange                                                                                                                                
{
     public final static String DIR_IN = System.getProperty("user.dir")+ "/"; 
     public final static String DIR_OUT = System.getProperty("user.dir")+ "/"; 
     public static void main(String[] args) throws Exception { 
     String[] files = {"table.docx", "Table1.docx"}; 
     List blockRanges = new ArrayList(); 
     for (int i=0 ; i< files.length; i++) { 
     BlockRange block = new BlockRange(WordprocessingMLPackage.load(new File(DIR_IN + files[i]))); 
        blockRanges.add( block ); 
        block.setStyleHandler(StyleHandler.RENAME_RETAIN);
        block.setNumberingHandler(NumberingHandler.ADD_NEW_LIST);
        block.setRestartPageNumbering(false);
        block.setHeaderBehaviour(HfBehaviour.DEFAULT);
        block.setFooterBehaviour(HfBehaviour.DEFAULT);
        block.setSectionBreakBefore(SectionBreakBefore.NEXT_PAGE);
    } 
        // Perform the actual merge 
        DocumentBuilder documentBuilder = new DocumentBuilder(); 
        WordprocessingMLPackage output = documentBuilder.buildOpenDocument(blockRanges); 
       // Save the result 
       SaveToZipFile saver = new SaveToZipFile(output); 
       saver.save(DIR_OUT+"OUT_MergeWholeDocumentsUsingBlockRange.docx"); 
       } 
    } 

0 个答案:

没有答案