this program is very much useful to convert a pdf file to png format using java
you can use this java program for converting pdf files to png file format.
free download and use this programe. java using pdf convertion to png file format
/**
* @author JohnJustin
www.johnjustin.tk
*
*/
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.PNGEncodeParam;
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.TIFFDecodeParam;
import com.sun.media.jai.codec.TIFFDirectory;
import multivalent.Behavior;
import multivalent.Context;
import multivalent.Document;
import multivalent.Node;
import multivalent.std.adaptor.pdf.PDF;
public class PDFToPNG extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public byte[] ConvertToPngImage(byte[] tiffRawData, HttpServletResponse res)
throws Exception {
Vector pngs = new Vector();
// set stream to the tiff url
SeekableStream tiffStream = SeekableStream.wrapInputStream(
new ByteArrayInputStream(tiffRawData), true);
// how many pages in one tiff
int pageNumber = TIFFDirectory.getNumDirectories(tiffStream);
TIFFDecodeParam decodeParam = new TIFFDecodeParam();
decodeParam.setDecodePaletteAsShorts(true);
ImageDecoder tiffDecoder = ImageCodec.createImageDecoder("tiff",
tiffStream, decodeParam);
// for (int p = 0; p < pageNumber; p ++) {
// render the current page
RenderedImage tiffPage = tiffDecoder.decodeAsRenderedImage();
PNGEncodeParam png = PNGEncodeParam.getDefaultEncodeParam(tiffPage);
// The png stream is outputted to a file. Change the directory
// accordingly.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Gets a PNG encoder.
ImageEncoder pngEncoder = ImageCodec.createImageEncoder("PNG", baos,png);
// Encodes the RenderedImage object.
pngEncoder.encode(tiffPage);
byte[] content = baos.toByteArray();
baos.close();
return content;
}
public static void main(String args[]) {
File outfile = new File("c:\\file.png");
try {
PDF pdf = (PDF) Behavior.getInstance("AdobePDF", "AdobePDF", null,
null, null);
File file = new File("c:\\somepdf.pdf");
pdf.setInput(file);
Document doc = new Document("doc", null, null);
pdf.parse(doc);
doc.clear();
doc.putAttr(Document.ATTR_PAGE, Integer.toString(1));
pdf.parse(doc);
Node top = doc.childAt(0);
doc.formatBeforeAfter(200, 200, null);
int w = top.bbox.width;
int h = top.bbox.height;
BufferedImage img = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
g.setClip(0, 0, w, h);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
Context cx = doc.getStyleSheet().getContext(g, null);
top.paintBeforeAfter(g.getClipBounds(), cx);
ImageIO.write(img, "png", outfile);
doc.removeAllChildren();
cx.reset();
g.dispose();
pdf.getReader().close();
outfile = null;
doc = null;
} catch (Exception e) {
}
}
}
3 comments:
Very good. Answer, for increase dpi? Tankyou
Hi, Thanks for sharing the code. I was searching for codes to convert pdf files to images in java and i found yours and the one i have found on Aspose website very useful. If you want your can try their code also. Below is the link of the code in java they have shared:
http://www.aspose.com/docs/display/pdfjava/Convert+PDF+pages+to+PNG+Image
Post a Comment