Page events for Chunks

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

31st May 2016
admin-marketing

Switch code for this example

DashedUnderline.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/29260730/how-do-you-underline-text-with-dashedline-in-itext-pdf
  11.  */
  12. package com.itextpdf.samples.sandbox.events;
  13.  
  14. import com.itextpdf.kernel.geom.Rectangle;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  18. import com.itextpdf.layout.Document;
  19. import com.itextpdf.layout.element.Paragraph;
  20. import com.itextpdf.layout.element.Text;
  21. import com.itextpdf.layout.renderer.DrawContext;
  22. import com.itextpdf.layout.renderer.TextRenderer;
  23. import com.itextpdf.samples.GenericTest;
  24. import com.itextpdf.test.annotations.type.SampleTest;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28. import java.io.File;
  29.  
  30. @Category(SampleTest.class)
  31. public class DashedUnderline extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/events/dashed_underline.pdf";
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new DashedUnderline().manipulatePdf(DEST);
  38.     }
  39.  
  40.     @Override
  41.     protected void manipulatePdf(String dest) throws Exception {
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  43.         Document doc = new Document(pdfDoc);
  44.         doc.add(new Paragraph("This text is not underlined"));
  45.         Text text1 = new Text("This text is underlined with a solid line");
  46.         text1.setUnderline(1, -3);
  47.         doc.add(new Paragraph(text1));
  48.         Text text2 = new Text("This text is underlined with a dashed line");
  49.         text2.setNextRenderer(new DashedLineTextRenderer(text2));
  50.         doc.add(new Paragraph(text2));
  51.         doc.close();
  52.     }
  53.  
  54.  
  55.     protected class DashedLineTextRenderer extends TextRenderer {
  56.         public DashedLineTextRenderer(Text textElement) {
  57.             super(textElement);
  58.         }
  59.  
  60.         @Override
  61.         public void draw(DrawContext drawContext) {
  62.             super.draw(drawContext);
  63.             Rectangle rect = this.getOccupiedAreaBBox();
  64.             PdfCanvas canvas = drawContext.getCanvas();
  65.             canvas
  66.                     .saveState()
  67.                     .setLineDash(3, 3)
  68.                     .moveTo(rect.getLeft(), rect.getBottom() - 3)
  69.                     .lineTo(rect.getRight(), rect.getBottom() - 3)
  70.                     .stroke()
  71.                     .restoreState();
  72.         }
  73.     }
  74. }
Every25Words.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/28709603/draw-a-line-every-n-words-using-itextsharp
  11.  */
  12. package com.itextpdf.samples.sandbox.events;
  13.  
  14. import com.itextpdf.kernel.geom.Rectangle;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  18. import com.itextpdf.layout.Document;
  19. import com.itextpdf.layout.element.Paragraph;
  20. import com.itextpdf.layout.element.Text;
  21. import com.itextpdf.layout.renderer.DrawContext;
  22. import com.itextpdf.layout.renderer.TextRenderer;
  23. import com.itextpdf.samples.GenericTest;
  24. import com.itextpdf.test.annotations.type.SampleTest;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28. import java.io.*;
  29.  
  30. @Category(SampleTest.class)
  31. public class Every25Words extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/events/every25words.pdf";
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new Every25Words().manipulatePdf(DEST);
  38.     }
  39.  
  40.     public String readFile() throws IOException {
  41.         StringBuilder sb = new StringBuilder();
  42.         BufferedReader in = new BufferedReader(new InputStreamReader(
  43.                 new FileInputStream("./src/test/resources/txt/liber1_1_la.txt"), "UTF8"));
  44.         String str;
  45.         while ((str = in.readLine()) != null) {
  46.             sb.append(str);
  47.         }
  48.         return sb.toString();
  49.     }
  50.  
  51.     @Override
  52.     protected void manipulatePdf(String dest) throws Exception {
  53.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  54.         Document doc = new Document(pdfDoc);
  55.         String[] words = readFile().split("\\s+");
  56.         Paragraph paragraph = new Paragraph();
  57.         Text text = null;
  58.         int i = 0;
  59.         for (String word : words) {
  60.             if (text != null) {
  61.                 paragraph.add(" ");
  62.             }
  63.             text = new Text(word);
  64.             text.setNextRenderer(new Word25TextRenderer(text, ++i));
  65.             paragraph.add(text);
  66.         }
  67.         doc.add(paragraph);
  68.         doc.close();
  69.     }
  70.  
  71.  
  72.     private class Word25TextRenderer extends TextRenderer {
  73.         private int count = 0;
  74.  
  75.         public Word25TextRenderer(Text textElement, int count) {
  76.             super(textElement);
  77.             this.count = count;
  78.         }
  79.  
  80.         @Override
  81.         public void draw(DrawContext drawContext) {
  82.             super.draw(drawContext);
  83.             if (0 == count % 25) {
  84.                 Rectangle rect = getOccupiedAreaBBox();
  85.                 int pageNumber = getOccupiedArea().getPageNumber();
  86.                 PdfCanvas canvas = drawContext.getCanvas();
  87.                 canvas.saveState()
  88.                         .setLineDash(5, 5)
  89.                         .moveTo(drawContext.getDocument().getPage(pageNumber).getPageSize().getLeft(), rect.getBottom())
  90.                         .lineTo(rect.getRight(), rect.getBottom())
  91.                         .lineTo(rect.getRight(), rect.getTop())
  92.                         .lineTo(drawContext.getDocument().getDefaultPageSize().getRight(), rect.getTop())
  93.                         .stroke()
  94.                         .restoreState();
  95.             }
  96.         }
  97.     }
  98. }
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