This example wraps the Linux program pdftotext from the Debian package xpdf-utils in a Java wrapper.
You can easily change or parameterise the executable yourself:
package com.chrisnewland.wrapper;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ConvertPDF
{
public static void main(String[] args) throws Exception
{
if (args.length != 2)
{
System.err.println("Usage: ConvertPDF <inputfile> <outputfile>");
}
String executable = "/usr/bin/pdftotext";
String input = args[0];
String output = args[1];
Process proc = Runtime.getRuntime().exec(executable + " " + input + " " + output);
// attach a reader to the Process standard err to get any error messages
final BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
new Thread(new Runnable()
{
@Override
public void run()
{
try
{
String line = reader.readLine();
while (line != null)
{
System.err.println(line);
line = reader.readLine();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}).start();
// block until the process returns
int result = proc.waitFor();
System.out.println("Result was " + result); // 0 == success
reader.close();
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ConvertPDF
{
public static void main(String[] args) throws Exception
{
if (args.length != 2)
{
System.err.println("Usage: ConvertPDF <inputfile> <outputfile>");
}
String executable = "/usr/bin/pdftotext";
String input = args[0];
String output = args[1];
Process proc = Runtime.getRuntime().exec(executable + " " + input + " " + output);
// attach a reader to the Process standard err to get any error messages
final BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
new Thread(new Runnable()
{
@Override
public void run()
{
try
{
String line = reader.readLine();
while (line != null)
{
System.err.println(line);
line = reader.readLine();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}).start();
// block until the process returns
int result = proc.waitFor();
System.out.println("Result was " + result); // 0 == success
reader.close();
}
}