I’m trying to convert a regular flavour PDF to a PDF/A. I already found some articles here that give some insight on how to produce a PDF/A. But every time I save the the PDDocument the resulting file is either empty (0kb) or corrupt. PDFBox 2.0.3 produces a corrupt PDF and PDFBox 3.0 just generates a no size document.
I tried both PDFBox 3 and PDFBox 2 both give me wrong results. What I could see from tests is that as soon as I call the getDocumentCatalog() on the PDDocument and I try to save the PDDocument the problem is there. It actually doesn’t matter what the next actions are.
Here is some of the code
private void initializeDocument() throws Exception {
try (PDDocument loadedDoc = Loader.loadPDF(new RandomAccessReadBufferedFile(pdfInfo.getInputFile())))
{
document = loadedDoc;
document.setVersion(1.7f);
}
catch (IOException ioException) {
throw new Exception(ioException.getMessage());
}
}
private void addMetadata() throws Exception {
catalog = document.getDocumentCatalog();
XMPMetadata xmp = XMPMetadata.createXMPMetadata();
Calendar cal = Calendar.getInstance();
try {
// set core metadata
DublinCoreSchema dublinCoreSchema = new DublinCoreSchema(xmp);
dublinCoreSchema.setTitle(pdfInfo.getOutputFileName());
dublinCoreSchema.addDate(cal);
dublinCoreSchema.addCreator(pdfInfo.getCreator());
XMPBasicSchema xmpBasicSchema = xmp.createAndAddXMPBasicSchema();
xmpBasicSchema.setAboutAsSimple(pdfInfo.getCreator());
xmpBasicSchema.setCreateDate(cal);
xmpBasicSchema.setCreatorTool(pdfInfo.getCreator());
PDDocumentInformation pdDocumentInformation = new PDDocumentInformation();
pdDocumentInformation.setTitle(pdfInfo.getOutputFileName());
pdDocumentInformation.setProducer(pdfInfo.getCreator());
pdDocumentInformation.setCreator(pdfInfo.getCreator());
document.setDocumentInformation(pdDocumentInformation);
PDMarkInfo markinfo = new PDMarkInfo();
markinfo.setMarked(true);
this.catalog.setMarkInfo(markinfo);
PDFAIdentificationSchema pdfaIdentificationSchema = xmp.createAndAddPDFAIdentificationSchema();
pdfaIdentificationSchema.setConformance("A");
pdfaIdentificationSchema.setPart(3);
pdfaIdentificationSchema.setAboutAsSimple("");
XmpSerializer serializer = new XmpSerializer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
serializer.serialize(xmp, baos, true);
PDMetadata metadata = new PDMetadata(document);
metadata.importXMPMetadata(baos.toByteArray());
this.catalog.setMetadata(metadata);
}
catch (BadFieldValueException e) {
throw new IllegalArgumentException(e.getMessage());
}
catch (Exception e) {
throw new IllegalArgumentException(e.getMessage());
}
}
private void saveFile() {
try (OutputStream fileOutputStream = new FileOutputStream(pdfInfo.getOutputFile())) {
document.save(fileOutputStream);
document.close();
fileOutputStream.close();
} catch (Exception e) {
// TODO: handle exception
}
}
Leander is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.