how to convert pdf to jpeg or pdf to jpg using java program

/
4 Comments
hai friends today iam sharing some example to convert a pdf file to jpeg using java program. below java program will helpful to convert a pdf document to jpeg or jpg image.i think this code will helpful to somebody who want to convert pdf to jpeg file.

package johnmukkad.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 multivalent.Behavior;
import multivalent.Context;
import multivalent.Document;
import multivalent.Node;
import multivalent.std.adaptor.pdf.PDF;

public class PDFToJPEG extends HttpServlet {

public PDFToJPEG(){

}
/*public static void main(String args[]) {
File outfile = new File("file1.jpg");

try {

PDF pdf = (PDF) Behavior.getInstance("AdobePDF", "AdobePDF", null,
null, null);
File file = new File("test2.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, "jpg", outfile);
doc.removeAllChildren();
cx.reset();
g.dispose();

pdf.getReader().close();
outfile = null;

doc = null;
} catch (Exception e) {
}
}*/

public boolean convertPdfToJpeg(String input, String output){

boolean blnSuccess = false;
File outfile = new File(output);

PDF pdf = (PDF) Behavior.getInstance("AdobePDF", "AdobePDF", null,null, null);
File infile = new File(input);
pdf.setInput(infile);

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 intImageWidth = top.bbox.width;
int intImageHeight = top.bbox.height;
BufferedImage theNewImage = new BufferedImage(intImageWidth, intImageHeight,BufferedImage.TYPE_INT_RGB);
Graphics2D loGraphics = theNewImage.createGraphics();
loGraphics.setClip(0, 0, intImageWidth, intImageWidth);
loGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
loGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
loGraphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
Context cx = doc.getStyleSheet().getContext(loGraphics, null);
top.paintBeforeAfter(loGraphics.getClipBounds(), cx);
try{
ImageIO.write(theNewImage, "jpg", outfile);
theNewImage = null;
blnSuccess = true;
}catch(ImageCreateException e){
theNewImage = null;
return(blnSuccess);
}

doc.removeAllChildren();
cx.reset();
loGraphics.dispose();

pdf.getReader().close();
outfile = null;

doc = null;
return(blnSuccess);
}

}



You may also like

4 comments:

  1. Thanks a lot.

    This is the only code i found which works and provided with no cost.

    ReplyDelete
  2. Hello,

    Can you tellme how to create an image with bigger size? I tried to change the w, h but without luck... the problem is that the images im getting are small, and when I zoom them, i can see they have no quality at all.

    thnak you

    ReplyDelete
  3. Hello,

    Can you tellme how to create an image with bigger size? I tried to change the w, h but without luck... the problem is that the images im getting are small, and when I zoom them, i can see they have no quality at all.

    thnak you

    ReplyDelete
  4. I get error at catch(ImageCreateException e) ...cannot find symbol

    ReplyDelete