I am using PDFBox v2.0.26 to modify existing PDFs that were produced with PrinceXML and then merged with the CLI of PDFBox.
Afterwards I want to update the PDF's meta data, among it author and title using a Java script. This is not quite working. When I open the PDF after post-processing, I cannot see the new author and title info, but it is there (somewhere), because when I read it back I get the new values.
Steps to reproduce:
- Open Adobe Acrobat Pro DC (I'm using version 2022.002)
- File > Create > Blank page
- Ctrl D > enter words for author and title (e.g. "author here" and "title here")
- Close dialogue and save document
- Run the code below
- Output on first run:
Existing author: author here
Existing title: title here
Output on second run:
Existing author: My new author
Existing title: My new title
When I open the PDF in Acrobat and do Ctrl-d I see:
Title: title here
Author: author here
What am I missing? It works well if I create a new PDF from scratch, just not with existing PDFs.
My code:
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
public class setTitle {
public static void main(String args[]) throws IOException {
String filepath = "C:\\Temp\\temp.pdf";
//Loading an existing document
File file = new File(filepath);
PDDocument document = PDDocument.load(file);
//Creating the PDDocumentInformation object
PDDocumentInformation pdd = document.getDocumentInformation();
System.out.println("Existing author: " pdd.getAuthor());
System.out.println("Existing title: " pdd.getTitle());
//Setting the author of the document
pdd.setAuthor("My new author");
// Setting the title of the document
pdd.setTitle("My new title");
//Saving the document
document.save("C:/Temp/temp.pdf");
//Closing the document
document.close();
}
}
CodePudding user response:
PDDocumentCatalog catalog = document.getDocumentCatalog();
PDMetadata meta = catalog.getMetadata();
XMPMetadata metadata;
if (meta != null)
{
DomXmpParser xmpParser = new DomXmpParser();
metadata = xmpParser.parse(meta.toByteArray());
}
else
{
meta = new PDMetadata(doc);
catalog.setMetadata(meta);
metadata = XMPMetadata.createXMPMetadata();
}
DublinCoreSchema dcSchema = metadata.getDublinCoreSchema();
if (dcSchema == null)
{
dcSchema = metadata.createAndAddDublinCoreSchema();
}
dcSchema.setTitle(pdd.getTitle());
dcSchema.addCreator(pdd.getAuthor()); // you may want to check whether the author is already there
XmpSerializer serializer = new XmpSerializer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
serializer.serialize(metadata, baos, false);
meta.importXMPMetadata(baos.toByteArray());