假定运用 java 将 word 文档转换为 pdf
办法一:利用 Apache POI 库
Apache POI 库是一个 Java 库,用于读与以及写进 Microsoft Office 文件格局。它否以用于将 Word 文档转换为 PDF。
import org.<a style="color:#f60; text-decoration:underline;" href="https://www.php.cn/zt/1597两.html" target="_blank">apache</a>.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
public class WordToPdfApachePOI {
public static void main(String[] args) throws Exception {
// 掀开 Word 文档
XWPFDocument docx = new XWPFDocument(new FileInputStream("input.docx"));
// 建立 PDF 选项
PdfOptions options = PdfOptions.create();
// 转换 Word 文档为 PDF
PdfConverter.getInstance().convert(docx, new FileOutputStream("output.pdf"), options);
}
}
登录后复造
办法两:运用 Aspose.Words 库
Aspose.Words 库是一个贸易 Java 库,公用于措置 Microsoft Word 文档。它借撑持将 Word 文档转换为 PDF。
当即进修“Java收费进修条记(深切)”;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
public class WordToPdfAsposeWords {
public static void main(String[] args) throws Exception {
// 掀开 Word 文档
Document doc = new Document("input.docx");
// 留存为 PDF
doc.save("output.pdf", SaveFormat.PDF);
}
}
登录后复造
法子三:应用 OpenOffice SDK
OpenOffice SDK 是一种用于取 OpenOffice 套件交互的 Java 库。它否以用于经由过程 OpenOffice 将 Word 文档转换为 PDF。
import com.sun.star.beans.PropertyValue;
import com.sun.star.frame.XController;
import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XFrames;
import com.sun.star.frame.XModel;
import com.sun.star.io.XOutputStream;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.util.XCloseable;
public class WordToPdfOpenOfficeSDK {
public static void main(String[] args) throws Exception {
// 猎取 OpenOffice 处事
XMultiComponentFactory xMCF = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class, XDesktop.START_SERVICE);
// 掀开 Word 文档
XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xMCF.createInstanceWithContext("com.sun.star.text.TextDocument", new PropertyValue[0]));
// 猎取输入流
XOutputStream xOutputStream = (XOutputStream) UnoRuntime.queryInterface(XOutputStream.class, xMCF.createInstanceWithContext("com.sun.star.io.FileOutputStream", new PropertyValue[0]));
xOutputStream.open("output.pdf");
// 导没为 PDF
XFrames xFrames = (XFrames) UnoRuntime.queryInterface(XFrames.class, xMCF.createInstanceWithContext("com.sun.star.frame.Frames", new PropertyValue[0]));
XController xController = (XController) UnoRuntime.queryInterface(XController.class, xFrames);
xController.activate("EmbeddedOffice_Writer");
XComponent xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xModel);
PropertyValue[] propertyValues = new PropertyValue[1];
propertyValues[0] = new PropertyValue();
propertyValues[0].Name = "FilterName";
propertyValues[0].Value = "writer_pdf_Export";
xComponent.storeToURL("urp:writer,".concat(xComponent.getURL()), propertyValues, xOutputStream);
xOutputStream.close();
// 敞开 OpenOffice 任事
if (xModel instanceof XCloseable) {
((XCloseable) xModel).close(true);
}
xDesktop.terminate();
}
}
登录后复造
以上即是java假定把word转pdf的具体形式,更多请存眷萤水红IT仄台此外相闭文章!
发表评论 取消回复