RSS

Convert pdf to images

This is a method thar convert de first page of a pdf file into an image using the open source library of IcePdf from IceSoft:


import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.imageio.ImageIO;
import javax.portlet.PortletPreferences;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.dispatcher.DefaultActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.portlet.interceptor.PortletPreferencesAware;
import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.PDimension;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;

private void generaImagenes(String urlDoc, String fileName) {
boolean control = true;
File file2 = new File(request.getSession().getServletContext()
.getRealPath("/thumbnails"));

String absoluteFile3 = file2.getAbsoluteFile().toString();
File folder = new File(absoluteFile3);
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
if (listOfFiles[i].getName().equals(fileName + ".png")) {
control = false;

}
}
}
if (control == true) {
Document document = new Document();

try {
URL files = new URL(urlDoc);

document.setUrl(files);

} catch (PDFException ex) {
System.out.println("Error parsing PDF document " + ex);

} catch (PDFSecurityException ex) {
System.out.println("Error encryption not supported " + ex);

} catch (FileNotFoundException ex) {
System.out.println("Error file not found " + ex);

} catch (IOException ex) {
System.out.println("Error handling PDF document " + ex);

}

float userRotation = 0;
float userZoom = 1;
PDimension pdfDimension = document.getPageDimension(1,
userRotation, userZoom);

float height = pdfDimension.getHeight();

float scale = (37000 / height) / 100;

float rotation = 0f;

// Paint each pages content to an image and write the image
// to file

BufferedImage image = (BufferedImage) document.getPageImage(0,
GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX,
rotation, scale);
RenderedImage rendImage = image;
// capture the page image to file
try {

File file = new File(request.getSession().getServletContext()
.getRealPath("/thumbnails")
+ File.separatorChar + fileName + ".png");

String absoluteFile2 = file.getAbsoluteFile().toString();
absoluteFile2 = absoluteFile2.replaceAll("bin", "");

file = new File(absoluteFile2);
file = file.getAbsoluteFile();
System.out.println("Generando: " + fileName + ".png");

ImageIO.write(rendImage, "png", file);

} catch (IOException e) {
e.printStackTrace();
}
image.flush();

// clean up resources
document.dispose();
}

}

IceSoft
0 comments

Posted in , ,