Adding metadata

This is a code example of iText PDF, discover more.

6th November 2015
admin-marketing

Switch code for this example

AddXmpToPage.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/28427100/how-do-i-add-xmp-metadata-to-each-page-of-an-existing-pdf-using-itextsharp
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfDictionary;
  9. import com.itextpdf.text.pdf.PdfIndirectObject;
  10. import com.itextpdf.text.pdf.PdfName;
  11. import com.itextpdf.text.pdf.PdfReader;
  12. import com.itextpdf.text.pdf.PdfStamper;
  13. import com.itextpdf.text.pdf.PdfStream;
  14. import com.itextpdf.text.xml.xmp.XmpWriter;
  15.  
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20.  
  21. public class AddXmpToPage {
  22.  
  23.     public static final String SRC = "resources/pdfs/hello.pdf";
  24.     public static final String DEST = "results/stamper/hello_with_page_xmp.pdf";
  25.  
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new AddXmpToPage().manipulatePdf(SRC, DEST);
  30.     }
  31.  
  32.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  33.         PdfReader reader = new PdfReader(src);
  34.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  35.         PdfDictionary page = reader.getPageN(1);
  36.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  37.         XmpWriter xmp = new XmpWriter(baos, new PdfDictionary());
  38.         xmp.close();
  39.         PdfIndirectObject ref = stamper.getWriter().addToBody(new PdfStream(baos.toByteArray()));
  40.         page.put(PdfName.METADATA, ref.getIndirectReference());
  41.         stamper.close();
  42.         reader.close();
  43.     }
  44. }
ChangeInfoDictionary.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/21607286/unicode-characters-in-document-info-dictionary-keys
  4.  *
  5.  * A user wants to update a Document Info Dictionary (DID)
  6.  * introducing a custom key with a Unicode character.
  7.  */
  8. package sandbox.stamper;
  9.  
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.pdf.PdfReader;
  12. import com.itextpdf.text.pdf.PdfStamper;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.util.Map;
  18.  
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class ChangeInfoDictionary {
  23.  
  24.     public static final String SRC = "resources/pdfs/hello.pdf";
  25.     public static final String DEST = "results/stamper/unicode_in_did.pdf";
  26.  
  27.     public static void main(String[] args) throws IOException, DocumentException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new ChangeInfoDictionary().manipulatePdf(SRC, DEST);
  31.     }
  32.  
  33.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  34.         PdfReader reader = new PdfReader(src);
  35.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  36.         Map info = reader.getInfo();
  37.         info.put("Special Character: \u00e4", "\u00e4");
  38.         StringBuilder sb = new StringBuilder();
  39.         sb.append((char) 0xc3);
  40.         sb.append((char) 0xa4);
  41.         info.put(sb.toString(), "\u00e4");
  42.         stamper.setMoreInfo(info);
  43.         stamper.close();
  44.         reader.close();
  45.     }
  46. }
AddLanguage.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/24370273/set-initial-view-pdf-document-properties-using-itextsharp-with-c-sharp
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfName;
  9. import com.itextpdf.text.pdf.PdfReader;
  10. import com.itextpdf.text.pdf.PdfStamper;
  11. import com.itextpdf.text.pdf.PdfString;
  12.  
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import sandbox.WrapToTest;
  17.  
  18. @WrapToTest
  19. public class AddLanguage {
  20.  
  21.     public static final String SRC = "resources/pdfs/hello.pdf";
  22.     public static final String DEST = "results/stamper/hello_english.pdf";
  23.     public static void main(String[] args) throws IOException, DocumentException {
  24.         File file = new File(DEST);
  25.         file.getParentFile().mkdirs();
  26.         new AddLanguage().manipulatePdf(SRC, DEST);
  27.     }
  28.  
  29.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  30.         PdfReader reader = new PdfReader(src);
  31.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  32.         stamper.getWriter().getExtraCatalog().put(PdfName.LANG, new PdfString("EN"));
  33.         stamper.close();
  34.         reader.close();
  35.     }
  36.  
  37. }
ChangeVersion.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/23083220/how-to-set-pdf-version-using-itextsharp
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfReader;
  9. import com.itextpdf.text.pdf.PdfStamper;
  10.  
  11. import java.io.File;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import sandbox.WrapToTest;
  15.  
  16. @WrapToTest
  17. public class ChangeVersion {
  18.  
  19.     public static final String SRC = "resources/pdfs/OCR.pdf";
  20.     public static final String DEST = "results/stamper/other_version.pdf";
  21.     public static void main(String[] args) throws IOException, DocumentException {
  22.         File file = new File(DEST);
  23.         file.getParentFile().mkdirs();
  24.         new ChangeVersion().manipulatePdf(SRC, DEST);
  25.     }
  26.  
  27.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  28.         PdfReader reader = new PdfReader(src);
  29.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest), '4');
  30.         stamper.close();
  31.         reader.close();
  32.     }
  33.  
  34. }
ChangeMetadata.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to a question on SO
  3.  */
  4. package sandbox.stamper;
  5.  
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.pdf.PdfDate;
  8. import com.itextpdf.text.pdf.PdfName;
  9. import com.itextpdf.text.pdf.PdfReader;
  10. import com.itextpdf.text.pdf.PdfStamper;
  11. import com.itextpdf.text.xml.xmp.XmpWriter;
  12. import java.io.ByteArrayOutputStream;
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import java.util.Map;
  17.  
  18. /**
  19.  * @author iText
  20.  */
  21. public class ChangeMetadata {
  22.  
  23.     public static final String SRC = "resources/pdfs/state.pdf";
  24.     public static final String DEST = "results/stamper/state_metadata.pdf";
  25.  
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new ChangeMetadata().manipulatePdf(SRC, DEST);
  30.     }
  31.        
  32.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  33.         PdfReader reader = new PdfReader(src);
  34.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  35.         Map info = reader.getInfo();
  36.         info.put("Title", "New title");
  37.         info.put("CreationDate", new PdfDate().toString());
  38.         stamper.setMoreInfo(info);
  39.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  40.         XmpWriter xmp = new XmpWriter(baos, info);
  41.         xmp.close();
  42.         stamper.setXmpMetadata(baos.toByteArray());
  43.         stamper.close();
  44.         reader.close();
  45.     }
  46. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now