博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 将PDF 转为Word、图片、SVG、XPS、Html、PDF/A(亲测有效)
阅读量:3897 次
发布时间:2019-05-23

本文共 2273 字,大约阅读时间需要 7 分钟。

本文将介绍通过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:通过官网下载。下载后,解压文件,并将lib文件夹下的Spire.Pdf.jar文件导入Java程序。

方法2:可通过maven仓库安装导入。参考。

(肯定是用maven导入更方便的吧哈哈哈哈)

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(); }}

本篇博客参考自:

转载地址:http://kifen.baihongyu.com/

你可能感兴趣的文章
【设计模式基础】行为模式 - 3 - 职责链(Chain of responsibility)
查看>>
【Java基础】反射 - Reflection
查看>>
【C++基础】const成员函数
查看>>
【设计模式基础】行为模式 - 5 - 策略(Strategy)
查看>>
【Maven】Archetype
查看>>
【Java.Web】Cookie —— 基础
查看>>
【Tools.Eclipse】代码自动提示
查看>>
【Java.Web】MVC —— Model1 V.S. Model2
查看>>
【Java.Web】MVC —— 基于Servlet Controller的Model2 —— 示例
查看>>
【Java.Web】MVC —— 基于Filter Dispatcher的Model2 —— 示例
查看>>
【Java.Web】MVC —— Action的验证器 —— Validator
查看>>
【Java.Spring.MVC】使用Spring MVC构建Web应用程序
查看>>
【DB.PL/SQL】程序流程控制 —— 异常处理
查看>>
【Java.IO】I/O 【字节】【处理流】 - 之 - 【压缩流】 - ZipInputStream,ZipOutputStream
查看>>
【Java.JDBC/ORM】纯JDBC系统的开发随想
查看>>
【Unix/Linux】【系统】环境变量
查看>>
【Architecture】CPU-bound(计算密集型) 和I/O bound(I/O密集型)
查看>>
【MacOS】Mac 系统下类似于 apt-get 的软件包管理器 -- Homebrew
查看>>
为窗口添加鼠标HOVER和LEAVE事件
查看>>
VC小技巧20个
查看>>