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

相关推荐
en.en..2 分钟前
Linux mmap 内存映射深度解析:基于帧缓冲 /dev/fb0
java·服务器·前端
洋不写bug4 分钟前
链表补充练习,双链表的模拟实现
java·数据结构·链表·双链表·底层实现
后台模板学习8 分钟前
用 IM 即时聊天项目一次讲清消息去重算法的踩坑与解决方案
java·数据库·spring
秋名RG10 分钟前
2026/6/15 系统故障复盘与整改方案
java·架构
今天要早睡_12 分钟前
C++ 核心语法速过:命名空间、引用、函数重载与 nullptr 深度解析
android·java·c++
南讯股份Nascent15 分钟前
2026年CRM技术选型观察:当“数据打通“成为第一需求,电商CRM的架构价值在哪里
java·大数据·用户运营
心易行者20 分钟前
Python自动化测试7步落地法:用python在线运行省掉90%环境配置时间
java·开发语言·人工智能·python·log4j·ai编程
霸道流氓气质1 小时前
Spring AI 技术细节:VectorStore 多库统一抽象
java·人工智能·spring
今天AI了吗1 小时前
Codex 配置自定义 AI API 完整指南:从零到一接入你的专属模型
java·人工智能·python·数据分析·embedding
学长毕业设计1 小时前
基于SpringBoot的健康食谱管理系统的设计与实现(源码+文档+讲解视频)
java·spring boot·后端