Logging mechanism

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

31st May 2016
iText PDF

These examples were written in the context of the question How can I log the number of documents / bytes I've processed?

CounterDemo.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.logging;
 
import com.itextpdf.kernel.log.CounterManager;
import com.itextpdf.kernel.log.ICounter;
import com.itextpdf.kernel.log.ICounterFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
 
import org.junit.Assert;
import org.junit.experimental.categories.Category;
 
@Category(SampleTest.class)
public class CounterDemo extends GenericTest {
    public static final String SRC = "./target/test/resources/sandbox/logging/hello.pdf";
    public static final String DEST = "./target/test/resources/sandbox/logging/stamp.pdf";
    public static final String LOG_RESULT = "./target/test/resources/sandbox/logging/log.txt";
    public static final String CMP_LOG = "./src/test/resources/sandbox/logging/cmp_log.txt";
    public static final String CMP_LOG_LICENSED = "./src/test/resources/sandbox/logging/cmp_log_licensed.txt";
    public static final String CMP_LOG_RELEASE = "./src/test/resources/sandbox/logging/cmp_log_release.txt";
 
    MyCounterFactory myCounterFactory;
 
    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new CounterDemo().manipulatePdf(DEST);
    }
 
    private void createPdf() throws FileNotFoundException {
        Document document = new Document(new PdfDocument(new PdfWriter(SRC)));
        document.add(new Paragraph("Hello World!"));
        document.close();
    }
 
    @Override
    public void manipulatePdf(String dest) throws IOException {
        myCounterFactory = new MyCounterFactory(getClass());
        CounterManager.getInstance().register(myCounterFactory);
 
        createPdf();
        PdfReader reader = new PdfReader(SRC);
        PdfDocument pdfDocument = new PdfDocument(reader, new PdfWriter(dest));
        Document document = new Document(pdfDocument).showTextAligned(new Paragraph("Stamped text"), 559, 806, TextAlignment.RIGHT);
        document.close();
 
        myCounterFactory.close();
        CounterManager.getInstance().unregister(myCounterFactory);
 
        compareLogs();
    }
 
    public static class MyCounterFactory implements ICounterFactory {
 
        protected FileWriter writer;
        protected String yourClass;
 
        public MyCounterFactory(Class<?> cls) {
            try {
                this.writer = new FileWriter(LOG_RESULT, false);
                yourClass = cls.getName();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
 
        @Override
        public ICounter getCounter(Class<?> cls) {
            return new MyCounter(cls, yourClass, writer);
        }
 
        public void close() throws IOException {
            writer.close();
        }
 
        private static class MyCounter implements ICounter {
 
            protected FileWriter writer;
            protected String yourClass;
            protected String iTextClass;
 
            public MyCounter(Class<?> klass, String yourClass, FileWriter writer) {
                this.yourClass = yourClass;
                this.iTextClass = klass.getName();
                this.writer = writer;
            }
 
            @Override
            public void onDocumentRead(long size) {
                if (writer == null)
                    throw new RuntimeException("No writer defined!");
                try {
                    writer.write(String.format(
                            "[%s:%s] %s: %s read\n", yourClass, iTextClass, new Date().toString(), size));
                    writer.flush();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
 
            @Override
            public void onDocumentWritten(long size) {
                if (writer == null)
                    throw new RuntimeException("No writer defined!");
                try {
                    writer.write(String.format(
                            "[%s:%s] %s: %s written\n", yourClass, iTextClass, new Date().toString(), size));
                    writer.flush();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
 
    private void compareLogs() throws IOException {
        boolean expectedResult = fileToString(LOG_RESULT).equals(fileToString(CMP_LOG))
                || fileToString(LOG_RESULT).equals(fileToString(CMP_LOG_LICENSED))
                || fileToString(LOG_RESULT).equals(fileToString(CMP_LOG_RELEASE));
        Assert.assertTrue(expectedResult);
    }
 
    private String fileToString(String filePath) throws IOException {
        FileInputStream fin = new FileInputStream(filePath);
        BufferedReader myInput = new BufferedReader(new InputStreamReader(fin));
        StringBuilder sb = new StringBuilder();
        String thisLine;
        while ((thisLine = myInput.readLine()) != null) {
            int index = thisLine.lastIndexOf(": ");
            if (index > -1) {
                sb.append(thisLine, index + 2, thisLine.length()).append("\n");
            }
        }
        return sb.toString();
    }
 
}
CounterDemoSystemOut.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
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.logging;
 
import com.itextpdf.kernel.log.CounterManager;
import com.itextpdf.kernel.log.SystemOutCounterFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
 
import org.junit.experimental.categories.Category;
 
@Category(SampleTest.class)
public class CounterDemoSystemOut extends GenericTest {
 
    public static final String SRC = "./target/test/resources/sandbox/logging/hello2.pdf";
    public static final String DEST = "./target/test/resources/sandbox/logging/stamp2.pdf";
 
    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new CounterDemoSystemOut().manipulatePdf(DEST);
    }
 
    private void createPdf() throws FileNotFoundException {
        Document document = new Document(new PdfDocument(new PdfWriter(SRC)));
        document.add(new Paragraph("Hello World!"));
        document.close();
    }
 
    @Override
    public void manipulatePdf(String dest) throws IOException {
        SystemOutCounterFactory sysOutFactory = new SystemOutCounterFactory();
        CounterManager.getInstance().register(sysOutFactory);
 
        createPdf();
        PdfReader reader = new PdfReader(SRC);
        PdfDocument pdfDocument = new PdfDocument(reader, new PdfWriter(dest));
        Document document = new Document(pdfDocument).showTextAligned(new Paragraph("Stamped text"), 559, 806, TextAlignment.RIGHT);
        document.close();
 
        CounterManager.getInstance().unregister(sysOutFactory);
    }
 
}

Click the following link to see the legacy example for iText 5. Except for security fixes, iText 5 is no longer being developed.



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