Get and set metadata for itext pdf document

I have an iText Document object and I want to write some metadata to it or read it.
How can i do this?

Imagine a document passed to a method of type:

 public void prePreccess(Object document) { Document pdfDocument = ((Document) document); //What to do here with pdfDocument? } 
+5
source share
1 answer

Do you want to fill out a PDF information dictionary? This is explained in the MetadataPdf example:

 // step 1 Document document = new Document(); // step 2 PdfWriter.getInstance(document, new FileOutputStream(filename)); // step 3 document.addTitle("Hello World example"); document.addAuthor("Bruno Lowagie"); document.addSubject("This example shows how to add metadata"); document.addKeywords("Metadata, iText, PDF"); document.addCreator("My program using iText"); document.open(); // step 4 document.add(new Paragraph("Hello World")); // step 5 document.close(); 

Do you want to set the XMP metadata? This is explained in the MetadataXmp example:

 // step 1 Document document = new Document(); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT1)); ByteArrayOutputStream os = new ByteArrayOutputStream(); XmpWriter xmp = new XmpWriter(os); XmpSchema dc = new com.itextpdf.text.xml.xmp.DublinCoreSchema(); XmpArray subject = new XmpArray(XmpArray.UNORDERED); subject.add("Hello World"); subject.add("XMP & Metadata"); subject.add("Metadata"); dc.setProperty(DublinCoreSchema.SUBJECT, subject); xmp.addRdfDescription(dc); PdfSchema pdf = new PdfSchema(); pdf.setProperty(PdfSchema.KEYWORDS, "Hello World, XMP, Metadata"); pdf.setProperty(PdfSchema.VERSION, "1.4"); xmp.addRdfDescription(pdf); xmp.close(); writer.setXmpMetadata(os.toByteArray()); // step 3 document.open(); // step 4 document.add(new Paragraph("Hello World")); // step 5 document.close(); 

Note that this method is deprecated: we recently replaced the XMP functionality, but we still need to write some examples using the new code.

Perhaps you want to configure the filling of the information dictionary and the simultaneous creation of XMP metadata:

 // step 1 Document document = new Document(); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); document.addTitle("Hello World example"); document.addSubject("This example shows how to add metadata & XMP"); document.addKeywords("Metadata, iText, step 3"); document.addCreator("My program using 'iText'"); document.addAuthor("Bruno Lowagie"); writer.createXmpMetadata(); // step 3 document.open(); // step 4 document.add(new Paragraph("Hello World")); // step 5 document.close(); 

If I were you, I would use this option because it is the most complete solution.

Cannot read metadata from a Document object.

You can read the XMP stream from an existing PDF file as follows:

 public void readXmpMetadata(String src, String dest) throws IOException { PdfReader reader = new PdfReader(src); FileOutputStream fos = new FileOutputStream(dest); byte[] b = reader.getMetadata(); fos.write(b, 0, b.length); fos.flush(); fos.close(); reader.close(); } 

You can read entries in the information dictionary as follows:

 PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); Map<String, String> info = reader.getInfo(); 

The info object will contain a series of keys and values ​​that are stored in the metadata inside the PDF.

+11
source

Source: https://habr.com/ru/post/1201956/


All Articles