Java word转pdf方法

本文介绍了如何在Java项目中引入破解版Aspose.Word 18.10 jar包,以避免在Word转PDF过程中出现水印。首先,需要在项目resource文件夹下添加许可文件license.xml,然后在代码中设置许可并调用Aspose库进行转换。提供的代码示例展示了具体的转换方法。

1、引入破解版Aspose Word 18.10 jar
若不是破解版的Jar包,word转成pdf之后,pdf中会有水印。可以自己在网上找破解的Jar包资源。

lib:存放Jar包的位置。
source:存放许可文件,格式为xml。

2、在项目resource文件夹下添加license.xml许可文件

<License>
  <Data>
    <Products>
      <Product>Aspose.Total for Java</Product>
    </Products>
    <EditionType>Enterprise</EditionType>
    <SubscriptionExpiry>29991231</SubscriptionExpiry>
    <LicenseExpiry>29991231</LicenseExpiry>
    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
  </Data>
  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

2、word转pdf工具类
import java.io.File;
import java.io.InputStream;
import java.io.FileOutputStream;

import com.aspose.words.License;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;

public class WordToPdf {
public static String convert(String filePath, String fileName, String urlRoot) throws Exception {
setLicense();
String name = fileName.substring(0, fileName.lastIndexOf("."));
String pdfPath = String.format("%s\%s.pdf", filePath, name);
String wordPath = String.format("%s\%s", filePath, fileName);

    File file = new File(pdfPath);
    FileOutputStream stream = new FileOutputStream(file);

    Document doc = new Document(wordPath);
    doc.save(stream, SaveFormat.PDF);
    stream.close();

    String url = String.format("%s\\%s", urlRoot, getRelativePath(pdfPath));
    return Helper.normalizeUrl(url);
}

private static String getRelativePath(String path) {
    String truncatePath = path.substring(path.lastIndexOf("uploads"));
    return truncatePath.substring(truncatePath.indexOf("\\"));
}

private static void setLicense() throws Exception {
    InputStream stream = WordToPdf.class.getClassLoader().getResourceAsStream("static/aspose/license.xml");
    License license = new License();
    license.setLicense(stream);
}

}

### WordPDFJava实现方法Java中实现Word文档换为PDF方法有多种,其中一些方法支持跨平台使用。以下是几种常见的实现方式: 1. **Apache POI + iText 或 Apache PDFBox**: - 使用Apache POI库来读取Word文档的内容,然后通过iText或Apache PDFBox库将内容换为PDF格式。这种方法的优点是可以完全用Java编写,因此具有良好的跨平台兼容性。不过需要注意的是,这两种库在处理复杂的Word文档格式时可能会有所限制[^1]。 2. **LibreOffice 无头模式**: - 利用LibreOffice的无头模式进行换,这种方法可以在Windows和Linux环境下运行,并且能够很好地支持复杂的Word文档格式。但是,它需要在服务器上安装LibreOffice,并且会占用较多的系统资源[^4]。 3. **Aspose.Words**: - Aspose.Words是一个商业库,提供了高质量的换功能,并且支持跨平台使用。尽管它是商业软件,但对于需要高质量换的企业级应用来说,这是一个不错的选择[^4]。 4. **Free Spire.Doc**: - Free Spire.Doc是一个免费但有限制的库,适用于非商业项目或者测试用途。它允许用户在没有安装Microsoft Office的情况下将Word文档换为PDF。但是,生成的PDF可能会带有水印[^4]。 对于在线预览功能,可以考虑使用Mozilla的PDF.js库,在浏览器中直接查看PDF内容,这同样是一个跨平台的解决方案。 ### 示例代码 以下是一个简单的示例,展示如何使用Apache POI和iText来Word文档到PDF格式。请注意,这个示例仅用于演示目的,实际应用中可能需要更复杂的处理来保持文档格式的一致性。 ```java import org.apache.poi.hwpf.HWPFDocument; import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; public class WordToPDFConverter { public static void convert(String inputPath, String outputPath) { try (FileInputStream fis = new FileInputStream(inputPath); HWPFDocument doc = new HWPFDocument(fis)) { Document pdfDoc = new Document(); PdfWriter.getInstance(pdfDoc, new FileOutputStream(outputPath)); pdfDoc.open(); String wordText = doc.getDocumentText(); pdfDoc.add(new Paragraph(wordText)); pdfDoc.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` ### 注意事项 - 在选择方法时,应考虑文档的复杂程度以及对格式保留的要求。 - 对于需要高度保真换的情况,推荐使用LibreOffice无头模式或Aspose.Words。 - 如果项目预算允许,Aspose.Words是一个非常强大的工具,它不仅支持换,还提供了一系列文档操作的功能。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值