当前位置: 首页 > news >正文

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);// 保存 PDFdoc.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);// 将每一页转为 JPEGfor (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。

相关文章:

  • Carlink 技术:搭建汽车与手机的智能桥梁
  • react+ts中函数组件父子通信方式
  • React Fiber
  • Canal mysql to mysql 增加 online 库同步配置指南
  • 【基础】Python包管理工具uv使用全教程
  • 13前端项目----购物车修改
  • MySQL初阶:基础增删改查(CRUD)
  • vue3使用轮播图组件swiper
  • 2.Redis高阶实战
  • On the Biology of a Large Language Model——论文学习笔记——拒答和越狱
  • 点分治解析
  • Python __new__ 一个特殊的静态方法
  • 使用Windows+Linux实现mysql的主从复制
  • LangChain入门(六)Agent
  • day5:nginx代理-动静分离
  • 【了解】通感算一体化网络
  • Selenium模拟人类行为,操作网页的方法(全)
  • 每日算法-250506
  • 大模型系列(三)--- ​ GPT1: Improving Language Understanding by Generative Pre-Training​
  • 【HarmonyOS 5】鸿蒙用户头像编辑功能实践
  • 上海证监局规范辖区私募经营运作,6月15日前完成自评自纠
  • 新疆维吾尔自治区乌鲁木齐市米东区政协原副主席朱文智被查
  • 人民日报钟声:中方维护自身发展利益的决心不会改变
  • 从黄土高原到黄浦江畔,澄城樱桃品牌推介会明日在上海举办
  • 全军军级以上单位新任纪委书记监委主任培训班结业
  • 住宿行业迎“最火五一”:数千家酒店连续3天满房,民宿预订量创历史新高