利用Java将word文件转成pdf文件

说明:使用本方法计算机中必须含有office

一.准备工作

1.准备如图三个文件:

2.将第二个文件复制到C:\Windows\System32,将第三个文件复制到C:\Windows\SysWOW64,

然后将这两个文件复制到jdk,jre里面的bin文件夹

二.开始编译

1.项目导入第一个文件的jar包

2.代码如图所示:

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;

import java.io.File;

public class Test {
        static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
        static final int wdFormatPDF = 17;// PDF 格式

        public static void wordToPdf(String wordpath, String pdfpath) {

            System.out.println("启动Word...");
            long start = System.currentTimeMillis();
            ActiveXComponent app = null;
            try {
                //打开word应用程序
                app = new ActiveXComponent("Word.Application");
                设置应用操作是文档不在明面上显示,只在后台静默处理。
                app.setProperty("Visible", false);
                //获得文档集合,用来操作我们需要处理的文档.
                Dispatch docs = app.getProperty("Documents").toDispatch();
                System.out.println("打开文档..." + wordpath);
                //打开word文档
                Dispatch doc = Dispatch.call(docs,//
                        "Open", //
                        wordpath,// FileName
                        false,// ConfirmConversions
                        true // ReadOnly
                ).toDispatch();

                System.out.println("转换文档到PDF..." + pdfpath);
                File tofile = new File(pdfpath);
                //创建存放pdf的文件夹
                if (tofile.exists()) {
                    tofile.delete();
                }
                //将word另存为pdf
                Dispatch.call(doc,//
                        "SaveAs", //
                        pdfpath, // FileName
                        wdFormatPDF);
                //关闭word文档
                Dispatch.call(doc, "Close", false);
                long end = System.currentTimeMillis();
                System.out.println("转换完成..用时:" + (end - start) + "ms.");
            } catch (Exception e) {
                System.out.println("========Error:文档转换失败:" + e.getMessage());
            } finally {
                if (app != null)
                    app.invoke("Quit", wdDoNotSaveChanges);
            }
        }

        public static void main(String[] args) {
            wordToPdf("C:\\Users\\wukefan\\Desktop\\test.doc","C:\\Users\\wukefan\\Desktop\\test.pdf");
        }
}

三.运行项目

1.运行成功,项目结果如图所示:

2.如图所示桌面,桌面多了个pdf文件:

可以使用Apache POI库和iText库来实现将本地word文件换为PDF。以下是一段Java代码实现该功能的示例: ```java import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDType1Font; public class WordToPDFConverter { public static void main(String[] args) { String sourceFile = "path/to/word/file.docx"; String destFile = "path/to/pdf/file.pdf"; try { XWPFDocument doc = new XWPFDocument(new FileInputStream(sourceFile)); PDDocument pdfDoc = new PDDocument(); for (XWPFParagraph para : doc.getParagraphs()) { PDPage page = new PDPage(); pdfDoc.addPage(page); PDPageContentStream contentStream = new PDPageContentStream(pdfDoc, page); contentStream.beginText(); contentStream.setFont(PDType1Font.TIMES_ROMAN, 12); contentStream.newLineAtOffset(100, 700); contentStream.showText(para.getText()); contentStream.endText(); contentStream.close(); } OutputStream outputStream = new FileOutputStream(new File(destFile)); pdfDoc.save(outputStream); pdfDoc.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 这段代码使用Apache POI库读取Word文档,并使用iText库将其换为PDF文档。使用PDPage和PDPageContentStream类来创建PDF页面和内容流,并使用PDType1Font类设置字体和字号。最后,使用PDF文档对象的save()方法将其保存到指定的输出流中。
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吴名氏.

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值