The primary goal of this code is to convert a PDF file to images. I also create a directory under C:/Media
based on a combination of the PDF name and current date and insert the uploaded PDF to the same directory as the images for safe keeping.
I am primarily concerned that I am over-complicating the process by initializing too many variables and the process by which I move the PDF to the newly created directory. I am looking for guidance or advice on how to improve this code.
public class PdfService {
public static void main(String[] args) {
System.out.println("Test of Convert to PNG");
convertPDFtoPNG(args[0]);
}
public static void convertPDFtoPNG(String PDFFileName) {
try {
// load PDF document
PDFDocument document = new PDFDocument();
File baseFile = new File(PDFFileName);
document.load(baseFile);
//Get todays date as a String so that you can append it to the directory
String df = new SimpleDateFormat("MM-dd-yy").format(new Date());
//Get the filename without extension so that you can create a directory based on the name
Path path = Paths.get(PDFFileName);
String filenameWithExtension = path.getFileName().toString();
String filename = FilenameUtils.removeExtension(filenameWithExtension);
//add the current date to the filename to get the directory name
String dirname = filename + "-" + df;
//Create the directory
Path dir = Paths.get("C:\\Media\\" + dirname);
Files.createDirectory(dir);
//Move the pdf to the newly created directory
movePDFToDirectory(baseFile, dirname, filenameWithExtension);
// create renderer
SimpleRenderer renderer = new SimpleRenderer();
// set resolution (in DPI)
renderer.setResolution(150);
// render
List<Image> images = renderer.render(document);
// write images to files to disk as JPEG
try {
System.out.println("Begin converting PDF...");
for (int i = 0; i < images.size(); i++) {
ImageIO.write((RenderedImage) images.get(i), "jpeg", new File("C:\\Media\\" + dirname + "\\" + filename + "_" + (i + 1) + ".jpeg"));
}
System.out.println("PDF converted to image(s) successfully.");
} catch (IOException e) {
System.out.println("ERROR: " + e.getMessage());
}
} catch (Exception e) {
System.out.println("ERROR: " + e.getMessage());
}
}
public static void movePDFToDirectory(File originalFile, String directory, String filename) {
InputStream inStream = null;
OutputStream outStream = null;
try{
File newFile = new File("C:\\Media\\" + directory + "\\" + filename);
inStream = new FileInputStream(originalFile);
outStream = new FileOutputStream(newFile);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = inStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
System.out.println("Original PDF has been copied successfully!");
}catch(IOException e){
e.printStackTrace();
}
}
}
Notes:
- Eventually I plan on moving this bit of code to a web application that accepts the image as an upload. As it stands I'm just passing the file location as an argument in the
main
method.
- I plan on making the base directory (
C:/Media
) a dynamic variable in the event we move directories.