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 分钟前
还在深夜加班改Bug?雷军都点赞的防重提方案来了!
java
程序猿DD29 分钟前
Spring Boot 3.5.0中文文档上线
java·数据库·spring boot
有你的冬天19834 分钟前
File—IO流
java
进击的小白菜44 分钟前
LeetCode 75. 颜色分类 - 双指针法高效解决(Java实现)
java·算法·leetcode
杨豆芽1 小时前
SpringBoot WebMvcConfigurer使用Jackson统一序列化格式化输出
java·spring boot·后端
程序猿大波1 小时前
基于Java,SpringBoot,Vue,UniAPP宠物洗护医疗喂养预约服务商城小程序管理系统设计
java·vue.js·spring boot
Nemo_XP1 小时前
C# 打印PDF的常用方法
windows·pdf·c#
Z.Virgil1 小时前
【案例95】“小”问题引发的“大”发现---记一次环境修复
java·开发语言·jvm·数据库·oracle·性能优化·tomcat
毕小宝1 小时前
PDF文件转换之输出指定页到新的 PDF 文件
python·pdf