Rectangle examples

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

8th November 2015
admin-marketing

Switch code for this example

DrawRectangle.java
  1. /**
  2.  * This code sample was written by Bruno Lowagie in answer to this question:
  3.  * http://stackoverflow.com/questions/21718712/itext-pdfcontentbyte-rectanglerectangle-does-not-behave-as-expected
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Rectangle;
  10. import com.itextpdf.text.pdf.PdfContentByte;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12.  
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16.  
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class DrawRectangle {
  21.  
  22.     public static final String DEST = "results/objects/draw_rectangle.pdf";
  23.    
  24.     public static void main(String[] args) throws IOException, DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new DrawRectangle().createPdf(DEST);
  28.     }
  29.     public void createPdf(String dest) throws IOException, DocumentException {
  30.         Document document = new Document();
  31.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  32.         document.open();
  33.  
  34.         PdfContentByte canvas = writer.getDirectContent();
  35.         Rectangle rect = new Rectangle(36, 36, 559, 806);
  36.         rect.setBorder(Rectangle.BOX);
  37.         rect.setBorderWidth(2);
  38.         canvas.rectangle(rect);
  39.         document.close();
  40.     }
  41. }
Rectangles.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/24582887/rectangle-overlapping-in-itext-pdf-generating
  4.  */
  5. package sandbox.objects;
  6.  
  7. import com.itextpdf.text.BaseColor;
  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.PdfWriter;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class Rectangles {
  21.     public static final String DEST = "results/objects/rectangles.pdf";
  22.    
  23.     public static void main(String[] args) throws IOException,
  24.             DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new Rectangles().createPdf(DEST);
  28.     }
  29.    
  30.     public void createPdf(String dest) throws IOException, DocumentException {
  31.         Document document = new Document();
  32.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  33.         document.open();
  34.         float llx = 36;
  35.         float lly = 700;
  36.         float urx = 200;
  37.         float ury = 806;
  38.         PdfContentByte canvas = writer.getDirectContent();
  39.         Rectangle rect1 = new Rectangle(llx, lly, urx, ury);
  40.         rect1.setBackgroundColor(BaseColor.LIGHT_GRAY);
  41.         rect1.setBorder(Rectangle.BOX);
  42.         rect1.setBorderWidth(1);
  43.         canvas.rectangle(rect1);
  44.         Rectangle rect2 = new Rectangle(llx + 60, lly, urx, ury - 40);
  45.         rect2.setBackgroundColor(BaseColor.DARK_GRAY);
  46.         rect2.setBorder(Rectangle.BOX);
  47.         rect2.setBorderColor(BaseColor.WHITE);
  48.         rect2.setBorderWidth(0.5f);
  49.         canvas.rectangle(rect2);
  50.         document.close();
  51.     }
  52. }
RectangleInCell.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/21625435/c-sharp-add-rectangle-into-pdfpcell-itextsharp
  4.  *
  5.  * Adding a Rectangle object inside a table cell.
  6.  */
  7. package sandbox.objects;
  8.  
  9. import com.itextpdf.text.BaseColor;
  10. import com.itextpdf.text.Document;
  11. import com.itextpdf.text.DocumentException;
  12. import com.itextpdf.text.Image;
  13. import com.itextpdf.text.Paragraph;
  14. import com.itextpdf.text.pdf.PdfContentByte;
  15. import com.itextpdf.text.pdf.PdfPCell;
  16. import com.itextpdf.text.pdf.PdfPTable;
  17. import com.itextpdf.text.pdf.PdfTemplate;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23.  
  24. import sandbox.WrapToTest;
  25.  
  26. @WrapToTest
  27. public class RectangleInCell {
  28.     public static final String DEST = "results/objects/rectangle_in_cell.pdf";
  29.    
  30.     public static void main(String[] args) throws IOException,
  31.             DocumentException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new RectangleInCell().createPdf(DEST);
  35.     }
  36.    
  37.     public void createPdf(String dest) throws IOException, DocumentException {
  38.         Document document = new Document();
  39.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  40.         document.open();
  41.         document.add(new Paragraph("Option 1:"));
  42.         PdfPTable table = new PdfPTable(3);
  43.         table.addCell("A rectangle:");
  44.         PdfTemplate template = writer.getDirectContent().createTemplate(120, 80);
  45.         template.setColorFill(BaseColor.RED);
  46.         template.rectangle(0, 0, 120, 80);
  47.         template.fill();
  48.         writer.releaseTemplate(template);
  49.         table.addCell(Image.getInstance(template));
  50.         table.addCell("The rectangle is scaled to fit inside the cell, you see a padding.");
  51.         document.add(table);
  52.         document.add(new Paragraph("Option 2:"));
  53.         table = new PdfPTable(3);
  54.         table.addCell("A rectangle:");
  55.         PdfPCell cell = new PdfPCell(Image.getInstance(template));
  56.         table.addCell(cell);
  57.         table.addCell("The rectangle keeps its original size, but can overlap other cells in the same row.");
  58.         document.add(table);
  59.         document.add(new Paragraph("Option 3:"));
  60.         table = new PdfPTable(3);
  61.         table.addCell("A rectangle:");
  62.         cell = new PdfPCell(Image.getInstance(template), true);
  63.         table.addCell(cell);
  64.         table.addCell("The rectangle is scaled to fit inside the cell, no padding.");
  65.         document.add(table);
  66.         PdfContentByte cb = writer.getDirectContent();
  67.         cb.moveTo(228, 810);
  68.         cb.lineTo(338, 810);
  69.         cb.stroke();
  70.         document.close();
  71.     }
  72. }
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