patternjavaMinor
Ghost4J PDF to image conversion
Viewed 0 times
ghost4jconversionimagepdf
Problem
The primary goal of this code is to convert a PDF file to images. I also create a directory under
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");
convertPDFtoImage(args[0]);
}
public static void convertPDFtoImage(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 images = renderer.render(document);
// write images to files to disk
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");
convertPDFtoImage(args[0]);
}
public static void convertPDFtoImage(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 images = renderer.render(document);
// write images to files to disk
Solution
I see that you're using Java 7, you could use the try-with-resource for all you're IO operation. It will close the stream you're using in your application.
You're not closing your streams if there is an
Argument name should not start with an uppercase letter :
I would revise some of you comments. Comments should be helpful, you don't need to repeat the code. Example :
Does
I know you're not in a production environment yet, but don't forget to change all the
It is worth to know that you can use
You're saying that your converting a PDF to an image, but it's not quite true. In fact, you are converting a PDF to a jpeg, which is quite different IMHO. You are hard coding the creation of the image as an jpeg, you will probably want to change in the future to support more formats. You will probably extract the writing code of the image into his own method or interface so that you have multiple implementation for each format (if needed, I am not good in image at all).
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();
}You're not closing your streams if there is an
IOException. This could lead to some potential problem in a future application. To get ride of the problem you could use the try-with-resource syntax. Your code could look like : File newFile = new File("C:\\Media\\");
try ( InputStream inStream = new FileInputStream(newFile);
OutputStream 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);
}
System.out.println("Original PDF has been copied successfully!");
} catch (IOException e) {
e.printStackTrace();
}Argument name should not start with an uppercase letter :
public static void convertPDFtoPNG(String PDFFileName). Do you need to capitalize pdf ?I would revise some of you comments. Comments should be helpful, you don't need to repeat the code. Example :
// load PDF document
PDFDocument document = new PDFDocument();
File baseFile = new File(PDFFileName);
document.load(baseFile);Does
//load PDF document is really needed ? You're already using document.load. One liner comment are most of time just repeating what you are doing in code. Other readers don't need to know what you are doing, but rather why are you doing it. I am always wondering myself when I am adding a comment : Does my code is clear enough by itself ? Do I really to add a comment or could I change the method name or variable to better express myself. Comments are hard to maintain when the code is changing.I know you're not in a production environment yet, but don't forget to change all the
e.printStackTrace for a logging of some sort.It is worth to know that you can use
/ for all of you path, instead of escaping \. Java will manage the one you need for the OS you're using.You're saying that your converting a PDF to an image, but it's not quite true. In fact, you are converting a PDF to a jpeg, which is quite different IMHO. You are hard coding the creation of the image as an jpeg, you will probably want to change in the future to support more formats. You will probably extract the writing code of the image into his own method or interface so that you have multiple implementation for each format (if needed, I am not good in image at all).
Code Snippets
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();
}File newFile = new File("C:\\Media\\");
try ( InputStream inStream = new FileInputStream(newFile);
OutputStream 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);
}
System.out.println("Original PDF has been copied successfully!");
} catch (IOException e) {
e.printStackTrace();
}// load PDF document
PDFDocument document = new PDFDocument();
File baseFile = new File(PDFFileName);
document.load(baseFile);Context
StackExchange Code Review Q#42431, answer score: 8
Revisions (0)
No revisions yet.