Java MS Word 和 PDF 转换为图像 (png/jpg)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28846877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java MS Word and PDF conversion to image (png/jpg)
提问by Peter Gorski
I'm looking for a freelib to convert from MS Word, WordPerfect and PDF to image. Is anyone aware of any good, and up-to date JAVA library?
我正在寻找一个免费的库来从 MS Word、WordPerfect 和 PDF 转换为图像。有没有人知道任何好的和最新的 JAVA 库?
回答by Atish Bundhe
To convert PDF to images You can use PDFbox
将 PDF 转换为图像您可以使用PDFbox
Following is the code to convert PDF to images using pdfbox api
以下是使用pdfbox api将PDF转换为图像的代码
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageNode;
public List<String> generateImages(String pdfFile) throws IOException {
String imagePath = "/Users/$user/pdfimages/";
List <String> fileNames = new ArrayList<String>();
document = PDDocument.load(pdfFile); //// load pdf
node = document.getDocumentCatalog().getPages(); ///// get pages
List<PDPage> kids = node.getKids();
int count=0;
for(PDPage page : kids) { ///// iterate
BufferedImage img = page.convertToImage(BufferedImage.TYPE_INT_RGB,128);
File imageFile = new File(imagePath+ count++ + ".jpg");
ImageIO.write(img, "jpg", imageFile);
fileNames.add(imageFile.getName());
}
return fileNames;
}
Also another library ApachePOIcan be used to convert PDF to Images
另一个库ApachePOI可用于将 PDF 转换为图像
Here is the code sample
这是代码示例
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
public class JavaApplication12 {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream is = new FileInputStream(“D:/Presentation1.ppt”);
SlideShow ppt = new SlideShow(is);
is.close();
Dimension pgsize = ppt.getPageSize();
Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, 1);
Graphics2D graphics = img.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
graphics.setColor(Color.white);
graphics.clearRect(0, 0, pgsize.width, pgsize.height);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
// render
slide[i].draw(graphics);
// save the output
FileOutputStream out = new FileOutputStream(“slide-” + (i + 1) + “.png”);
javax.imageio.ImageIO.write(img, “png”, out);
out.close();
}
}
}
To convert MS Word to imagesyou can have look at question posted hereWhich uses JODConverter
要将MS Word转换为图像,您可以查看此处发布的问题其中使用JODConverter
JODConverter automates all conversions supported by OpenOffice.org, including
JODConverter 自动执行 OpenOffice.org 支持的所有转换,包括
- Any format to PDF o OpenDocument (Text, Spreadsheet, Presentation) to PDF o Word to PDF; Excel to PDF; PowerPoint to PDF o RTF to PDF; WordPerfect to PDF; ...
- And more o OpenDocument Presentation (odp) to Flash; PowerPoint to Flash o RTF to OpenDocument; WordPerfect to OpenDocument o Any format to HTML (with limitations) o Support for OpenOffice.org 1.0 and old StarOffice formats
- 任何格式到 PDF o OpenDocument(文本、电子表格、演示文稿)到 PDF o Word 到 PDF;Excel转PDF;PowerPoint 转 PDF 或 RTF 转 PDF;WordPerfect 转 PDF;...
- 还有更多 o OpenDocument Presentation (odp) 到 Flash;PowerPoint 到 Flash o RTF 到 OpenDocument;WordPerfect 到 OpenDocument o 任何格式到 HTML(有限制) o 支持 OpenOffice.org 1.0 和旧的 StarSuite 格式
回答by Sangram Badi
//DOC to .jpeg
package org.doc;
import java.io.File;
import com.aspose.words.Document;
import com.aspose.words.ImageSaveOptions;
import com.aspose.words.SaveFormat;
public class DocToImage {
public static void main(String[] args) {
try {
String sourcePath = "D://G.doc";
Document doc = new Document(sourcePath);
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG);
options.setJpegQuality(100);
options.setResolution(100);
options.setUseHighQualityRendering(true);
for (int i = 0; i < doc.getPageCount(); i++) {
String imageFilePath = "E://"+ "images" + File.separator + "img_" + i + ".jpeg";
options.setPageIndex(i);
doc.save(imageFilePath, options);
}
System.out.println("Done...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
/*Here is the link from where we can download latest Aspose word jar.
http://www.aspose.com/java/word-component.aspx*/
//PDF to .png
package org.pdf;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
public class PdfToImage {
public static void main(final String[] args) throws Exception {
String storagePath = "E://dd.pdf";
//Image Save Directory
String realPathtopdfImageSaveDir = "E://uploads/";
RandomAccessFile raf = new RandomAccessFile(storagePath, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
int numPgs = pdffile.getNumPages();
for (int i = 0; i < numPgs; i++) {
PDFPage page = pdffile.getPage(i);
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
Image img = page.getImage(rect.width, rect.height, rect, null, true, true);
// save it as a file
BufferedImage bImg = toBufferedImage(img);
File yourImageFile = new File(realPathtopdfImageSaveDir +File.separator + "page_" + i + ".png");
ImageIO.write(bImg, "png", yourImageFile);
}
}
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
image = new ImageIcon(image).getImage();
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
int transparency = Transparency.OPAQUE;
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
} catch (HeadlessException e) {
System.out.println("The system does not have a screen");
}
if (bimage == null) {
int type = BufferedImage.TYPE_INT_RGB;
bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}
Graphics g = bimage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
}
// jar required pdf-renderer.jar