Logging mechanism

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

7th November 2015
admin-marketing

Switch code for this example

CounterDemoSyso.java
  1. package sandbox.logging;
  2.  
  3. import com.itextpdf.text.Document;
  4. import com.itextpdf.text.DocumentException;
  5. import com.itextpdf.text.Element;
  6. import com.itextpdf.text.Paragraph;
  7. import com.itextpdf.text.Phrase;
  8. import com.itextpdf.text.log.CounterFactory;
  9. import com.itextpdf.text.log.SysoCounter;
  10. import com.itextpdf.text.pdf.ColumnText;
  11. import com.itextpdf.text.pdf.PdfContentByte;
  12. import com.itextpdf.text.pdf.PdfReader;
  13. import com.itextpdf.text.pdf.PdfStamper;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19.  
  20. public class CounterDemoSyso {
  21.  
  22.     public static final String HELLO = "results/logging/hello.pdf";
  23.     public static final String HELLO2 = "results/logging/hello2.pdf";
  24.     public static void main(String[] args) throws IOException, DocumentException {
  25.         CounterDemoSyso app = new CounterDemoSyso();
  26.         app.initCounter();
  27.         app.createPdf(HELLO);
  28.         app.manipulatePdf(HELLO, HELLO2);
  29.     }
  30.  
  31.     public void initCounter() throws IOException {
  32.         File file = new File(HELLO);
  33.         file.getParentFile().mkdirs();
  34.         CounterFactory.getInstance().setCounter(new SysoCounter());
  35.     }
  36.  
  37.     public void createPdf(String filename) throws IOException, DocumentException {
  38.         Document document = new Document();
  39.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  40.         document.open();
  41.         document.add(new Paragraph("Hello World!"));
  42.         document.close();
  43.     }
  44.    
  45.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  46.         PdfReader reader = new PdfReader(src);
  47.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  48.         PdfContentByte pagecontent = stamper.getOverContent(1);
  49.         ColumnText.showTextAligned(pagecontent, Element.ALIGN_RIGHT,
  50.                     new Phrase("Stamped text"), 559, 806, 0);
  51.         stamper.close();
  52.         reader.close();
  53.     }
  54. }
CounterDemo.java
  1. package sandbox.logging;
  2.  
  3. import com.itextpdf.text.Document;
  4. import com.itextpdf.text.DocumentException;
  5. import com.itextpdf.text.Element;
  6. import com.itextpdf.text.ExceptionConverter;
  7. import com.itextpdf.text.Paragraph;
  8. import com.itextpdf.text.Phrase;
  9. import com.itextpdf.text.log.Counter;
  10. import com.itextpdf.text.log.CounterFactory;
  11. import com.itextpdf.text.pdf.ColumnText;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfReader;
  14. import com.itextpdf.text.pdf.PdfStamper;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.FileWriter;
  20. import java.io.IOException;
  21. import java.util.Date;
  22.  
  23. public class CounterDemo {
  24.  
  25.     public static final String HELLO = "results/logging/hello.pdf";
  26.     public static final String HELLO2 = "results/logging/hello2.pdf";
  27.     protected MyCounter counter;
  28.    
  29.     public class MyCounter implements Counter {
  30.  
  31.         public static final String LOG = "results/logging/counter.txt";
  32.         protected FileWriter writer;
  33.         protected String yourClass;
  34.         protected String iTextClass;
  35.        
  36.         public MyCounter(Class klass) throws IOException {
  37.             this.yourClass = klass.getName();
  38.             writer = new FileWriter(LOG, true);
  39.         }
  40.        
  41.         private MyCounter(Class klass, String yourClass, FileWriter writer)
  42.             throws IOException {
  43.             this.yourClass = yourClass;
  44.             this.iTextClass = klass.getName();
  45.             this.writer = writer;
  46.         }
  47.        
  48.         public Counter getCounter(Class klass) {
  49.             try {
  50.                 return new MyCounter(klass, yourClass, writer);
  51.             } catch (IOException e) {
  52.                 throw new ExceptionConverter(e);
  53.             }
  54.         }
  55.  
  56.         public void read(long l) {
  57.             if (writer == null)
  58.                 throw new RuntimeException("No writer defined!");
  59.             try {
  60.                 writer.write(String.format(
  61.                     "[%s:%s] %s: %s read\n", yourClass, iTextClass, new Date().toString(), l));
  62.                 writer.flush();
  63.             } catch (IOException e) {
  64.                 throw new ExceptionConverter(e);
  65.             }
  66.         }
  67.  
  68.         public void written(long l) {
  69.             if (writer == null)
  70.                 throw new RuntimeException("No writer defined!");
  71.             try {
  72.                 writer.write(String.format(
  73.                     "[%s:%s] %s: %s written\n", yourClass, iTextClass, new Date().toString(), l));
  74.                 writer.flush();
  75.             } catch (IOException e) {
  76.                 throw new ExceptionConverter(e);
  77.             }
  78.         }
  79.        
  80.         public void close() throws IOException {
  81.             writer.close();
  82.         }
  83.     }
  84.    
  85.     public static void main(String[] args) throws IOException, DocumentException {
  86.         File file = new File(HELLO);
  87.         file.getParentFile().mkdirs();
  88.         CounterDemo app = new CounterDemo();
  89.         app.initCounter();
  90.         app.createPdf(HELLO);
  91.         app.manipulatePdf(HELLO, HELLO2);
  92.         app.closeCounter();
  93.     }
  94.  
  95.     public void initCounter() throws IOException {
  96.         counter = new MyCounter(getClass());
  97.         CounterFactory.getInstance().setCounter(counter);
  98.     }
  99.  
  100.     public void createPdf(String filename) throws IOException, DocumentException {
  101.         // step 1
  102.         Document document = new Document();
  103.         // step 2
  104.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  105.         // step 3
  106.         document.open();
  107.         // step 4
  108.         document.add(new Paragraph("Hello World!"));
  109.         // step 5
  110.         document.close();
  111.     }
  112.    
  113.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  114.         PdfReader reader = new PdfReader(src);
  115.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  116.         PdfContentByte pagecontent = stamper.getOverContent(1);
  117.         ColumnText.showTextAligned(pagecontent, Element.ALIGN_RIGHT,
  118.                     new Phrase("Stamped text"), 559, 806, 0);
  119.         stamper.close();
  120.         reader.close();
  121.     }
  122.  
  123.     public void closeCounter() throws IOException {
  124.         counter.close();
  125.     }
  126. }
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