GroupDocs.Metadata for Java can copy known metadata properties from one document to another. This is useful when a document is regenerated or converted and the new file has to keep the author, dates and other properties of the original.
try(MetadatasourceMetadata=newMetadata(Constants.InputPdf);MetadatadestinationMetadata=newMetadata(Constants.CopyPdf)){PdfRootPackagedestinationRoot=destinationMetadata.getRootPackageGeneric();// Copy all known properties from the source document to the destination package
sourceMetadata.copyTo(destinationRoot);System.out.println(destinationRoot.getDocumentProperties().getAuthor());System.out.println(destinationRoot.getDocumentProperties().getCreatedDate());System.out.println(destinationRoot.getDocumentProperties().getProducer());destinationMetadata.save(Constants.OutputPdf);}
Copying properties with specific tags
To copy only some of the properties, pass a list of tags to the copyTo method. Only the properties marked with at least one of the tags are copied. The method returns the number of affected properties.
try(MetadatasourceMetadata=newMetadata(Constants.InputPdf);MetadatadestinationMetadata=newMetadata(Constants.CopyPdf)){PdfRootPackagedestinationRoot=destinationMetadata.getRootPackageGeneric();// Copy only the properties marked with the specified tags
List<PropertyTag>tags=newArrayList<PropertyTag>();tags.add(Tags.getPerson().getCreator());tags.add(Tags.getTime().getCreated());intaffected=sourceMetadata.copyTo(destinationRoot,tags);System.out.println(String.format("Properties copied: %d",affected));destinationMetadata.save(Constants.OutputPdf);}
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples: