Logging mechanism
These examples were written in the context of the question Click How can I log the number of documents / bytes I've processed?
counterdemo
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.logging;
import com.itextpdf.commons.actions.AbstractContextBasedEventHandler;
import com.itextpdf.commons.actions.AbstractContextBasedITextEvent;
import com.itextpdf.commons.actions.EventManager;
import com.itextpdf.commons.actions.confirmations.ConfirmEvent;
import com.itextpdf.commons.actions.contexts.IContext;
import com.itextpdf.commons.actions.contexts.UnknownContext;
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
ToLogCounter logCounter = new ToLogCounter(UnknownContext.PERMISSIVE);
EventManager.getInstance().register(logCounter);
// Generate 2 events by creating 2 pdf documents
for (int i = 0; i < 2; i++) {
createPdf();
}
EventManager.getInstance().unregister(logCounter);
}
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 AbstractContextBasedEventHandler {
private ToLogCounter(IContext fallback) {
super(fallback);
}
// Triggering registered factories to produce events and count them
@Override
protected void onAcceptedEvent(AbstractContextBasedITextEvent event) {
try (FileWriter writer = new FileWriter(DEST, true)) {
if (event instanceof ConfirmEvent) {
ConfirmEvent confirmEvent = (ConfirmEvent) event;
writer.write(String.format("%s\n", confirmEvent.getEventType()));
}
} catch (IOException e) {
System.err.println("IOException occurred.");
}
}
}
}
C#
C#
using System;
using System.IO;
using iText.Commons.Actions;
using iText.Commons.Actions.Confirmations;
using iText.Commons.Actions.Contexts;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
namespace iText.Samples.Sandbox.Logging
{
public class CounterDemo
{
public const String DEST_PDF = "results/sandbox/logging/helloCounterDemo.pdf";
public const String DEST = "results/sandbox/logging/CounterDemo.txt";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new CounterDemo().ManipulatePdf();
}
protected virtual void ManipulatePdf()
{
// Implement and register custom factory
ToLogCounter logCounter = new ToLogCounter(UnknownContext.PERMISSIVE);
EventManager.GetInstance().Register(logCounter);
// Generate 2 events by creating 2 pdf documents
for (int i = 0; i < 2; i++)
{
CreatePdf();
}
EventManager.GetInstance().Unregister(logCounter);
}
private static void CreatePdf()
{
Document document = new Document(new PdfDocument(new PdfWriter(DEST_PDF)));
document.Add(new Paragraph("Hello World!"));
document.Close();
}
private class ToLogCounter : AbstractContextBasedEventHandler {
public ToLogCounter(IContext fallback)
: base(fallback)
{
}
// Triggering registered factories to produce events and count them
protected override void OnAcceptedEvent(AbstractContextBasedITextEvent iTextEvent)
{
try {
using (StreamWriter writer = new StreamWriter(DEST, true))
{
if (iTextEvent is ConfirmEvent) {
ConfirmEvent confirmEvent = (ConfirmEvent) iTextEvent;
writer.Write(String.Format("{0}\n", confirmEvent.GetEventType()));
}
}
}
catch (IOException)
{
Console.Error.WriteLine("IOException occured.");
}
}
}
}
}