Custom keys and comments

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

7th November 2015
admin-marketing

Switch code for this example

CustomPageDictKeyCreate.java
  1. /**
  2.  * This example is written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/32527211/itext-how-to-check-if-giant-string-is-present-on-the-pdf-page
  4.  */
  5. package sandbox.logging;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Paragraph;
  10. import com.itextpdf.text.pdf.PdfDictionary;
  11. import com.itextpdf.text.pdf.PdfName;
  12. import com.itextpdf.text.pdf.PdfNumber;
  13. import com.itextpdf.text.pdf.PdfReader;
  14. import com.itextpdf.text.pdf.PdfString;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. /**
  22.  *
  23.  * @author iText
  24.  */
  25. @WrapToTest
  26. public class CustomPageDictKeyCreate {
  27.    
  28.  
  29.     public static final String DEST = "results/logging/custom_page_dict_key.pdf";
  30.    
  31.     public static void main(String[] args) throws IOException,
  32.             DocumentException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         CustomPageDictKeyCreate app = new CustomPageDictKeyCreate();
  36.         app.createPdf(DEST);
  37.         app.check(DEST);
  38.     }
  39.    
  40.    
  41.     public void createPdf(String dest) throws IOException, DocumentException {
  42.         Document document = new Document();
  43.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  44.         document.open();
  45.         document.add(new Paragraph("Page 1"));
  46.         document.newPage();
  47.         document.add(new Paragraph("Page 2"));
  48.         document.newPage();
  49.         document.add(new Paragraph("Page 3"));
  50.         document.newPage();
  51.         document.add(new Paragraph("Page 4"));
  52.         writer.addPageDictEntry(new PdfName("ITXT_PageMarker"), new PdfString("Marker for page 4"));
  53.         document.newPage();
  54.         document.add(new Paragraph("Page 5"));
  55.         document.newPage();
  56.         document.add(new Paragraph("Page 6"));
  57.         writer.addPageDictEntry(new PdfName("ITXT_PageMarker"), new PdfName("PageMarker"));
  58.         document.newPage();
  59.         document.add(new Paragraph("Page 7"));
  60.         writer.addPageDictEntry(new PdfName("ITXT_PageMarker"), new PdfNumber(7));
  61.         document.newPage();
  62.         document.add(new Paragraph("Page 8"));
  63.         document.close();
  64.     }
  65.    
  66.     public void check(String filename) throws IOException {
  67.         PdfReader reader = new PdfReader(filename);
  68.         PdfDictionary pagedict;
  69.         for (int i = 1; i < reader.getNumberOfPages(); i++) {
  70.             pagedict = reader.getPageN(i);
  71.             System.out.println(pagedict.get(new PdfName("ITXT_PageMarker")));
  72.         }
  73.         reader.close();
  74.     }
  75. }
CustomPageDictKeyMerge.java
  1. /*
  2.  * This example is written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/34617914
  4.  */
  5. package sandbox.logging;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.pdf.PdfCopy;
  10. import com.itextpdf.text.pdf.PdfDictionary;
  11. import com.itextpdf.text.pdf.PdfImportedPage;
  12. import com.itextpdf.text.pdf.PdfName;
  13. import com.itextpdf.text.pdf.PdfReader;
  14. import com.itextpdf.text.pdf.PdfString;
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20.  
  21. /**
  22.  * @author Bruno Lowagie (iText Software)
  23.  */
  24. public class CustomPageDictKeyMerge {
  25.  
  26.     public static final String SRC1 = "resources/pdfs/hello.pdf";
  27.     public static final String SRC2 = "resources/pdfs/united_states.pdf";
  28.     public static final String SRC3 = "resources/pdfs/hero.pdf";
  29.     public static final String DEST = "results/logging/custom_page_dict_key_merge.pdf";
  30.    
  31.     public static void main(String[] args) throws IOException, DocumentException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new CustomPageDictKeyMerge().createPdf(DEST);
  35.     }
  36.    
  37.    
  38.     public void createPdf(String filename) throws IOException, DocumentException {
  39.         PdfName marker = new PdfName("ITXT_PageMarker");
  40.         List readers = new ArrayList();
  41.         readers.add(new PdfReader(SRC1));
  42.         readers.add(new PdfReader(SRC2));
  43.         readers.add(new PdfReader(SRC3));
  44.         Document document = new Document();
  45.         PdfCopy copy = new PdfCopy(document, new FileOutputStream(filename));
  46.         document.open();
  47.         int counter = 0;
  48.         int n;
  49.         PdfImportedPage importedPage;
  50.         PdfDictionary pageDict;
  51.         for (PdfReader reader : readers) {
  52.             counter++;
  53.             n = reader.getNumberOfPages();
  54.             for (int p = 1; p
PdfWithComments.java
  1. package sandbox.logging;
  2.  
  3. import com.itextpdf.text.Document;
  4. import com.itextpdf.text.DocumentException;
  5. import com.itextpdf.text.Paragraph;
  6. import com.itextpdf.text.pdf.OutputStreamCounter;
  7. import com.itextpdf.text.pdf.PdfWriter;
  8.  
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import sandbox.WrapToTest;
  14.  
  15. @WrapToTest
  16. public class PdfWithComments {
  17.     public static final String DEST = "results/logging/pdf_comments.pdf";
  18.  
  19.     public static void main(String[] args) throws IOException, DocumentException {
  20.         File file = new File(DEST);
  21.         file.getParentFile().mkdirs();
  22.         new PdfWithComments().createPdf(DEST);
  23.     }
  24.  
  25.     public void createPdf(String filename) throws DocumentException, IOException {
  26.         // step 1
  27.         Document document = new Document();
  28.         // step 2
  29.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  30.         // step 3
  31.         document.open();
  32.         OutputStreamCounter os = writer.getOs();
  33.         os.write("% After open\n".getBytes());
  34.         // step 4
  35.         document.add(new Paragraph("Hello World!"));
  36.         document.newPage();
  37.         // step 5
  38.         os.write("% Before close\n".getBytes());
  39.         document.close();
  40.     }
  41. }
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