Examples
This example shows how to parse a control file and how to apply it on multiple documents.
All documents in a batch are supposed to be similar and compatible with the control
file. For performance reasons, the control file is parsed a single time.
The software is expected to run on servers with multiple CPU cores. This example is multi-threaded because it is the standard use case.
package com.ricoh.pdf2pdfua.api.example;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.ricoh.pdf2pdfua.api.IControlFile;
import com.ricoh.pdf2pdfua.api.PDFUAException;
import com.ricoh.pdf2pdfua.api.PdfUaLoader;
import com.ricoh.pdf2pdfua.api.PdfUaOpt;
import com.ricoh.pdf2pdfua.api.Pdfua;
import com.ricoh.pdf2pdfua.api.preprocessor.PreprocessingException;
public class Pdf2PdfUaApiExample {
static class ThreadSafeIterator {
Iterator<File> i;
ThreadSafeIterator(List<File> files) {
i = files.iterator();
}
synchronized File next() {
if (i.hasNext())
return i.next();
return null;
}
}
static void exit(String msg) {
System.err.println(msg);
System.exit(1);
}
static File changeExtension(File f) {
return new File(f.getAbsolutePath().replace(".pdf", ".ua.pdf"));
}
static Thread[] createThreadArray() {
return new Thread[Runtime.getRuntime().availableProcessors()];
}
static void convertBatch(String controlFilePath, List<File> pdfs)
throws PreprocessingException, InterruptedException {
final Pdfua pdfuaInstance = PdfUaLoader.load();
final IControlFile cf = pdfuaInstance.newControlFile(controlFilePath);
final PdfUaOpt opt = pdfuaInstance.newOpt();
final ThreadSafeIterator it = new ThreadSafeIterator(pdfs);
Thread[] threads = createThreadArray();
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread() {
public void run() {
File pdfIn;
while ((pdfIn = it.next()) != null) {
try {
FileInputStream fis = new FileInputStream(pdfIn);
File out = changeExtension(pdfIn);
pdfuaInstance.run(fis, out, cf, opt);
} catch (FileNotFoundException | PDFUAException e) {
e.printStackTrace();
}
}
}
};
}
for (Thread t : threads)
t.start();
for (Thread t : threads)
t.join();
System.out.println("Batch complete!");
}
public static void main(String[] args)
throws PreprocessingException, InterruptedException {
if (args.length < 1)
exit("No control file!");
ArrayList<File> files = new ArrayList<>();
for (int i = 1; i < args.length; i++)
files.add(new File(args[i]));
if (files.isEmpty())
exit("No PDF file to convert!");
convertBatch(args[0], files);
}
}
This example illustrates how PDF2PDFUA can work without using files. This is useful when PDF2PDFUA is integrated in other systems such as web applications.
package com.ricoh.pdf2pdfua.api.example;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import com.ricoh.pdf2pdfua.api.PdfUaLoader;
import com.ricoh.pdf2pdfua.api.Pdfua;
public class Pdf2PdfUaApiExampleBytesOutput {
public static void main(String[] args) throws Exception {
Pdfua ua = PdfUaLoader.load();
byte[] outputBytes = ua.run(
new FileInputStream("./untagged.pdf"),
ua.newControlFile("./control-file.xml"),
ua.newOpt()
);
var os = new FileOutputStream("./out.pdf");
os.write(outputBytes);
os.close();
}
}