Java Tutorial/PDF/Metadata
Содержание
Add XMP metadata automatically, based on the PDF metadata
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.addTitle("title");
document.addSubject("subject");
document.addKeywords("keywords");
document.addCreator("creator");
document.addAuthor("author");
writer.createXmpMetadata();
document.open();
document.add(new Paragraph("Hello World"));
document.close();
}
}
Get meta data from pdf document
import java.util.Iterator;
import java.util.Map;
import com.lowagie.text.pdf.PdfReader;
public class MainClass {
public static void main(String[] args) throws Exception {
PdfReader reader = new PdfReader("2.pdf");
Map info = reader.getInfo();
for (Iterator i = info.keySet().iterator(); i.hasNext();) {
String key = (String) i.next();
String value = (String) info.get(key);
System.out.println(key + ": " + value);
}
if (reader.getMetadata() == null) {
System.out.println("No XML Metadata.");
} else {
System.out.println("XML Metadata: " + new String(reader.getMetadata()));
}
}
}
Get Pdf metadata
import java.util.Iterator;
import java.util.Map;
import com.lowagie.text.pdf.PdfReader;
public class MainClass {
public static void main(String[] args) throws Exception {
PdfReader reader = new PdfReader("HelloWorldToRead.pdf");
if (reader.getMetadata() == null) {
System.out.println("No XML Metadata.");
} else {
System.out.println("XML Metadata: " + new String(reader.getMetadata()));
}
}
}
Read PDF to add some metadata
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();
}
}
Set meta data to pdf document
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();
}
}
use PdfSchema to update Meta data
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.xml.xmp.DublinCoreSchema;
import com.lowagie.text.xml.xmp.PdfSchema;
import com.lowagie.text.xml.xmp.XmpArray;
import com.lowagie.text.xml.xmp.XmpSchema;
import com.lowagie.text.xml.xmp.XmpWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
ByteArrayOutputStream os = new ByteArrayOutputStream();
XmpWriter xmp = new XmpWriter(os);
XmpSchema dc = new DublinCoreSchema();
XmpArray subject = new XmpArray(XmpArray.UNORDERED);
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());
document.open();
document.add(new Paragraph("Hello World"));
document.close();
}
}