Pdf转Word案例(java)

Pdf转Word案例(java)

需要导入aspose-pdf.jar 需要先手动下载jar包到本地,然后通过systemPath在pom文件中引入。

下载地址:https://releases.aspose.com/java/repo/com/aspose/aspose-pdf/25.4/

复制代码
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-pdf</artifactId>
            <version>25.4</version>
            <scope>system</scope>
            <systemPath>C:/Users/aaa/Downloads/aspose-pdf-25.4.jar</systemPath>
        </dependency>

主要功能总结

功能 描述
PDF 创建与编辑 创建、添加文本/图像/表格等
格式转换 PDF ↔ Word/Excel/HTML/Image
合并与拆分 合并多个 PDF 或按页拆分
安全保护 加密/解密、权限设置
表单处理 动态表单字段的创建与填充
内容提取 提取文本、图像、元数据
高级操作 水印、注释、页眉页脚、压缩优化

如果需要word转pdf,需要使用aspose-words.jar包。


1. 创建 PDF 文件

复制代码
import com.aspose.pdf.Document;
import com.aspose.pdf.Page;
import com.aspose.pdf.TextFragment;

public class CreatePdf {
    public static void main(String[] args) {
        // 创建空 PDF 文档
        Document doc = new Document();
        
        // 添加页面
        Page page = doc.getPages().add();
        
        // 添加文本
        TextFragment text = new TextFragment("Hello, Aspose.PDF!");
        text.getTextState().setFontSize(14);
        page.getParagraphs().add(text);
        
        // 保存 PDF
        doc.save("output.pdf");
    }
}

2. PDF 转 Word (DOCX)

复制代码
import com.aspose.pdf.Document;
import com.aspose.pdf.SaveFormat;

public class PdfToWord {
    public static void main(String[] args) {
        Document doc = new Document("input.pdf");
        doc.save("output.docx", SaveFormat.DocX);
    }
}

3. PDF 转 HTML

复制代码
import com.aspose.pdf.Document;
import com.aspose.pdf.HtmlSaveOptions;
import com.aspose.pdf.SaveFormat;

public class PdfToHtml {
    public static void main(String[] args) {
        Document doc = new Document("input.pdf");
        HtmlSaveOptions options = new HtmlSaveOptions();
        doc.save("output.html", options);
    }
}

4. PDF 转图像(PNG/JPEG)

复制代码
import com.aspose.pdf.Document;
import com.aspose.pdf.devices.JpegDevice;
import com.aspose.pdf.devices.Resolution;

public class PdfToImage {
    public static void main(String[] args) {
        Document doc = new Document("input.pdf");
        
        // 设置分辨率(300 dpi)
        Resolution resolution = new Resolution(300);
        JpegDevice device = new JpegDevice(resolution);
        
        // 将每一页转为 JPEG
        for (int i = 1; i <= doc.getPages().size(); i++) {
            device.process(doc.getPages().get_Item(i), "page_" + i + ".jpg");
        }
    }
}

5. 合并多个 PDF 文件

复制代码
import com.aspose.pdf.Document;
import com.aspose.pdf.facades.PdfFileEditor;

public class MergePdf {
    public static void main(String[] args) {
        PdfFileEditor editor = new PdfFileEditor();
        
        // 合并两个 PDF 文件
        editor.concatenate("input1.pdf", "input2.pdf", "merged.pdf");
    }
}

6. 拆分 PDF 文件

复制代码
import com.aspose.pdf.facades.PdfFileEditor;

public class SplitPdf {
    public static void main(String[] args) {
        PdfFileEditor editor = new PdfFileEditor();
        
        // 按页码拆分(例如:拆分前3页)
        editor.extract("input.pdf", 1, 3, "split.pdf");
    }
}

7. 添加水印

复制代码
import com.aspose.pdf.*;
import com.aspose.pdf.facades.WatermarkArtifact;

public class AddWatermark {
    public static void main(String[] args) {
        Document doc = new Document("input.pdf");
        
        // 添加水印文本
        WatermarkArtifact watermark = new WatermarkArtifact();
        watermark.setText("Confidential");
        watermark.getTextState().setFontSize(48);
        watermark.getTextState().setFont(FontRepository.findFont("Arial"));
        watermark.setOpacity(0.5);
        
        // 添加到每一页
        for (Page page : doc.getPages()) {
            page.getArtifacts().add(watermark);
        }
        
        doc.save("output.pdf");
    }
}

8. 加密/解密 PDF

复制代码
import com.aspose.pdf.Document;
import com.aspose.pdf.facades.DocumentPrivilege;

public class EncryptPdf {
    public static void main(String[] args) {
        Document doc = new Document("input.pdf");
        
        // 设置密码和权限
        doc.encrypt("user_pass", "owner_pass", 
            DocumentPrivilege.getPrint(), 
            CryptoAlgorithm.AESx256);
        
        doc.save("encrypted.pdf");
    }
}

9. 提取 PDF 文本

复制代码
import com.aspose.pdf.Document;
import com.aspose.pdf.TextAbsorber;

public class ExtractText {
    public static void main(String[] args) {
        Document doc = new Document("input.pdf");
        TextAbsorber absorber = new TextAbsorber();
        
        // 提取所有页面文本
        doc.getPages().accept(absorber);
        String extractedText = absorber.getText();
        
        System.out.println(extractedText);
    }
}

10. 添加表单字段

复制代码
import com.aspose.pdf.*;

public class AddFormField {
    public static void main(String[] args) {
        Document doc = new Document();
        Page page = doc.getPages().add();
        
        // 添加文本框
        TextBoxField textBox = new TextBoxField(page, new Rectangle(100, 600, 200, 650));
        textBox.setPartialName("text_field");
        textBox.setValue("Default Text");
        
        doc.getForm().add(textBox);
        doc.save("form.pdf");
    }
}

11. 添加页眉/页脚

复制代码
import com.aspose.pdf.*;

public class AddHeaderFooter {
    public static void main(String[] args) {
        Document doc = new Document("input.pdf");
        
        // 添加页眉
        TextFragment header = new TextFragment("Header Text");
        header.getTextState().setFontSize(12);
        header.setHorizontalAlignment(HorizontalAlignment.Center);
        
        // 添加到每一页
        for (Page page : doc.getPages()) {
            page.getParagraphs().add(header);
        }
        
        doc.save("output.pdf");
    }
}

12. 压缩 PDF

复制代码
import com.aspose.pdf.Document;
import com.aspose.pdf.optimization.OptimizationOptions;

public class CompressPdf {
    public static void main(String[] args) {
        Document doc = new Document("input.pdf");
        
        OptimizationOptions options = new OptimizationOptions();
        options.setRemoveUnusedObjects(true);
        options.setLinkDuplcateStreams(true);
        
        doc.optimizeResources(options);
        doc.save("compressed.pdf");
    }
}

13. 处理 PDF 注释

复制代码
import com.aspose.pdf.*;

public class AddAnnotation {
    public static void main(String[] args) {
        Document doc = new Document();
        Page page = doc.getPages().add();
        
        // 添加高亮注释
        HighlightAnnotation highlight = new HighlightAnnotation(page, new Rectangle(100, 600, 200, 650));
        highlight.setTitle("Important Note");
        highlight.setColor(Color.getYellow());
        
        page.getAnnotations().add(highlight);
        doc.save("annotated.pdf");
    }
}

注意事项

  1. 许可证:未应用许可证时,生成的文件会包含评估水印。通过以下代码激活:

    复制代码
    com.aspose.pdf.License license = new com.aspose.pdf.License();
    license.setLicense("Aspose.PDF.Java.lic");
  2. 依赖管理:建议通过 Maven/Gradle 管理依赖,确保使用最新版本。

  3. 文档参考 :完整 API 文档见 Aspose.PDF for Java Documentation

相关推荐
工业互联网专业18 分钟前
基于springboot+vue的社区药房系统
java·vue.js·spring boot·毕业设计·源码·课程设计·社区药房系统
API小爬虫34 分钟前
如何用爬虫获得按关键字搜索淘宝商品
java·爬虫·python
sanx181 小时前
从零搭建体育比分网站完整步骤
java·开发语言
夏季疯1 小时前
学习笔记:黑马程序员JavaWeb开发教程(2025.3.29)
java·笔记·学习
努力也学不会java2 小时前
【HTTP】《HTTP 全原理解析:从请求到响应的奇妙之旅》
java·网络·网络协议·http
Ten peaches2 小时前
苍穹外卖(订单状态定时处理、来单提醒和客户催单)
java·数据库·sql·springboot
caihuayuan52 小时前
全文索引数据库Elasticsearch底层Lucene
java·大数据·vue.js·spring boot·课程设计
冼紫菜2 小时前
Spring 项目无法连接 MySQL:Nacos 配置误区排查与解决
java·spring boot·后端·mysql·docker·springcloud
yuren_xia3 小时前
Spring MVC中跨域问题处理
java·spring·mvc