Read PDF to add some metadata : Metadata « PDF « Java Tutorial






import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.util.HashMap;

import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.xml.xmp.XmpWriter;

public class HelloWorldAddMetadata {

  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
    document.open();
    document.add(new Paragraph("Hello World"));
    document.close();

    PdfReader reader = new PdfReader("HelloWorld.pdf");
    System.out.println("Tampered? " + reader.isTampered());
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
        "HelloWorldStampedMetadata.pdf"));
    System.out.println("Tampered? " + reader.isTampered());
    HashMap<String, String> info = reader.getInfo();
    info.put("Subject", "Hello World");
    info.put("Author", "your name");
    info.put("Keywords", "iText pdf");
    info.put("Title", "Hello World stamped");
    info.put("Creator", "your name");
    stamper.setMoreInfo(info);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XmpWriter xmp = new XmpWriter(baos, info);
    xmp.close();
    stamper.setXmpMetadata(baos.toByteArray());
    stamper.close();
  }

}








29.32.Metadata
29.32.1.Read PDF to add some metadata
29.32.2.Get Pdf metadata
29.32.3.Add XMP metadata automatically, based on the PDF metadata
29.32.4.Get meta data from pdf document
29.32.5.Set meta data to pdf document
29.32.6.use PdfSchema to update Meta data