Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Convert pdf to tiff compression g4

843810Jul 30 2008 — edited Jul 31 2008
I need to convert a pdf file to a tiff compression g4, here is the code.

public class Pdf2Image {

static String GetJobsNum = "";
/**
* @param args
*/
@SuppressWarnings("static-access")
public static void main(String getPdf,int Seq,int JobNumber) throws SQLException {



SQLInsert sqls = new SQLInsert();

///Create a new file.
File file = new File(getPdf);
RandomAccessFile raf;

try {
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);
// draw the first page to an image
int num = pdffile.getNumPages();
for (int i = 0; i < num; i++)
{
PDFPage page = pdffile.getPage(i);

//get the width and height for the doc at the default zoom
int width=(int)page.getBBox().getWidth();
int height=(int)page.getBBox().getHeight();

Rectangle rect = new Rectangle(0,0,width,height);
int rotation=page.getRotation();
Rectangle rect1=rect;
if(rotation==90 || rotation==270)
rect1=new Rectangle(0,0,rect.height,rect.width);

//generate the image
BufferedImage img = (BufferedImage)page.getImage(
rect.width, rect.height, //width & height
rect1, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);

ImageIO.write(img, "png", new File("CopyImage" + i + ".png"));
String source = "";
try {
source = "CopyImage" + i + ".png";
} catch (Exception e) {
JOptionPane msg = new JOptionPane();
msg.showMessageDialog(msg, "Failed to obtain CopyImage0.png (Pdf2Image.java)", "Error", JOptionPane.ERROR_MESSAGE);

}
///Create a tiff file with below name.
String NewFile = "S:/GIROSOL/FaxServer/OUT/" + sqls.GetJobsNum + ".tiff";
FileOutputStream out = null;
try {
out = new FileOutputStream(NewFile);
} catch (Exception e) {
JOptionPane msg = new JOptionPane();
msg.showMessageDialog(msg, "Failed to create job file (Pdf2Image.java)", "Error", JOptionPane.ERROR_MESSAGE);
}
RenderedOp src = JAI.create("fileload", source);

TIFFImageEncoder encoder = new TIFFImageEncoder(out,null);

TIFFEncodeParam Param=new TIFFEncodeParam();
Param.setCompression(TIFFEncodeParam.COMPRESSION_PACKBITS);

encoder.setParam(Param);

encoder.encode(src);



}
}
catch (FileNotFoundException e1) {
System.err.println(e1.getLocalizedMessage());
} catch (IOException e) {
System.err.println(e.getLocalizedMessage());
}
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 28 2008
Added on Jul 30 2008
2 comments
316 views