笔记java doc转docx

本文介绍了如何使用Doc2Docx工具将Java文档格式转换为Docx格式,并提供了替换水印的步骤,附带相关用例下载。

Doc2Docx 主函数


 
import java.io.File;
 
import java.util.HashMap;
import java.util.Map;
 

import com.aspose.words.Document;

public class Doc2Docx {

	public static void main(String[] args) {

		String docFile = "E:\\poi\\2.doc";
		String docxFile = "E:\\poi\\x2.docx";
		doc2docx(docFile, docxFile);

	}

	public static void doc2docx(String docFile, String docxFile) {

		Document doc;
		try {

			String tempFile = docxFile.substring(0, docxFile.lastIndexOf(".")) + "_temp"
					+ docxFile.substring(docxFile.lastIndexOf("."), docxFile.length());

			doc = new Document(docFile);
			doc.save(tempFile);
			Map<String, String> map = new HashMap<String, String>();
			map.put("Evaluation Only. Created with Aspose.Words. Copyright 2003-2018 Aspose Pty Ltd.", "");

			// 使用 aspose不使用license.xml会产生,替换水印的文字
			DocxReplace.replaceAndGenerateWord(tempFile, docxFile, map);
			// 删除临时文件
			forceDelete(new File(tempFile));

		} catch (Exception e1) {

			e1.printStackTrace();
		}

	}

	public static boolean forceDelete(File file) {
		boolean result = file.delete();
		int tryCount = 0;
		while (!result && tryCount++ < 10) {
			System.gc();
			result = file.delete();
		}
		return result;
	}

}

DocxReplace 替换水印


  
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.util.ArrayList;  
import java.util.HashMap;  
import java.util.Iterator;  
import java.util.List;  
import java.util.Map;  
import java.util.Map.Entry;  
import java.util.regex.Matcher;  
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.POIXMLDocument;  
import org.apache.poi.hwpf.HWPFDocument;  
import org.apache.poi.hwpf.usermodel.Range;  
import org.apache.poi.xwpf.usermodel.XWPFDocument;  
import org.apache.poi.xwpf.usermodel.XWPFParagraph;  
import org.apache.poi.xwpf.usermodel.XWPFRun;  
import org.apache.poi.xwpf.usermodel.XWPFTable;  
import org.apache.poi.xwpf.usermodel.XWPFTableCell;  
import org.apache.poi.xwpf.usermodel.XWPFTableRow;  
  
public class DocxReplace {  
  
    // 返回Docx中需要替换的特殊字符,没有重复项  
    // 推荐传入正则表达式参数"\\$\\{[^{}]+\\}"  
    public ArrayList<String> getReplaceElementsInWord(String filePath,  
            String regex) {  
        String[] p = filePath.split("\\.");  
        if (p.length > 0) {// 判断文件有无扩展名  
            // 比较文件扩展名  
            if (p[p.length - 1].equalsIgnoreCase("doc")) {  
                ArrayList<String> al = new ArrayList<>();  
                File file = new File(filePath);  
                HWPFDocument document = null;  
                try {  
                    InputStream is = new FileInputStream(file);  
                    document = new HWPFDocument(is);  
                } catch (FileNotFoundException e) {  
                    e.printStackTrace();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
                Range range = document.getRange();  
                String rangeText = range.text();  
                CharSequence cs = rangeText.subSequence(0, rangeText.length());  
                Pattern pattern = Pattern.compile(regex);  
                Matcher matcher = pattern.matcher(cs);  
                int startPosition = 0;  
                while (matcher.find(startPosition)) {  
                    if (!al.contains(matcher.group())) {  
                        al.add(matcher.group());  
                    }  
                    startPosition = matcher.end();  
                }  
                return al;  
            } else if (p[p.length - 1].equalsIgnoreCase("docx")) {  
                ArrayList<String> al = new ArrayList<>();  
                XWPFDocument document = null;  
                try {  
                    document = new XWPFDocument(  
                            POIXMLDocument.openPackage(filePath));  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
                // 遍历段落  
                Iterator<XWPFParagraph> itPara = document  
                        .getParagraphsIterator();  
                while (itPara.hasNext()) {  
                    XWPFParagraph paragraph = (XWPFParagraph) itPara.next();  
                    String paragraphString = paragraph.getText();  
                    CharSequence cs = paragraphString.subSequence(0,  
                            paragraphString.length());  
                    Pattern pattern = Pattern.compile(regex);  
                    Matcher matcher = pattern.matcher(cs);  
                    int startPosition = 0;  
                    while (matcher.find(startPosition)) {  
                        if (!al.contains(matcher.group())) {  
                            al.add(matcher.group());  
                        }  
                        startPosition = matcher.end();  
                    }  
                }  
                // 遍历表  
                Iterator<XWPFTable> itTable = document.getTablesIterator();  
                while (itTable.hasNext()) {  
                    XWPFTable table = (XWPFTable) itTable.next();  
                    int rcount = table.getNumberOfRows();  
                    for (int i = 0; i < rcount; i++) {  
                        XWPFTableRow row = table.getRow(i);  
                        List<XWPFTableCell> cells = row.getTableCells();  
                        for (XWPFTableCell cell : cells) {  
                            String cellText = "";  
                            cellText = cell.getText();  
                            CharSequence cs = cellText.subSequence(0,  
                                    cellText.length());  
                            Pattern pattern = Pattern.compile(regex);  
                            Matcher matcher = pattern.matcher(cs);  
                            int startPosition = 0;  
                            while (matcher.find(startPosition)) {  
                                if (!al.contains(matcher.group())) {  
                                    al.add(matcher.group());  
                                }  
                                startPosition = matcher.end();  
                            }  
                        }  
                    }  
                }  
                return al;  
            } else {  
                return null;  
            }  
        } else {  
            return null;  
        }  
    }  
 
    // 替换word中需要替换的特殊字符  
    public static boolean replaceAndGenerateWord(String srcPath,  
            String destPath, Map<String, String> map) { 
     
        String[] sp = srcPath.split("\\.");  
        String[] dp = destPath.split("\\.");  
        if ((sp.length > 0) && (dp.length > 0)) {// 判断文件有无扩展名  
            // 比较文件扩展名  
            if (sp[sp.length - 1].equalsIgnoreCase("docx")) {  
                try {  
                    XWPFDocument document = new XWPFDocument(  
                            POIXMLDocument.openPackage(srcPath));  
                    // 替换段落中的指定文字  
                    Iterator<XWPFParagraph> itPara = document  
                            .getParagraphsIterator();  
                    while (itPara.hasNext()) {  
                        XWPFParagraph paragraph = (XWPFParagraph) itPara.next();  
                        List<XWPFRun> runs = paragraph.getRuns();  
                        for (int i = 0; i < runs.size(); i++) {  
                            String oneparaString = runs.get(i).getText(  
                                    runs.get(i).getTextPosition());  
                            for (Map.Entry<String, String> entry : map  
                                    .entrySet()) {  
                            	if(StringUtils.isNotBlank(oneparaString) && oneparaString.indexOf(entry.getKey())!=-1){
                            		oneparaString = oneparaString.replace(  
                                            entry.getKey(), entry.getValue());
                            		runs.get(i).setText(oneparaString, 0);  
                            	}
                                  
                            }  
                            
                           
                        }  
                    }  
  
                    // 替换表格中的指定文字  
                    Iterator<XWPFTable> itTable = document.getTablesIterator();  
                    while (itTable.hasNext()) {  
                        XWPFTable table = (XWPFTable) itTable.next();  
                        int rcount = table.getNumberOfRows();  
                        for (int i = 0; i < rcount; i++) {  
                            XWPFTableRow row = table.getRow(i);  
                            List<XWPFTableCell> cells = row.getTableCells();  
                            for (XWPFTableCell cell : cells) {  
                                String cellTextString = cell.getText();  
                                for (Entry<String, String> e : map.entrySet()) {  
                                    if (cellTextString.contains(e.getKey())) {
                                    	 cellTextString = cellTextString  
                                                 .replace(e.getKey(),  
                                                         e.getValue());  
                                    	 cell.removeParagraph(0);  
                                         cell.setText(cellTextString); 
                                         cell.setColor(cell.getColor());
                                          
                                    } 
                                       
                                }  
                                
                            }  
                        }  
                    }  
                    FileOutputStream outStream = null;  
                    outStream = new FileOutputStream(destPath);  
                    document.write(outStream);  
                    outStream.close();  
                    return true;  
                } catch (Exception e) {  
                    e.printStackTrace();  
                    return false;  
                }  
  
            } else  
            // doc只能生成doc,如果生成docx会出错  
            if ((sp[sp.length - 1].equalsIgnoreCase("doc"))  
                    && (dp[dp.length - 1].equalsIgnoreCase("doc"))) {  
                HWPFDocument document = null;  
                try {  
                    document = new HWPFDocument(new FileInputStream(srcPath));  
                    Range range = document.getRange();  
                    for (Map.Entry<String, String> entry : map.entrySet()) {  
                        range.replaceText(entry.getKey(), entry.getValue());  
                    }  
                    FileOutputStream outStream = null;  
                    outStream = new FileOutputStream(destPath);  
                    document.write(outStream);  
                    outStream.close();  
                    return true;  
                } catch (FileNotFoundException e) {  
                    e.printStackTrace();  
                    return false;  
                } catch (IOException e) {  
                    e.printStackTrace();  
                    return false;  
                }  
            } else {  
                return false;  
            }  
        } else {  
            return false;  
        }  
    }  
  
     
}

用例下载
 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值