java 转换xml格式的doc或docx文档为docx以及docx转PDF

转换xml格式的doc或docx文档为docx

import org.docx4j.Docx4J;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @author yichuan@iscas.ac.cn
 * @version 1.0
 * @date 2022/8/17 10:48
 * 转换xml格式的doc或docx文档为docx
 */
public class XmlDocToDocxUtil {
    public static void main(String[] args) {
        XmlDocToDocxUtil.invoke("C:\\Users\\x\\Desktop\\半年报20210607091824.docx");
    }

    /**
     * 转换执行方法,转换后和原始路径
     *
     * @param xmlPath 原始路径
     */
    public static String invoke(String xmlPath) {
        if (xmlPath.endsWith(".doc") || xmlPath.endsWith(".docx")) {
            //不仅可以将doc转为docx,还可以将freemarker生成的xml类型的docx转为正常的文档
            String docxPath = xmlPath.replaceAll("(\\.docx)|(\\.doc)", ".docx");
            try (FileInputStream inputStream = new FileInputStream(xmlPath);) {
                WordprocessingMLPackage wmlPackage = Docx4J.load(inputStream);
                //转换为DOCX
                try (FileOutputStream docx = new FileOutputStream(docxPath);) {
                    Docx4J.save(wmlPackage, docx, Docx4J.FLAG_SAVE_ZIP_FILE);
                    xmlPath = docxPath;
                }
            } catch (Exception e) {
                System.out.println((xmlPath + ":不需要转换:" + e.getLocalizedMessage()));
            }
        }
        System.out.println("WORD 路径:" + xmlPath);
        return xmlPath;
    }
}

依赖

<!--        xmlDoc转docx-->
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j</artifactId>
            <version>3.2.1</version>
        </dependency>

gradle版本
//报告- xmlDoc转docx
    implementation (group: 'org.docx4j', name: 'docx4j', version: '3.2.1')
            {
        exclude group: 'org.slf4j',module:'slf4j-api'
    }
    implementation group: 'com.sun.xml.bind', name: 'jaxb-impl', version: '2.2.3-1'

docx转PDF

import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.*;

/**
 * @author yichuan@iscas.ac.cn
 * @version 1.0
 * @date 2022/8/17 10:48
 */
public class DocxToPDFConverter {
    public static void main(String[] args) {
        String outpath = "C:\\Users\\x\\Desktop\\半年报20210607091824.docx";
        String docxPath = XmlDocToDocxUtil.invoke(outpath);
        try {
            InputStream source = new FileInputStream(docxPath);
            OutputStream target = new FileOutputStream("C:\\Users\\x\\Desktop\\半年报20210607091824.pdf");
            PdfOptions options = PdfOptions.create();
            DocxToPDFConverter converter = new DocxToPDFConverter(source, target, true, true);
            converter.convert();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private long startTime;
    private long startOfProcessTime;
    protected InputStream inStream;
    protected OutputStream outStream;
    protected boolean showOutputMessages = false;
    protected boolean closeStreamsWhenComplete = true;

    public DocxToPDFConverter(InputStream inStream, OutputStream outStream,
                              boolean showMessages, boolean closeStreamsWhenComplete) {
        this.inStream = inStream;
        this.outStream = outStream;
        this.showOutputMessages = showMessages;
        this.closeStreamsWhenComplete = closeStreamsWhenComplete;
    }

    public void convert() throws Exception {
        loading();
        XWPFDocument document = new XWPFDocument(inStream);
        PdfOptions options = PdfOptions.create();
        processing();
        PdfConverter.getInstance().convert(document, outStream, options);
        finished();
    }

    private void startTime() {
        startTime = System.currentTimeMillis();
        startOfProcessTime = startTime;
    }

    protected void loading() {
        sendToOutputOrNot(String.format("\nLoading stream\n\n"));
        startTime();
    }

    protected void processing() {
        long currentTime = System.currentTimeMillis();
        long prevProcessTook = currentTime - startOfProcessTime;
        sendToOutputOrNot(String.format("Load completed in %1$dms, now converting...\n\n", prevProcessTook));
        startOfProcessTime = System.currentTimeMillis();
    }

    protected void finished() {
        long currentTime = System.currentTimeMillis();
        long timeTaken = currentTime - startTime;
        long prevProcessTook = currentTime - startOfProcessTime;
        startOfProcessTime = System.currentTimeMillis();
        if (closeStreamsWhenComplete) {
            try {
                inStream.close();
                outStream.close();
            } catch (IOException e) {
                //Nothing done
            }
        }
        sendToOutputOrNot(String.format("Conversion took %1$dms.\n\nTotal: %2$dms\n", prevProcessTook, timeTaken));
    }

    private void sendToOutputOrNot(String toBePrinted) {
        if (showOutputMessages) {
            actuallySendToOutput(toBePrinted);
        }
    }

    protected void actuallySendToOutput(String toBePrinted) {
        System.out.println(toBePrinted);
    }
}

<!--        docx转pdf-->
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
            <version>1.0.5</version>
        </dependency>
要将XML格式DOCDOCX文档转换DOCX格式,您可以使用Apache POI库来处理Word文档。以下是一个示例代码,展示了如何将XML格式DOCDOCX文档转换DOCX格式: ```java 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.hwpf.HWPFDocument; import org.apache.poi.hwpf.converter.WordToConverter; import org.apache.poi.hwpf.usermodel.Range; import java.io.FileInputStream; import java.io.FileOutputStream; public class XMLToDocxConverter { public static void main(String[] args) { try { // 读取XML格式DOCDOCX文件 FileInputStream fis = new FileInputStream("input.xml"); // 创建Word文档对象 XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); // 将XML内容写入Word文档 byte[] buffer = new byte[fis.available()]; fis.read(buffer); run.setText(new String(buffer)); // 保存为DOCX格式的Word文档 FileOutputStream fos = new FileOutputStream("output.docx"); document.write(fos); // 关闭流 fos.close(); fis.close(); System.out.println("XML格式DOCDOCX文档已成功转换DOCX格式!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 上述示例代码使用Apache POI库中的XWPFDocument、XWPFParagraph和XWPFRun类来处理DOCX格式的Word文档,通过读取XML文件的内容并将其写入Word文档来实现转换。请确保您已将Apache POI库正确添加到项目中。 希望能对您有所帮助!
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值