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

相关推荐
夜晚中的人海2 分钟前
【C++】智能指针介绍
android·java·c++
正在走向自律22 分钟前
RSA加密从原理到实践:Java后端与Vue前端全栈案例解析
java·前端·vue.js·密钥管理·rsa加密·密钥对·aes+rsa
咯哦哦哦哦40 分钟前
关于QT 打印中文 乱码问题
java·数据库·qt
爱读源码的大都督1 小时前
天下苦@NonNull久矣,JSpecify总算来了,Spring 7率先支持!
java·后端·架构
木头没有瓜1 小时前
Slf4j 接口文档左侧菜单有显示,但是点击后空白
java
野犬寒鸦1 小时前
从零起步学习Redis || 第十二章:Redis Cluster集群如何解决Redis单机模式的性能瓶颈及高可用分布式部署方案详解
java·数据库·redis·后端·缓存
cxyxiaokui0012 小时前
还在用 @Autowired 字段注入?你可能正在写出“脆弱”的 Java 代码
java·后端·spring
珹洺2 小时前
Java-Spring入门指南(二十二)SSM整合前置基础
java·开发语言·spring
天天摸鱼的java工程师2 小时前
Java IO 流 + MinIO:游戏玩家自定义头像上传(格式校验、压缩处理、存储管理)
java·后端
程序员小富2 小时前
改了 Nacos 一行配置,搞崩线上支付系统!
java·后端