Page events for Paragraphs

31st May 2016
admin-marketing

Switch code for this example

BorderForParagraph.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.  * This question was answered by Bruno Lowagie in answer to the following question on StackOverflow:
  10.  * http://stackoverflow.com/questions/30053684/how-to-add-border-to-paragraph-in-itext-pdf-library-in-java
  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.layout.Document;
  18. import com.itextpdf.layout.element.Paragraph;
  19. import com.itextpdf.layout.renderer.DrawContext;
  20. import com.itextpdf.layout.renderer.ParagraphRenderer;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import org.junit.experimental.categories.Category;
  25.  
  26. import java.io.File;
  27.  
  28. @Category(SampleTest.class)
  29. public class BorderForParagraph extends GenericTest {
  30.     public static final String DEST = "./target/test/resources/sandbox/events/border_for_paragraph.pdf";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new BorderForParagraph().manipulatePdf(DEST);
  36.     }
  37.  
  38.     @Override
  39.     protected void manipulatePdf(String dest) throws Exception {
  40.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  41.         Document doc = new Document(pdfDoc);
  42.         doc.add(new Paragraph("Hello,"));
  43.         doc.add(new Paragraph("In this doc, we'll add several paragraphs that will trigger page events. " +
  44.                 "As long as the event isn't activated, nothing special happens, " +
  45.                 "but let's make the event active and see what happens:"));
  46.         Paragraph renderedParagraph = new Paragraph("This paragraph now has a border. Isn't that fantastic? " +
  47.                 "By changing the event, we can even provide a background color, " +
  48.                 "change the line width of the border and many other things. Now let's deactivate the event.");
  49.         renderedParagraph.setNextRenderer(new BorderParagraphRenderer(renderedParagraph));
  50.         doc.add(renderedParagraph);
  51.         doc.add(new Paragraph("This paragraph no longer has a border."));
  52.         doc.close();
  53.     }
  54.  
  55.  
  56.     private class BorderParagraphRenderer extends ParagraphRenderer {
  57.         public BorderParagraphRenderer(Paragraph modelElement) {
  58.             super(modelElement);
  59.         }
  60.  
  61.         @Override
  62.         public void drawBorder(DrawContext drawContext) {
  63.             super.drawBorder(drawContext);
  64.             Rectangle rect = getOccupiedAreaBBox();
  65.             drawContext.getCanvas().rectangle(rect.getLeft(), rect.getBottom(),
  66.                     rect.getWidth(), rect.getHeight());
  67.             drawContext.getCanvas().stroke();
  68.         }
  69.     }
  70. }
BorderForParagraph2.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.  * This question was answered by Bruno Lowagie in answer to the following question on StackOverflow:
  10.  * http://stackoverflow.com/questions/30053684/how-to-add-border-to-paragraph-in-itext-pdf-library-in-java
  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.layout.Document;
  18. import com.itextpdf.layout.element.Paragraph;
  19. import com.itextpdf.layout.renderer.DrawContext;
  20. import com.itextpdf.layout.renderer.IRenderer;
  21. import com.itextpdf.layout.renderer.ParagraphRenderer;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import java.io.File;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. @Category(SampleTest.class)
  30. public class BorderForParagraph2 extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/events/border_for_paragraph2.pdf";
  32.  
  33.     public static void main(String[] args) throws Exception {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new BorderForParagraph2().manipulatePdf(DEST);
  37.     }
  38.  
  39.     @Override
  40.     protected void manipulatePdf(String dest) throws Exception {
  41.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  42.         Document doc = new Document(pdfDoc);
  43.         doc.add(new Paragraph("Hello,"));
  44.         doc.add(new Paragraph("In this doc, we'll add several paragraphs that will trigger page events. " +
  45.                 "As long as the event isn't activated, nothing special happens, " +
  46.                 "but let's make the event active and see what happens:"));
  47.         Paragraph renderedParagraph = new Paragraph("This paragraph now has a border. " +
  48.                 "Isn't that fantastic? By changing the event, we can even provide a background color, " +
  49.                 "change the line width of the border and many other things. Now let's deactivate the event.");
  50.         renderedParagraph.setNextRenderer(new BorderParagraphRenderer(renderedParagraph));
  51.         doc.add(renderedParagraph);
  52.         doc.add(new Paragraph("This paragraph no longer has a border."));
  53.         doc.add(new Paragraph("Let's repeat:"));
  54.         for (int i = 0; i < 10; i++) {
  55.             renderedParagraph = new Paragraph("This paragraph now has a border. Isn't that fantastic? " +
  56.                     "By changing the event, we can even provide a background color, " +
  57.                     "change the line width of the border and many other things. Now let's deactivate the event.");
  58.             renderedParagraph.setNextRenderer(new BorderParagraphRenderer(renderedParagraph));
  59.             doc.add(renderedParagraph);
  60.             doc.add(new Paragraph("This paragraph no longer has a border."));
  61.         }
  62.         doc.close();
  63.     }
  64.  
  65.  
  66.     private class BorderParagraphRenderer extends ParagraphRenderer {
  67.         public BorderParagraphRenderer(Paragraph modelElement) {
  68.             super(modelElement);
  69.         }
  70.  
  71.         @Override
  72.         public void draw(DrawContext drawContext) {
  73.             super.draw(drawContext);
  74.         }
  75.  
  76.         @Override
  77.         public void drawBorder(DrawContext drawContext) {
  78.             super.drawBorder(drawContext);
  79.             Rectangle rect = getOccupiedAreaBBox();
  80.             drawContext.getCanvas().rectangle(rect.getLeft(), rect.getBottom(),
  81.                     rect.getWidth(), rect.getHeight());
  82.             drawContext.getCanvas().stroke();
  83.         }
  84.  
  85.         @Override
  86.         public IRenderer getNextRenderer() {
  87.             return new BorderParagraphRenderer((Paragraph) modelElement);
  88.         }
  89.     }
  90. }
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