Custom keys and comments

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

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


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