iText pdf library
Website search

Logging mechanism

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
/*
    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.counter.EventCounter;
import com.itextpdf.kernel.counter.EventCounterHandler;
import com.itextpdf.kernel.counter.IEventCounterFactory;
import com.itextpdf.kernel.counter.SimpleEventCounterFactory;
import com.itextpdf.kernel.counter.context.IContext;
import com.itextpdf.kernel.counter.context.UnknownContext;
import com.itextpdf.kernel.counter.event.IEvent;
import com.itextpdf.kernel.counter.event.IMetaInfo;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
 
public class CounterDemo {
    public static final String DEST_PDF = "./target/sandbox/logging/helloCounterDemo.pdf";
    public static final String DEST = "./target/sandbox/logging/CounterDemo.txt";
 
    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new CounterDemo().manipulatePdf();
    }
 
    protected void manipulatePdf() throws IOException {
 
        // Implement and register custom factory
        IEventCounterFactory myCounterFactory = new SimpleEventCounterFactory(new ToLogCounter(UnknownContext.PERMISSIVE));
        EventCounterHandler.getInstance().register(myCounterFactory);
 
        // Generate 2 events by creating 2 pdf documents
        for (int i = 0; i < 2; i++) {
            createPdf();
        }
 
        EventCounterHandler.getInstance().unregister(myCounterFactory);
    }
 
    private static void createPdf() throws FileNotFoundException {
        Document document = new Document(new PdfDocument(new PdfWriter(DEST_PDF)));
        document.add(new Paragraph("Hello World!"));
        document.close();
    }
 
    private static class ToLogCounter extends EventCounter {
        private ToLogCounter(IContext fallback) {
            super(fallback);
        }
 
        // Triggering registered factories to produce events and count them
        @Override
        protected void onEvent(IEvent event, IMetaInfo metaInfo) {
            try (FileWriter writer = new FileWriter(DEST, true)) {
                writer.write(String.format("%s\n", event.getEventType()));
            } catch (IOException e) {
                System.err.println("IOException occured.");
            }
        }
    }
}
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
61
62
63
64
/*
    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.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.counter.EventCounterHandler;
import com.itextpdf.kernel.counter.SystemOutEventCounterFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
 
public class CounterDemoSystemOut {
    public static final String DEST = "./target/sandbox/logging/CounterDemoSystemOut.pdf";
 
    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new CounterDemoSystemOut().manipulatePdf();
    }
 
    protected void manipulatePdf() throws IOException {
 
        // Implement default SystemOut factory and register it
        SystemOutEventCounterFactory counterFactory = new SystemOutEventCounterFactory();
        EventCounterHandler.getInstance().register(counterFactory);
 
        // Generate 3 core events by creating 3 pdf documents
        for (int i = 0; i < 3; i++) {
            createPdf();
        }
 
        String html = "<p>iText</p>";
 
        // Generate 2 events (core and html-convert) by converting html to pdf: the first during pdf document creation,
        // the second one during conversion
        convertToPdf(html);
 
        EventCounterHandler.getInstance().unregister(counterFactory);
    }
 
    private static void createPdf() throws FileNotFoundException {
        Document document = new Document(new PdfDocument(new PdfWriter(DEST)));
        document.add(new Paragraph("Hello World!"));
        document.close();
    }
 
    private static void convertToPdf(String html) throws IOException {
        PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
        HtmlConverter.convertToPdf(html, pdfDocument, new ConverterProperties());
    }
}

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