当前位置: 移动技术网 > IT编程>开发语言>Java > Java 将PDF 转为Word、图片、SVG、XPS、Html、PDF/A

Java 将PDF 转为Word、图片、SVG、XPS、Html、PDF/A

2019年07月23日  | 移动技术网IT编程  | 我要评论

本文将介绍通过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文件包。下载后,解压文件,并将lib文件夹下的spire.pdf.jar文件导入java程序。

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

 

java代码示例

【示例1pdf word

pdfdocument pdf = new pdfdocument("test.pdf");
pdf.savetofile("toword.docx",fileformat.docx);

【示例2pdf转图片

支持的图片格式包括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();
    }
}

【示例3pdfhtml

pdfdocument pdf = new pdfdocument("test.pdf");
pdf.savetofile("tohtml.html", fileformat.html);

【示例4pdfsvg

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

【示例5pdf xps

pdfdocument pdf = new pdfdocument("test.pdf");
pdf.savetofile("toxps.xps", fileformat.xps);

【示例6pdfpdf/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();

    }
}

(本文完)

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网