转换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>