免费工具,亲测未出现结构错位异常等
话不多说,直接上代码抄吧!
代码
@Slf4j
public class DocPdf {
public static void main(String[] args) {
doc2pdf("D:\\desktop\\V3.docx","D:\\desktop\\V3.pdf");
}
public static void doc2pdf(String inPath, String outPath) {
FileOutputStream os =null;
try {
// 新建一个空白pdf文档
File file = new File(outPath);
os = new FileOutputStream(file);
// Address是将要被转化的word文档
Document doc = new Document(inPath);
doc.save(os, SaveFormat.PDF);
} catch (Exception e) {
log.error("word转pdf异常Exception:{}",e);
}finally{
if(os!=null){
try {
os.close();
} catch (IOException e) {
log.error("word转pdf异常IOException:{}",e);
}
}
}
}
}
导包
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
引入依赖
<dependency>
<groupId>com.luhuiguo</groupId>
<artifactId>aspose-words</artifactId>
<version>22.10</version>
</dependency>