Convert PDF to Image
HI All,
Requirement-
Convert a section view to Image in w-1540px L-2200px
Solution:
1. converted section to pdf
3. Call HTMLToPDF
3.. Java pdf to image
Now the issue - i have used PDFBox to convert from PDF to Image
Code 1: The below code works fine but PDFBox only allows image to be converted with the input parameter DPI and not Pixel. I need to set a definite pixel size
byte[] byteArray=(byte[])tools.getParameterPage().getParameterValue("PDFDocument");
HI All,
Requirement-
Convert a section view to Image in w-1540px L-2200px
Solution:
1. converted section to pdf
3. Call HTMLToPDF
3.. Java pdf to image
Now the issue - i have used PDFBox to convert from PDF to Image
Code 1: The below code works fine but PDFBox only allows image to be converted with the input parameter DPI and not Pixel. I need to set a definite pixel size
byte[] byteArray=(byte[])tools.getParameterPage().getParameterValue("PDFDocument");
try { String pdfName=tools.getParamValue("PDFName"); pdfName= pdfName+".pdf"; String fileName=""; String pdfpath= java.io.File.separator+"temp"+java.io.File.separator+"StaticContent"+java.io.File.separator+"global"+java.io.File.separator+"ServiceExport"+java.io.File.separator; String ImagePath= pdfpath; org.apache.pdfbox.pdmodel.PDDocument document = org.apache.pdfbox.pdmodel.PDDocument.load(byteArray); org.apache.pdfbox.rendering.PDFRenderer pdfRenderer = new org.apache.pdfbox.rendering.PDFRenderer(document); for (int page = 0; page < document.getNumberOfPages(); ++page) { java.awt.image.BufferedImage bim = pdfRenderer.renderImageWithDPI(page,186); fileName = ImagePath + "image-" + page + ".png"; org.apache.pdfbox.tools.imageio.ImageIOUtil.writeImage(bim, fileName, 117); //resize } document.close(); } catch (Exception e) { oLog.error(e.getStackTrace()); }
Code 2: Here i am trying to save the image stream with pixels. But this code is not working and neither i am getting any error.
byte[] byteArray=(byte[])tools.getParameterPage().getParameterValue("PDFDocument");
try { int scaledWidth=1540; int scaledHeight=2200;
String pdfName=tools.getParamValue("PDFName"); //String result=tools.sendFile(byteArray,pdfName+".pdf",false,null,true); pdfName= pdfName+".pdf"; String fileName=""; String ImagePath= java.io.File.separator+"temp"+java.io.File.separator+"StaticContent"+java.io.File.separator+"global"+java.io.File.separator+"ServiceExport"+java.io.File.separator; org.apache.pdfbox.pdmodel.PDDocument document = org.apache.pdfbox.pdmodel.PDDocument.load(byteArray); org.apache.pdfbox.rendering.PDFRenderer pdfRenderer = new org.apache.pdfbox.rendering.PDFRenderer(document); for (int page = 0; page < document.getNumberOfPages(); ++page) { java.awt.image.BufferedImage bim = pdfRenderer.renderImageWithDPI(page,180); fileName = ImagePath + "image-" + page + ".png"; java.awt.image.BufferedImage outputImage = new java.awt.image.BufferedImage(scaledWidth,scaledHeight, bim.getType()); java.awt.Graphics2D g2d = outputImage.createGraphics(); g2d.drawImage(bim, 0, 0, scaledWidth, scaledHeight, null); g2d.dispose(); String formatName = ImagePath + "Resizedimage-" + page + ".png"; javax.imageio.ImageIO.write(outputImage, formatName, new java.io.File(formatName)); org.apache.pdfbox.tools.imageio.ImageIOUtil.writeImage(outputImage, formatName, null); } document.close(); } catch (Exception e) { oLog.error(e.getStackTrace()); }
Can anyone help ?
***Edited by Moderator Marije to add Capability tags***