java word转pdf的几种方法

最近公司需要以word为模版,填充数据,然后转成pdf。做了一点点研究

1.使用xdocreport进行转(优点效率高,缺点对word格式要求较大,适合对生成pdf要求不高的情况)

/**
 * 将word文档, 转换成pdf
 * 宋体:STSong-Light
 *
 * @param fontParam1 可以字体的路径,也可以是itextasian-1.5.2.jar提供的字体,比如宋体"STSong-Light"
 * @param fontParam2 和fontParam2对应,fontParam1为路径时,fontParam2=BaseFont.IDENTITY_H,为itextasian-1.5.2.jar提供的字体时,fontParam2="UniGB-UCS2-H"
 * @param tmp        源为word文档, 必须为docx文档
 * @param target     目标输出
 * @throws Exception
 */

public void wordConverterToPdf(String tmp, String target, String fontParam1, String fontParam2) {
    InputStream sourceStream = null;
    OutputStream targetStream = null;
    XWPFDocument doc = null;
    try {
        sourceStream = new FileInputStream(tmp);
        targetStream = new FileOutputStream(target);
        doc = new XWPFDocument(sourceStream);
        PdfOptions options = PdfOptions.create();
        //中文字体处理
        options.fontProvider(new IFontProvider() {
            public Font getFont(String familyName, String encoding, float size, int style, Color color) {
                try {
                    BaseFont bfChinese = BaseFont.createFont(fontParam1, fontParam2, BaseFont.NOT_EMBEDDED);
                    Font fontChinese = new Font(bfChinese, size, style, color);
                    if (familyName != null)
                        fontChinese.setFamily(familyName);
                    return fontChinese;
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
        });
        PdfConverter.getInstance().convert(doc, targetStream, options);
        File file = new File(tmp);
        file.delete();  //刪除word文件
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(doc);
        IOUtils.closeQuietly(targetStream);
        IOUtils.closeQuietly(sourceStream);
    }
}

2.使用dom4j进行转换,试了下效率较低,而且转换质量还不如xdoreport,故没有继续。也可能是小弟没研究清楚,代码就不粘了


3.使用libreoffice来进行转(效果好,格式便于控制,基本上转出来的pdf和用libreoffice查看看到的word样子已经非常接近了)

最开始网上查用jodconverter来调用openOffice或者libreoffice的服务来转换,试了下也是速度极慢,转一个2页的word需要10s,确实不能忍。

然后想到既然jodconverter能调用,那我自己执行libreoffice命令转换不可以吗,之后发现这个思路可行。

步骤:

1.安装libreoffice(linux还需要装unoconv),目前我安装的是5.3.3版本

2.黑窗口直接敲命令,windows:soffice --convert-to pdf  example.docx   linux: doc2pdf example.docx, windows需要添加path系统变量(C:\Program Files\LibreOffice 5\program),不然无法识别soffice命令

3.ok,既然可以这么玩,放到java项目就简单了

public boolean wordConverterToPdf(String docxPath) throws IOException {
    File file = new File(docxPath);
    String path = file.getParent();
    try {
        String osName = System.getProperty("os.name");
        String command = "";
        if (osName.contains("Windows")) {
            command = "soffice --convert-to pdf  -outdir " + path + " " + docxPath;
        } else {
            command = "doc2pdf --output=" + path + File.separator + file.getName().replaceAll(".(?i)docx", ".pdf") + " " + docxPath;
        }
        String result = CommandExecute.executeCommand(command);
        LOGGER.info("result==" + result);
        if (result.equals("") || result.contains("writer_pdf_Export")) {
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    return false;
}
代码亲试可用,欢迎吐槽

写这篇文章后想到应该还有些其他的方法,比如用wps、office来进行转化,若大虾们有更合适的方式请赐教之

源码地址:

https://github.com/AryaRicky/toPdfUtils.git

  • 13
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 35
    评论
Java中,有几种方法可以将Word换为PDF。以下是其中几种常见的方法: 1. 使用Apache POI和iText库:Apache POI用于读取Word文档,然后使用iText库将其换为PDF。您可以使用以下代码示例: ```java import org.apache.poi.xwpf.converter.pdf.PdfConverter; import org.apache.poi.xwpf.usermodel.XWPFDocument; import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfWriter; import java.io.*; public class WordToPdfConverter { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("input.docx"); XWPFDocument document = new XWPFDocument(fis); File outputFile = new File("output.pdf"); FileOutputStream out = new FileOutputStream(outputFile); Document pdfDoc = new Document(); PdfWriter writer = PdfWriter.getInstance(pdfDoc, out); pdfDoc.open(); PdfConverter.getInstance().convert(document, pdfDoc, null); pdfDoc.close(); System.out.println("Word换为PDF成功!"); } catch (Exception ex) { System.out.println("换失败:" + ex.getMessage()); } } } ``` 2. 使用Aspose.Words库:Aspose.Words是一个功能强大的Java库,可以轻松地将Word换为PDF。您需要在项目中引入Aspose.Words库,然后可以使用以下代码示例: ```java import com.aspose.words.Document; import com.aspose.words.SaveFormat; public class WordToPdfConverter { public static void main(String[] args) { try { Document doc = new Document("input.docx"); doc.save("output.pdf", SaveFormat.PDF); System.out.println("Word换为PDF成功!"); } catch (Exception ex) { System.out.println("换失败:" + ex.getMessage()); } } } ``` 3. 使用Jacob和Microsoft Office:如果您的系统已安装Microsoft Office,则可以使用Jacob库通过自动化操作将Word文档保存为PDF。您可以使用以下代码示例: ```java import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class WordToPdfConverter { public static void main(String[] args) { ActiveXComponent app = null; Dispatch doc = null; try { app = new ActiveXComponent("Word.Application"); app.setProperty("Visible", new Variant(false)); Dispatch docs = app.getProperty("Documents").toDispatch(); doc = Dispatch.call(docs, "Open", "input.docx").toDispatch(); Dispatch.call(doc, "SaveAs", "output.pdf", 17); System.out.println("Word换为PDF成功!"); } catch (Exception ex) { System.out.println("换失败:" + ex.getMessage()); } finally { if (doc != null) { Dispatch.call(doc, "Close", false); } if (app != null) { app.invoke("Quit"); } } } } ``` 这些是将Word换为PDF几种方法。您可以根据您的需求选择其中一种方法进行实现

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 35
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值