pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tilman Hausherr <THaush...@t-online.de>
Subject Re: Overwriting Metadata
Date Fri, 20 Jul 2018 20:16:04 GMT
It works for me... here's my code:


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.xml.transform.TransformerException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.common.PDMetadata;
import org.apache.xmpbox.XMPMetadata;
import org.apache.xmpbox.schema.DublinCoreSchema;
import org.apache.xmpbox.xml.XmpSerializer;

public class ChangeMeta
{
     public static void main(String[] args) throws IOException, 
TransformerException
     {
         PDDocument doc = PDDocument.load(new File("testing.pdf"));
         XMPMetadata xmp = XMPMetadata.createXMPMetadata();
         DublinCoreSchema dc = xmp.createAndAddDublinCoreSchema();
         dc.setDescription("descr");
         XmpSerializer serializer = new XmpSerializer();
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         serializer.serialize(xmp, baos, true);
         PDMetadata metadata = new PDMetadata(doc);
         metadata.importXMPMetadata(baos.toByteArray());
         doc.getDocumentCatalog().setMetadata(metadata);
         doc.save(new File("testing-new.pdf"));
     }
}

And the proof that it worked:




Tilman

Am 20.07.2018 um 21:15 schrieb Matthew Clemente:
> Forgive me if this question has an obvious answer; perhaps I’m not taking
> the right approach to the problem.
>
> My goal is to save a version of a pdf, with modified metadata. In most
> cases, I’ll be removing metadata (setting the author, title, description to
> blank), though in some cases I’ll be adding information to those fields.
>
> I’ve tried both approaches from these StackOverflow answers:
> https://stackoverflow.com/questions/40295264/how-to-add-metadata-to-pdf-document-using-pdfbox
>
>
> That is, I’ve tried creating the metadata via XMPMetadata and using
> importXMPMetadata(). I’ve also tried using the Document Information object
> (inputDoc.getDocumentInformation().setCreator("Some meta”);).
>
> In both cases, if the field is empty in the original document I’ve loaded,
> the new value is set without issue. However, if the metadata field already
> contains a value, the new value is not applied.
>
> Is there a way for me to overwrite metadata, or am I approaching this all
> wrong?
>
> Here’s a pdf I was using while testing (it has a title and author set, but
> no subject): https://www.dropbox.com/s/olk2zhnh47ohtpk/testing.pdf?dl=0
>
> Thanks, in advance, for any insight.
>


Mime
View raw message