-
Java 将PDF 转为Word、图片、SVG、XPS、Html、PDF/A
本文将介绍通过Java编程来实现PDF文档转换的方法。包括:
1. PDF转为Word
2. PDF转为图片
3. PDF转为Html
4. PDF转为SVG
4.1 将PDF每一页转为单个的SVG
4.2 将一个包含多页的PDF文档转为一个SVG
5. PDF转为XPS
6. PDF转为PDF/A
使用工具:Free Spire.PDF for Java(免费版)
Jar文件获取及导入:
方法1:通过官网下载jar https://www.e-iceblue.cn/Downloads/Free-Spire-PDF-JAVA.html文件包。下载后,解压文件,并将lib文件夹下的Spire.Pdf.jar文件导入Java程序。
方法2:可通过maven仓库安装导入。参考导入方法https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html
Java代码示例
【示例1】PDF 转Word
PdfDocument pdf = new PdfDocument("test.pdf"); pdf.saveToFile("ToWord.docx",FileFormat.DOCX);
【示例2】PDF转图片
支持的图片格式包括Jpeg, Jpg, Png, Bmp, Tiff, Gif, EMF等。这里以保存为Png格式为例。
import com.spire.pdf.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class PDFtoimage { public static void main(String[] args) throws IOException { PdfDocument pdf = new PdfDocument("test.pdf"); BufferedImage image; for(int i = 0; i< pdf.getPages().getCount();i++){ image = pdf.saveAsImage(i); File file = new File( String.format("ToImage-img-%d.png", i)); ImageIO.write(image, "PNG", file); } pdf.close(); } }
【示例3】PDF转Html
PdfDocument pdf = new PdfDocument("test.pdf"); pdf.saveToFile("ToHTML.html", FileFormat.HTML);
【示例4】PDF转SVG
1.转为单个svg
PdfDocument pdf = new PdfDocument("test.pdf"); pdf.saveToFile("ToSVG.svg", FileFormat.SVG);
2.多页pdf转为一个svg
PdfDocument pdf = new PdfDocument("sampe.pdf"); pdf.getConvertOptions().setOutputToOneSvg(true); pdf.saveToFile("ToOneSvg.svg",FileFormat.SVG);
【示例5】PDF 转XPS
PdfDocument pdf = new PdfDocument("test.pdf"); pdf.saveToFile("ToXPS.xps", FileFormat.XPS);
【示例6】PDF转PDF/A
import com.spire.pdf.*; import com.spire.pdf.graphics.PdfMargins; import java.awt.geom.Dimension2D; public class PDFtoPDFA { public static void main(String[]args){ //加载测试文档 PdfDocument pdf = new PdfDocument(); pdf.loadFromFile("test.pdf"); //转换为Pdf_A_1_B格式 PdfNewDocument newDoc = new PdfNewDocument(); newDoc.setConformance(PdfConformanceLevel.Pdf_A_1_B); PdfPageBase page; for ( int i=0;i< pdf.getPages().getCount();i++) { page = pdf.getPages().get(i); Dimension2D size = page.getSize(); PdfPageBase p = newDoc.getPages().add(size, new PdfMargins(0)); page.createTemplate().draw(p, 0, 0); } //保存结果文件 newDoc.save("ToPDFA.pdf"); newDoc.close(); } }
出 处:https://www.cnblogs.com/Yesi/p/11233238.html
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比