Adding metadata

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

31st May 2016
admin-marketing

Switch code for this example

AddLanguage.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/24370273/set-initial-view-pdf-document-properties-using-itextsharp-with-c-sharp
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.kernel.pdf.*;
  15. import com.itextpdf.samples.GenericTest;
  16. import com.itextpdf.test.annotations.type.SampleTest;
  17.  
  18. import org.junit.experimental.categories.Category;
  19.  
  20. import java.io.File;
  21.  
  22. @Category(SampleTest.class)
  23. public class AddLanguage extends GenericTest {
  24.     public static final String DEST = "./target/test/resources/sandbox/stamper/add_language.pdf";
  25.     public static final String SRC = "./src/test/resources/pdfs/hello.pdf";
  26.  
  27.     public static void main(String[] args) throws Exception {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new AddLanguage().manipulatePdf(DEST);
  31.     }
  32.  
  33.     @Override
  34.     protected void manipulatePdf(String dest) throws Exception {
  35.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  36.         pdfDoc.getCatalog().put(PdfName.Lang, new PdfString("EN"));
  37.         pdfDoc.close();
  38.     }
  39. }
AddXmpToPage.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/28427100/how-do-i-add-xmp-metadata-to-each-page-of-an-existing-pdf-using-itextsharp
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfPage;
  16. import com.itextpdf.kernel.pdf.PdfReader;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.kernel.xmp.XMPMetaFactory;
  19. import com.itextpdf.samples.GenericTest;
  20. import com.itextpdf.test.annotations.type.SampleTest;
  21.  
  22. import org.junit.experimental.categories.Category;
  23.  
  24. import java.io.File;
  25.  
  26. @Category(SampleTest.class)
  27. public class AddXmpToPage extends GenericTest {
  28.     public static final String DEST = "./target/test/resources/sandbox/stamper/add_xmp_to_page.pdf";
  29.     public static final String SRC = "./src/test/resources/pdfs/hello.pdf";
  30.  
  31.     public static void main(String[] args) throws Exception {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new AddXmpToPage().manipulatePdf(DEST);
  35.     }
  36.  
  37.     @Override
  38.     protected void manipulatePdf(String dest) throws Exception {
  39.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  40.  
  41.         PdfPage page = pdfDoc.getFirstPage();
  42.         page.setXmpMetadata(XMPMetaFactory.create());
  43.  
  44.         pdfDoc.close();
  45.     }
  46. }
ChangeInfoDictionary.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/21607286/unicode-characters-in-document-info-dictionary-keys
  11.  *
  12.  * A user wants to update a Document Info Dictionary (DID)
  13.  * introducing a custom key with a Unicode character.
  14.  */
  15. package com.itextpdf.samples.sandbox.stamper;
  16.  
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfDocumentInfo;
  19. import com.itextpdf.kernel.pdf.PdfReader;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import org.junit.experimental.categories.Category;
  25.  
  26. import java.io.File;
  27. import java.util.HashMap;
  28. import java.util.Map;
  29.  
  30. @Category(SampleTest.class)
  31. public class ChangeInfoDictionary extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/stamper/change_info_dictionary.pdf";
  33.     public static final String SRC = "./src/test/resources/pdfs/hello.pdf";
  34.  
  35.     public static void main(String[] args) throws Exception {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new AddLongTable().manipulatePdf(DEST);
  39.     }
  40.  
  41.     @Override
  42.     protected void manipulatePdf(String dest) throws Exception {
  43.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  44.         PdfDocumentInfo info = pdfDoc.getDocumentInfo();
  45.         Map newInfo = new HashMap();
  46.         newInfo.put("Special Character: \u00e4", "\u00e4");
  47.         StringBuilder buf = new StringBuilder();
  48.         buf.append((char) 0xc3);
  49.         buf.append((char) 0xa4);
  50.         newInfo.put(buf.toString(), "\u00e4");
  51.         info.setMoreInfo(newInfo);
  52.         pdfDoc.close();
  53.     }
  54. }
ChangeVersion.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/23083220/how-to-set-pdf-version-using-itextsharp
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfReader;
  16. import com.itextpdf.kernel.pdf.PdfVersion;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.kernel.pdf.WriterProperties;
  19. import com.itextpdf.samples.GenericTest;
  20.  
  21. import java.io.File;
  22.  
  23. public class ChangeVersion extends GenericTest {
  24.     public static final String DEST = "./target/test/resources/sandbox/stamper/change_version.pdf";
  25.     public static final String SRC = "./src/test/resources/pdfs/OCR.pdf";
  26.  
  27.     public static void main(String[] args) throws Exception {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new ChangeVersion().manipulatePdf(DEST);
  31.     }
  32.  
  33.     @Override
  34.     protected void manipulatePdf(String dest) throws Exception {
  35.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST, new WriterProperties().setPdfVersion(PdfVersion.PDF_1_5)));
  36.         pdfDoc.close();
  37.     }
  38. }
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