aspose-pdf 渗透版

90 阅读1分钟

Java API to Process & Manipulate PDF Files

mvnrepository.com/artifact/co…


<dependency>
    <groupId>com.luhuiguo</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>23.1</version>
</dependency>

pdf 转 word

测试代码

package com.lcw.one.main.test;

import com.aspose.pdf.Document;
import com.aspose.pdf.SaveFormat;

public class PdfToWordConverter {
    public static void main(String[] args) {
        // 输入的 PDF 文件路径
        String inputFilePath = "/Users/snackpub/Desktop/temp-files/许可.pdf";
        // 输出的 Word 文件路径
        String outputFilePath = "/Users/snackpub/Desktop/temp-files/许可.docx";

        try {
            // 加载 PDF 文档
            Document pdfDocument = new Document(inputFilePath);
            // 将 PDF 文档保存为 Word 格式
            pdfDocument.save(outputFilePath, SaveFormat.DocX);
            System.out.println("PDF 转换为 Word 成功。");
        } catch (Exception e) {
            System.out.println("转换过程中出现错误: " + e.getMessage());
        }
    }

}

上述测试代码转换一个21页的pdf文件(164KB),经测试全部转换word文件成功!

pdf文件如下 image.png

word文件如下

image.png

html转word

package com.lcw.one.main.test;

import com.aspose.pdf.Document;
import com.aspose.pdf.HtmlLoadOptions;
import com.aspose.pdf.SaveFormat;

public class HtmlToPdf {
    public static void main(String[] args) {
        // 设置许可证(如果需要)
        // new com.aspose.pdf.License().setLicense("Aspose.Total.lic");

        // 定义 HTML 内容
        String htmlContent = "<h1>Hello, World!</h1><p>This is a sample HTML content.</p>";

        // 创建 HTML 加载选项
        HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();

        // 将 HTML 字符串加载到 Document 对象
        Document pdfDocument = new Document(new java.io.ByteArrayInputStream(htmlContent.getBytes()), htmlLoadOptions);

        // 保存 PDF 文件
        pdfDocument.save("/Users/snackpub/Desktop/temp-files/HtmlToPdf.pdf", SaveFormat.Pdf);

        System.out.println("PDF created successfully.");
    }
}

image.png

仅供研究使用!