Set meta data to pdf document : 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 MainClass {

  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    document.add(new Paragraph("Hello World"));
    document.close();
    PdfReader reader = new PdfReader("2.pdf");
    System.out.println("Tampered? " + reader.isTampered());
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("2.pdf"));
    HashMap info = reader.getInfo();
    info.put("Subject", "subject");
    info.put("Author", "author");
    info.put("Keywords", "keywords");
    info.put("Title", "title");
    info.put("Creator", "creator");
    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