简体   繁体   中英

How to access all the PDF metadata using pdfbox

I have a simple JAVA code that uses TIKA library to get the metadata of a PDF file and it lists the below metadata.

Tika code:

Metadata metadata = new Metadata();
tika.parse(file, metadata);
String[] metadataNames = metadata.names();
for (String name : metadataNames) {
    System.out.println(name + " : " + metadata.get(name));
}

Output:

date : 1996-11-19T09:00:46Z
pdf:PDFVersion : 1.1
access_permission:modify_annotations : true
access_permission:can_print_degraded : true
dcterms:created : 1996-10-22T07:44:27Z
Last-Modified : 1996-11-19T09:00:46Z
dcterms:modified : 1996-11-19T09:00:46Z
dc:format : application/pdf; version=1.1
title : Test
Last-Save-Date : 1996-11-19T09:00:46Z
access_permission:fill_in_form : true
meta:save-date : 1996-11-19T09:00:46Z
pdf:encrypted : false
dc:title : Test
modified : 1996-11-19T09:00:46Z
Content-Type : application/pdf
meta:creation-date : 1996-10-22T07:44:27Z
created : Tue Oct 22 00:44:27 PDT 1996
access_permission:extract_for_accessibility : true
access_permission:assemble_document : true
xmpTPg:NPages : 64
Creation-Date : 1996-10-22T07:44:27Z
access_permission:extract_content : true
access_permission:can_print : true
producer : Acrobat Distiller 2.1 for Power Macintosh
access_permission:can_modify : true

I am using the below code that uses PDF box to get the metadata but I don't want to specify the metadata key rather I would like to get all the available metadata keys and iterate over them.

What is the best way to generically access all the metadata key/value pair when using PDF box library?

public static void main(String args[]) {
        PDFTextStripper pdfStripper = null;
        PDDocument pdDoc = null;
        COSDocument cosDoc = null;
        File file = new File("test/test.pdf");
        try {

            PDFParser parser = new PDFParser(new FileInputStream(file));
            parser.parse();
            cosDoc = parser.getDocument();
            pdfStripper = new PDFTextStripper();
            pdDoc = new PDDocument(cosDoc);
            pdfStripper.setStartPage(1);
            pdfStripper.setEndPage(5);
            String parsedText = pdfStripper.getText(pdDoc);
            // System.out.println(parsedText);

            PDDocumentCatalog cat = pdDoc.getDocumentCatalog();
            PDMetadata metadata = cat.getMetadata();

            if (metadata != null) {
                System.out.println(metadata.getInputStreamAsString());
            }

            printMetadata(pdDoc);

    } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


public static void printMetadata(PDDocument document) throws IOException {
        PDDocumentInformation info = document.getDocumentInformation();
        PDDocumentCatalog cat = document.getDocumentCatalog();
        PDMetadata metadata = cat.getMetadata();

        System.out.println("Page Count=" + document.getNumberOfPages());
        System.out.println("Title=" + info.getTitle());
        System.out.println("Author=" + info.getAuthor());
        System.out.println("Subject=" + info.getSubject());
        System.out.println("Keywords=" + info.getKeywords());
        System.out.println("Creator=" + info.getCreator());
        System.out.println("Producer=" + info.getProducer());
        System.out.println("Creation Date=" + formatDate(info.getCreationDate()));
        System.out.println("Modification Date=" + formatDate(info.getModificationDate()));
        System.out.println("Trapped=" + info.getTrapped());   
        if (metadata != null) {
            System.out.println("Metadata=" + metadata.getStream());
        }
    }

Output:

 Page Count=64
    Title=test
    Author=null
    Subject=null
    Keywords=null
    Creator=null
    Producer=Acrobat Distiller 2.1 for Power Macintosh
    Creation Date=10/22/96 12:44 AM
    Modification Date=11/19/96 1:00 AM
Trapped=null

Sorry, there is no easy way to iterate through all metadata values. You could go meta (sorry) and use reflection on the PDDocumentInformation object and iterate through the getters, but then you'd also have to handle the different return types. At that point, you may as well just hardcode what you've done above.

And, that's just for the PDDocumentInformation object.

Navigating through the XMP, where the really fun metadata can live, is even more interesting because it can contain different schemas (DublinCore, XMPMM and many more, see eg Jempbox ), and even custom metadata.

Over on Tika, we're trying to make more and more of the XMP metadata available (just added XMPMM, and will soon add Photoshop)...if you have any requests, please let us know .

Finally, if you do start working with XMP and PDFBox, I'd recommend sticking with Jempbox for a while (see this ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM