Home > other >  Changing author / title info of an existing PDF not working
Changing author / title info of an existing PDF not working

Time:09-05

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:

  1. Open Adobe Acrobat Pro DC (I'm using version 2022.002)
  2. File > Create > Blank page
  3. Ctrl D > enter words for author and title (e.g. "author here" and "title here")
  4. Close dialogue and save document
  5. Run the code below
  6. 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());
  • Related