Page events for Chunks

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

24th October 2015
admin-marketing

Switch code for this example

DashedUnderline.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/29260730/how-do-you-underline-text-with-dashedline-in-itext-pdf
  4.  */
  5. package sandbox.events;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfPageEventHelper;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. /**
  22.  *
  23.  * @author iText
  24.  */
  25. @WrapToTest
  26. public class DashedUnderline {
  27.    
  28.     public static final String DEST = "results/events/dashed_underline.pdf";
  29.    
  30.     public class DashedLine extends PdfPageEventHelper {
  31.  
  32.         @Override
  33.         public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
  34.             PdfContentByte canvas = writer.getDirectContent();
  35.             canvas.saveState();
  36.             canvas.setLineDash(3, 3);
  37.             canvas.moveTo(rect.getLeft(), rect.getBottom() - 3);
  38.             canvas.lineTo(rect.getRight(), rect.getBottom() - 3);
  39.             canvas.stroke();
  40.             canvas.restoreState();
  41.         }
  42.     }
  43.    
  44.     public static void main(String[] args) throws IOException, DocumentException {
  45.         File file = new File(DEST);
  46.         file.getParentFile().mkdirs();
  47.         new DashedUnderline().createPdf(DEST);
  48.     }
  49.    
  50.     public void createPdf(String dest) throws IOException, DocumentException {
  51.         Document document = new Document();
  52.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  53.         writer.setPageEvent(new DashedLine());
  54.         document.open();
  55.         document.add(new Paragraph("This text is not underlined"));
  56.         Chunk chunk1 = new Chunk("This text is underlined with a solid line");
  57.         chunk1.setUnderline(1, -3);
  58.         document.add(new Paragraph(chunk1));
  59.         Chunk chunk2 = new Chunk("This text is underlined with a dashed line");
  60.         chunk2.setGenericTag("");
  61.         document.add(new Paragraph(chunk2));
  62.         document.close();
  63.     }
  64. }
Every25Words.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/28709603/draw-a-line-every-n-words-using-itextsharp
  4.  */
  5. package sandbox.events;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.PdfContentByte;
  12. import com.itextpdf.text.pdf.PdfPageEventHelper;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.BufferedReader;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.io.InputStreamReader;
  21. import sandbox.WrapToTest;
  22.  
  23. /**
  24.  *
  25.  * @author iText
  26.  */
  27. @WrapToTest
  28. public class Every25Words {
  29.    
  30.     public static final String DEST = "results/events/every25words.pdf";
  31.    
  32.     public class WordCounter extends PdfPageEventHelper {
  33.  
  34.         public int count = 0;
  35.        
  36.         @Override
  37.         public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
  38.             count++;
  39.             if (count % 25 == 0) {
  40.                 PdfContentByte canvas = writer.getDirectContent();
  41.                 canvas.saveState();
  42.                 canvas.setLineDash(5, 5);
  43.                 canvas.moveTo(document.left(), rect.getBottom());
  44.                 canvas.lineTo(rect.getRight(), rect.getBottom());
  45.                 canvas.lineTo(rect.getRight(), rect.getTop());
  46.                 canvas.lineTo(document.right(), rect.getTop());
  47.                 canvas.stroke();
  48.                 canvas.restoreState();
  49.             }
  50.         }
  51.     }
  52.    
  53.     public static void main(String[] args) throws IOException, DocumentException {
  54.         File file = new File(DEST);
  55.         file.getParentFile().mkdirs();
  56.         new Every25Words().createPdf(DEST);
  57.     }
  58.     public void createPdf(String dest) throws IOException, DocumentException {
  59.         Document document = new Document();
  60.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  61.         writer.setPageEvent(new WordCounter());
  62.         writer.setInitialLeading(16);
  63.         document.open();
  64.         String[] words = readFile().split("\\s+");
  65.         Chunk chunk = null;
  66.         for (String word : words) {
  67.             if (chunk != null) {
  68.                 document.add(new Chunk(" "));
  69.             }
  70.             chunk = new Chunk(word);
  71.             chunk.setGenericTag("");
  72.             document.add(chunk);
  73.         }
  74.         document.close();
  75.     }
  76.    
  77.     public String readFile() throws IOException {
  78.         StringBuilder sb = new StringBuilder();
  79.         BufferedReader in = new BufferedReader(
  80.             new InputStreamReader(new FileInputStream("resources/text/liber1_1_la.txt"), "UTF8"));
  81.         String str;
  82.         while ((str = in.readLine()) != null) {
  83.             sb.append(str);
  84.         }
  85.         return sb.toString();
  86.     }
  87. }
IndexWords.java
  1. package sandbox.events;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.util.List;
  7.  
  8. import com.itextpdf.text.Chunk;
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13. import com.itextpdf.text.pdf.events.IndexEvents;
  14. import com.itextpdf.text.pdf.events.IndexEvents.Entry;
  15.  
  16. public class IndexWords {
  17.  
  18.     public static final String DEST = "results/events/index_words.pdf";
  19.  
  20.     public static void main(String[] args) throws IOException,
  21.             DocumentException {
  22.         File file = new File(DEST);
  23.         file.getParentFile().mkdirs();
  24.         new IndexWords().createPdf(DEST);
  25.     }
  26.     public void createPdf(String dest) throws IOException, DocumentException {
  27.         Document document = new Document();
  28.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  29.         IndexEvents index = new IndexEvents();
  30.         writer.setPageEvent(index);
  31.         document.open();
  32.         Paragraph p = new Paragraph("Quick brown fox ");
  33.         p.add(index.create("jumps", "Jump"));
  34.         p.add(" over the lazy dog.");
  35.         document.add(p);
  36.         document.newPage();
  37.        
  38.         p = new Paragraph();
  39.         p.add(index.create("Quick brown fox", "Fox", "quick, brown"));
  40.         p.add(new Chunk(" jumps over "));
  41.         p.add(index.create("the lazy dog.", "Dog", "lazy"));
  42.         p.add(index.create(" ", "Jumping"));
  43.         document.add(p);
  44.         document.newPage();
  45.        
  46.         p = new Paragraph();
  47.         p.add(new Chunk("The fox is "));
  48.         p.add(index.create("brown", "Color", "brown"));
  49.         p.add(index.create(" ", "Brown", "color", "see Color; brown"));
  50.         p.add(Chunk.NEWLINE);
  51.         document.add(p);
  52.         document.newPage();
  53.        
  54.         p = new Paragraph();
  55.         p.add(new Chunk("The dog is "));
  56.         p.add(index.create("yellow", "Color", "yellow"));
  57.         p.add(index.create(" ", "Yellow", "color", "see Color; yellow"));
  58.         p.add(Chunk.NEWLINE);
  59.         document.add(p);
  60.         document.newPage();
  61.        
  62.         // we add the index
  63.         document.add(new Paragraph("Index:"));
  64.         List list = index.getSortedEntries();
  65.         for (Entry entry : list) {
  66.             Paragraph in = new Paragraph();
  67.             in.add(new Chunk(entry.getIn1()));
  68.             if (entry.getIn2().length() > 0) {
  69.                 in.add(new Chunk("; " + entry.getIn2()));
  70.             }
  71.             if (entry.getIn3().length() > 0) {
  72.                 in.add(new Chunk(" (" + entry.getIn3() + ")"));
  73.             }
  74.             List pages = entry.getPagenumbers();
  75.             List tags = entry.getTags();
  76.             in.add(": ");
  77.             for (int i = 0, x = pages.size(); i < x; i++) {
  78.                 Chunk pagenr = new Chunk(" p" + pages.get(i));
  79.                 pagenr.setLocalGoto((String) tags.get(i));
  80.                 in.add(pagenr);
  81.             }
  82.             document.add(in);
  83.         }
  84.        
  85.         document.close();
  86.     }
  87. }
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