Word文档或PDF转图片
首先把Word文档转为pdf,方法网上很多,比如用jacob、poi、pdfbox、xpdf、OpenOffice+JodConverter(Openoffice)等。
现在主要讲pdf转图片方法:
1、 Icepdf(http://www.icepdf.org/)
功能强大,转图片,直接搜索pdf,提取文本等。
例子,其中包在官方下载:
package com.test;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;
public class TestPdfToGif3 {
public static void main(String[] args) {
String p=System.getProperty("user.dir") + "/"+"333.pdf";
String filePath = p;
Document document = new Document();
try {
document.setFile(filePath);
} catch (Exception ex) {
}
// save page caputres to file.
float scale = 1.3f;
float rotation = 0f;
// Paint each pages content to an image and write the image to file
for (int i = 0; i < document.getNumberOfPages(); i++) {
BufferedImage image = (BufferedImage)
document.getPageImage(i,GraphicsRenderingHints.SCREEN,Page.BOUNDARY_CROPBOX, rotation, scale);
RenderedImage rendImage = image;
// capture the page image to file
try {
System.out.println("/t capturing page " + i);
File file = new File("imageCapture1_" + i + ".png");
ImageIO.write(rendImage, "png", file);
} catch (IOException e) {
e.printStackTrace();
}
image.flush();
}
// clean up resources
document.dispose();
}
}
2、 pdf-renderer(https://pdf-renderer.dev.java.net/)
转图片例子,其中包官方下载:
package com.test;
import java.awt.Color;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
/**
* 测试pdf转图片,https://pdf-renderer.dev.java.net/
*/
public class TestPdfToGif2 {
private static Logger log = Logger.getLogger(TestPdfToGif2.class);
private String pdfName;
public String getPdfName() {
return pdfName;
}
public void setPdfName(String pdfName) {
this.pdfName = pdfName;
}
public String getBasePath() {
return basePath;
}
public void setBasePath(String basePath) {
this.basePath = basePath;
}
private String fileNameNoExtMD5;
private String basePath;
/**
* 测试
*
* @param args
*/
public static void main(String[] args) {
try {
TestPdfToGif2 pg = new TestPdfToGif2();
pg.setPdfName("333");
pg.setBasePath(System.getProperty("user.dir") + "/");
pg.fileNameNoExtMD5 = pg.basePath + pg.getPdfName();
pg.pdfToGIF("gif");
} catch (Exception e) {
e.printStackTrace();
}
log.info("程序运行完了!");
}
/**
* 建立PDF文档读取类
*
* @param filePath
* PDF文件的路径
* @return null 或者PDFFile instance
*/
private static PDFFile getPdfFile(String filePath) {
try {
File file = new File(filePath);
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0,
channel.size());
PDFFile pdffile = new PDFFile(buf);
return pdffile;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
/**
* pdf文件转成gif文件 转换成功后一定要记得写磁盘
*
* @throws IOException
*/
private void pdfToGIF(String suff) throws IOException {
PDFFile pdffile =getPdfFile(fileNameNoExtMD5 + ".pdf");
int pages = pdffile.getNumPages();
File gifFile = null;
BufferedImage contImage = null;
FileOutputStream fos = null;
String giffilename = "";
for (int i = 0; i < pages; i++) {
try {
giffilename = fileNameNoExtMD5 + "." + i + ".gif";
gifFile = new File(giffilename);
if (gifFile.exists()) {
continue;
}
fos = new FileOutputStream(gifFile);
PDFPage page = pdffile.getPage(i);
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()
.getWidth(), (int) page.getBBox().getHeight());
// generate the image
Image img = page.getImage(rect.width, rect.height, rect, null,
true, true);
contImage = new BufferedImage(rect.width, rect.height,
BufferedImage.TYPE_INT_ARGB);
contImage.getGraphics().drawImage(img, 0, 0, Color.WHITE, null);
ImageIO.write(contImage, suff, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
fos.flush();
fos.close();
}
}
}
}
3、 jpedal(http://www.jpedal.org/)
有些问题,比如一些字生成图片是一块黑的。不过还是附上例子和jar包jpedal_gpl.jar(把上传的图片后缀改一下jar),可能用到的其他包请另外下载可能用到的其他包:
commons-cli-1.2.jar
commons-io-1.4.jar
FontBox-0.1.0-dev.jar
juh.jar
jurt.jar
PDFBox-0.7.3.jar
poi-3.5-beta6-20090622.jar
poi-contrib-3.5-beta6-20090622.jar
poi-ooxml-3.5-beta6-20090622.jar
poi-scratchpad-3.5-beta6-20090622.jar
ridl.jar
unoil.jar
xmlbeans-2.3.0.jar
xstream-1.3.1.jar
package com.test;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
import org.jpedal.PdfDecoder;
public class TestPdfToGif1 {
private static Logger log = Logger.getLogger(TestPdfToGif1.class);
private String pdfName;
public String getPdfName() {
return pdfName;
}
public void setPdfName(String pdfName) {
this.pdfName = pdfName;
}
public String getBasePath() {
return basePath;
}
public void setBasePath(String basePath) {
this.basePath = basePath;
}
private String fileNameNoExtMD5;
private String basePath;
/**
* 测试
* @param args
*/
public static void main(String[] args) {
try {
TestPdfToGif1 pg = new TestPdfToGif1();
pg.setPdfName("333");
pg.setBasePath(System.getProperty("user.dir") + "/");
pg.fileNameNoExtMD5 = pg.basePath + pg.getPdfName();
pg.pdfToGIF();
} catch (Exception e) {
e.printStackTrace();
}
log.info("程序运行完了!");
}
private void pdfToGIF() {
StringBuffer errorpages = new StringBuffer();
int pages = 0;
String giffilename = "";
PdfDecoder decodePdf = new PdfDecoder(true);
BufferedImage imageToSave = null;
BufferedImage contImage = null;
FileOutputStream fos = null;
File gifFile = null;
int i = 0;
try {
decodePdf.openPdfFile(fileNameNoExtMD5 + ".pdf");
//decodePdf
pages = decodePdf.getPageCount();
int h = decodePdf.getPDFHeight();
int w = decodePdf.getPDFWidth();
log.info("h = " + h);
log.info("w = " + w);
for (; i < pages; i++) {
try {
giffilename = fileNameNoExtMD5 + "." + i + "_.gif";
gifFile = new File(giffilename);
if (!gifFile.exists()) {
int tmpw, tmph;
fos = new FileOutputStream(gifFile);
imageToSave = decodePdf.getPageAsImage(i + 1);
tmpw = imageToSave.getWidth();
tmph = imageToSave.getHeight();
log.info("h = " + tmph);
log.info("w = " + tmpw);
contImage = new BufferedImage(tmpw, tmph,
BufferedImage.TYPE_INT_ARGB);
contImage.getGraphics().drawImage(imageToSave, 0, 0,
Color.WHITE, null);
ImageIO.write(contImage, "gif", fos);
fos.flush();
fos.close();
//convertInfo.setTotalPage(i + 1);
// savestatus();// 不要每次都写磁盘
}
} catch (Exception e) {
errorpages.append(i).append(",");
}
}
if (errorpages.length() > 1) {
errorpages.setLength(errorpages.length() - 1);
}
//convertInfo.setErrorpages(errorpages.toString());
if (errorpages.length() > 0) {
// convertInfo.setErrorcode(3);
} else {
//convertInfo.setErrorcode(2);
}
} catch (Exception e) {
log.error(e.getMessage());
//convertInfo.setErrorcode(6);
} finally {
fos = null;
decodePdf.closePdfFile();
contImage = null;
imageToSave = null;
decodePdf = null;
gifFile = null;
}
}
}
附件: http://hi.csdn.net/attachment/201005/28/0_12750380531542.gif
Word文档或PDF转图片相关推荐
- azw3转换为pdf_干货:如何Java 将 Word 文档转换为 PDF
在日常工作中,PDF格式良好的视觉阅读性和稳定性使其使用越来越广泛.因此我们常会遇到需要将成型的Word文档转换为PDF格式的情况.本文就将通过使用Java程序来演示如何将Word文档转换成PDF格式 ...
- mysql导出表结构word文档_如何将Word文档导出为长图片格式
如何对word文档转化成潮图片格式呢?给大家分享一下,将word文档转化成图片具体方法,对大家能有所帮助. 方法/步骤 1 首先,如果想将word文档转化成图片格式文档,我们可以直接用word程序来实 ...
- 简要介绍word文档转换为pdf格式文档的工具
找了很多工具,其实都是乱七八糟的,没几个好用的,最好还是用Adobe Acrobat Pro吧,这个就很方便了,而且转换的也不错. ABC Amber PDF Converter ABC Amber ...
- 如何将Word文档每页转换为图片保存?
我们在日常的工作和学习中,将Word文档的每一页转换为图片似乎并不多见,但是如果遇到需要这种操作时似乎会难到很到人,因为Word文档不具有直接保存为图片形式的功能.有时候我们需要将每一页的Word文档 ...
- Word文档转PDF的功能
最近项目中有用到Word文档转PDF的功能,做了一些尝试,也遇到了一些困难. 下面把尝试的情况记录下来,也希望做过类似工作的童鞋能一起探讨一下. http://www.iteye.com/topi ...
- java实现word文档转换pdf文档并且添加水印功能使用插件Aspose.Words
前段时间,项目需要自动生成word文档,用WordFreeMarker生成word文档后,又要求生成的文档能在浏览器浏览,思来想去,把word文档转成pdf就好了,于是乎研究了一下. 将word文档转 ...
- word文档转pdf,支持.doc和.docx
word文档有需要兼容.doc和.docx两种文档格式.其中.docx通过poi直接就可以将word转成pdf,.doc则无法这样实现,上网查询很多资料,大概思路是正确的,既将.doc文档转成html ...
- word文档转为PDF以及多种文档格式转换
word文档转为PDF以及多种文档格式转换 项目地址:https://gitee.com/Jakewabc/word-of-pdf.git 相关案例: https://github.com/aspos ...
- word doc怎么显示base64图片_win10系统word文档打印不出图片怎么办
word文档帮助用户更好排版和保存文字,白领都经常用到此软件,可以说是电脑必不可少的一款办公软件.Win10系统编辑好Word文档后准备打印,发现在打印预览里也没有图片 ,真实打印word文档也没图片 ...
最新文章
- 花了3个月整理的超级全面的Python资料和Java面试题,分享给大家!
- xadmin2 django 搭建学生系统 model层
- 012 背包二叉树遍历分析和代码编写
- 蓝桥杯C++ AB组辅导课 第六讲 双指针、BFS与图论 Acwing
- Hadoop 停止hdfs和yarn的命令
- solr创建索引_Solr:创建拼写检查器
- 如何用python做计算软件_如何用Python写一个计算器软件 附带效果图
- c语言程序设计课件数组,数组(C语言程序设计)课件
- C++_二维数组_定义方式_数组名称的作用_案例考试成绩统计---C++语言工作笔记021
- AFNetworking下 http 改 https后遇到出现Error Domain=NSURLErrorDomain Code=-999 已取消 错误...
- 小知识--电脑隐藏文件
- 【密码专栏】动手计算双线性对(上)
- 怎么把linux装入移动硬盘,把Ubuntu装进你的移动硬盘中
- MYSQL设置初始密码
- 二进制数据文件中的字符串替换的实现(C++)
- 最后一天了(再见了)
- 你知道吗?什么是 Responsive JavaScript ?
- C# 接口和抽象类的区别和使用场景
- 服务器系统重启和断电重启,服务器设置断电重启吗
- 常见的USB接口种类以及区别