HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

pdftk replacement

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pdftkreplacementstackoverflow

Problem

I am working on a replacement for a program called pdftk. The program needs to be able to take pdf's, and generate fdf files for them, fill a pdf from an fdf, output info about the forms, and output data about the pdf. I'm sorry for horrible formatting (I'm new to Java). Right now the program works, but is quite slow. Any advice to speed it up would be great.

```
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;

import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.fdf.FDFDocument;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;

public class ppdftk {
public static void main(String[] args) throws IOException{
String[] arg = args;
//String[] arg = {"leadlevel.pdf", "fill_form", "out.fdf", "output", "out.pdf", "flatten"};

//checking that request is valid
if(arg.length == 0){
System.err.println(info(false));
return;
}
if("-h".equals(arg[0]) || "--help".equals(arg[0]) || "-help".equals(arg[0])){
System.out.println(info(true));
return;
}
if(arg.length == 1){
System.err.println(info(false));
return;
}

String path = arg[0];
if("PROMPT".equals(path)){
System.out.println("Please enter a filename for an input PDF:");
Scanner console = new Scanner(System.in);
path = console.nextLine();
console.close();
}
PDDocument doc;
try{
if("-".equals(path))
doc = PDDocument.load(System.in);

Solution


  • For formatting it helps tremendously to use an editor/IDE that does


that for you, at least while you're still starting out. E.g. with
IDEA it's one key-stroke to reformat it according to the current
preset.

  • Next, you should try and use one of the existing libraries for


command-line argument parsing. I'm not going to try and go through
the whole main function since there's just too much going on in
there.

  • doc.close() is being called manually at the end - consider not doing


that, because the program ends right after anyway, or use the
try (PDDocument doc = ...) { } syntax to automatically close the
document (assuming it's AutoCloseable and if not perhaps create a
helper for you). That goes for all resources that can, in the widest
sense, be "closed" or "freed". Actually you're already doing that in
some cases, so be consistent.

  • In a real program I'd probably put the documentation into a separate


file and load it from there (as a resource from the generated JAR
file), since that makes editing and working with it much easier.

  • Catching NullPointerExceptions is probably wrong. I can't even tell


where they're coming from, so even if you are catching them, move the
scope of the exception handler right over the specific call, otherwise
it's really hard to figure out where this kind of exception came from
originally. Oh and even worse catching it and not doing anything with
the exception.

  • Now the part about if being slow ... well that's hard to tell as


you're mostly using some API. Consider using a profiler to figure out
what's actually the slow part.

Context

StackExchange Code Review Q#137853, answer score: 2

Revisions (0)

No revisions yet.