Document properties of a pdf can be accessed/created using the Apache PDFBox library. Lets see an example on all the document properties in Apache PDFBox.
The class used for accessing the document properties in Apache PDFBox is org.apache.pdfbox.pdmodel.PDDocumentInformation
Some of its useful methods to set the properties are
setAuthor(String author)
This will set the author of the document.
setCreationDate(Calendar date)
This will set the creation date of the document.
setCreator(String creator)
This will set the creator of the document.
setCustomMetadataValue(String fieldName, String fieldValue)
Set the custom metadata value.
setKeywords(String keywords)
This will set the keywords of the document.
setModificationDate(Calendar date)
This will set the modification date of the document.
setProducer(String producer)
This will set the producer of the document.
setSubject(String subject)
This will set the subject of the document.
setTitle(String title)
This will set the title of the document.
setTrapped(String value)
This will set the trapped of the document.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package com.kscodes.examples.pdfbox; import java.io.IOException; import java.util.Calendar; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocumentInformation; import org.apache.pdfbox.pdmodel.PDPage; public class DocPropertiesExample { public static void main(String args[]) { // Create a Document object. PDDocument pdDocument = new PDDocument(); // Create a Page object PDPage pdPage = new PDPage(); // Add the page to the document and save the document to a desired file. pdDocument.addPage(pdPage); try { PDDocumentInformation pdDocumentInformation = pdDocument.getDocumentInformation(); pdDocumentInformation.setAuthor("KSCodes"); pdDocumentInformation.setCreationDate(Calendar.getInstance()); pdDocumentInformation.setCreator("KSCodes"); pdDocumentInformation.setCustomMetadataValue("myCustomField", "myCustomValue"); pdDocumentInformation.setKeywords("Java, Apache PDFBox"); pdDocumentInformation.setModificationDate(Calendar.getInstance()); pdDocumentInformation.setProducer("testProducer"); pdDocumentInformation.setSubject("Document Information"); pdDocumentInformation.setTitle("Example of Document Information"); pdDocument.save("K:\\Kscodes\\pdf\\doc-info-sample.pdf"); pdDocument.close(); System.out.println("PDF saved to the location !!!"); } catch (IOException ioe) { System.out.println("Error while saving pdf" + ioe.getMessage()); } } } |
Output