Adding images to a table

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

31st May 2016
admin-marketing

Switch code for this example

AddOverlappingImage.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 the following question:
  10.  * http://stackoverflow.com/questions/22094289/itext-precisely-position-an-image-on-top-of-a-pdfptable
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.io.image.ImageData;
  15. import com.itextpdf.io.image.ImageDataFactory;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.layout.Document;
  19. import com.itextpdf.layout.element.Cell;
  20. import com.itextpdf.layout.element.Paragraph;
  21. import com.itextpdf.layout.element.Table;
  22. import com.itextpdf.layout.renderer.DrawContext;
  23. import com.itextpdf.layout.renderer.TableRenderer;
  24. import com.itextpdf.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. import java.io.File;
  30.  
  31. @Category(SampleTest.class)
  32. public class AddOverlappingImage extends GenericTest {
  33.     public static final String DEST = "./target/test/resources/sandbox/tables/add_overlapping_image.pdf";
  34.  
  35.     public static void main(String[] args) throws Exception {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new AddOverlappingImage().manipulatePdf(DEST);
  39.     }
  40.  
  41.     @Override
  42.     protected void manipulatePdf(String dest) throws Exception {
  43.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  44.         Document doc = new Document(pdfDoc);
  45.  
  46.         Table table = new Table(5);
  47.         table.setNextRenderer(new OverlappingImageTableRenderer(table, new Table.RowRange(0, 25),
  48.                 ImageDataFactory.create("./src/test/resources/img/hero.jpg")));
  49.         Cell cell;
  50.         for (int r = 'A'; r
IconDescriptionTable.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 example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/23375618/how-to-add-an-icon-to-an-itext-pdfpcell
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.io.image.ImageDataFactory;
  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.Image;
  19. import com.itextpdf.layout.element.Table;
  20. import com.itextpdf.samples.GenericTest;
  21. import com.itextpdf.test.annotations.type.SampleTest;
  22.  
  23. import org.junit.experimental.categories.Category;
  24.  
  25. import java.io.File;
  26.  
  27. @Category(SampleTest.class)
  28. public class IconDescriptionTable extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/tables/icon_description_table.pdf";
  30.     public static final String IMG = "./src/test/resources/img/bulb.gif";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new IconDescriptionTable().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.  
  43.         Table table = new Table(new float[]{1, 9});
  44.         table.setWidthPercent(100);
  45.         Image img = new Image(ImageDataFactory.create(IMG));
  46.         table.addCell(img.setAutoScale(true));
  47.         table.addCell("A light bulb icon");
  48.         doc.add(table);
  49.  
  50.         doc.close();
  51.     }
  52. }
ImageNextToText.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/25780258/how-to-display-image-and-text-beside-each-other-itext
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.io.image.ImageDataFactory;
  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.border.Border;
  19. import com.itextpdf.layout.element.Cell;
  20. import com.itextpdf.layout.element.Image;
  21. import com.itextpdf.layout.element.Paragraph;
  22. import com.itextpdf.layout.element.Table;
  23. import com.itextpdf.layout.property.TextAlignment;
  24. import com.itextpdf.layout.property.VerticalAlignment;
  25. import com.itextpdf.samples.GenericTest;
  26. import com.itextpdf.test.annotations.type.SampleTest;
  27.  
  28. import org.junit.experimental.categories.Category;
  29.  
  30. import java.io.File;
  31. import java.net.MalformedURLException;
  32.  
  33. @Category(SampleTest.class)
  34. public class ImageNextToText extends GenericTest {
  35.     public static final String DEST = "./target/test/resources/sandbox/tables/image_next_to_text.pdf";
  36.     public static final String IMG1 = "./src/test/resources/img/javaone2013.jpg";
  37.  
  38.     public static void main(String[] args) throws Exception {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new ImageNextToText().manipulatePdf(DEST);
  42.     }
  43.  
  44.     public static Cell createImageCell(String path) throws MalformedURLException {
  45.         Image img = new Image(ImageDataFactory.create(path));
  46.         Cell cell = new Cell().add(img.setAutoScale(true));
  47.         cell.setBorder(null);
  48.         return cell;
  49.     }
  50.  
  51.     public static Cell createTextCell(String text) {
  52.         Cell cell = new Cell();
  53.         Paragraph p = new Paragraph(text);
  54.         p.setTextAlignment(TextAlignment.RIGHT);
  55.         cell.add(p).setVerticalAlignment(VerticalAlignment.BOTTOM);
  56.         cell.setBorder(Border.NO_BORDER);
  57.         return cell;
  58.     }
  59.  
  60.     @Override
  61.     protected void manipulatePdf(String dest) throws Exception {
  62.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  63.         Document doc = new Document(pdfDoc);
  64.         Table table = new Table(new float[]{1, 2});
  65.         table.setWidthPercent(100);
  66.         table.addCell(createImageCell(IMG1));
  67.         table.addCell(createTextCell("This picture was taken at Java One.\nIt shows the iText crew at Java One in 2013."));
  68.         doc.add(table);
  69.  
  70.         doc.close();
  71.     }
  72. }
ImagesInChunkInCell.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 the following question:
  10.  * http://stackoverflow.com/questions/32130219/itext-list-of-images-in-a-cell
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.io.image.ImageDataFactory;
  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.Image;
  19. import com.itextpdf.layout.element.Paragraph;
  20. import com.itextpdf.layout.element.Table;
  21. import com.itextpdf.layout.element.Text;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. import java.io.File;
  28.  
  29. @Category(SampleTest.class)
  30. public class ImagesInChunkInCell extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/tables/images_in_chunk_in_cell.pdf";
  32.     public static final String IMG = "./src/test/resources/img/bulb.gif";
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new ImagesInChunkInCell().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.         Image image = new Image(ImageDataFactory.create(IMG));
  45.         Table table = new Table(new float[]{120});
  46.         Paragraph listOfDots = new Paragraph();
  47.         for (int i = 0; i < 40; i++) {
  48.             listOfDots.add(image);
  49.             listOfDots.add(new Text(" "));
  50.         }
  51.         table.addCell(listOfDots);
  52.         doc.add(table);
  53.         doc.close();
  54.     }
  55. }
ImagesNextToEachOther.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/19700549/itextsharp-images-are-not-coming-next-to-one-another
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.io.image.ImageDataFactory;
  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.Cell;
  19. import com.itextpdf.layout.element.Image;
  20. import com.itextpdf.layout.element.Table;
  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. import java.net.MalformedURLException;
  28.  
  29. @Category(SampleTest.class)
  30. public class ImagesNextToEachOther extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/tables/image_next_to_each_other.pdf";
  32.     public static final String IMG1 = "./src/test/resources/img/javaone2013.jpg";
  33.     public static final String IMG2 = "./src/test/resources/img/berlin2013.jpg";
  34.  
  35.     public static void main(String[] args) throws Exception {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new ImagesNextToEachOther().manipulatePdf(DEST);
  39.     }
  40.  
  41.     public static Cell createImageCell(String path) throws MalformedURLException {
  42.         Image img = new Image(ImageDataFactory.create(path));
  43.         return new Cell().add(img.setAutoScale(true));
  44.     }
  45.  
  46.     @Override
  47.     protected void manipulatePdf(String dest) throws Exception {
  48.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  49.         Document doc = new Document(pdfDoc);
  50.  
  51.         Table table = new Table(2);
  52.         table.addCell(createImageCell(IMG1));
  53.         table.addCell(createImageCell(IMG2));
  54.         doc.add(table);
  55.  
  56.         doc.close();
  57.     }
  58. }
MultipleImagesInCell.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/33603296/how-to-add-two-images-in-one-cell-in-itext
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.io.image.ImageDataFactory;
  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.AreaBreak;
  19. import com.itextpdf.layout.element.Cell;
  20. import com.itextpdf.layout.element.Image;
  21. import com.itextpdf.layout.element.Paragraph;
  22. import com.itextpdf.layout.element.Table;
  23. import com.itextpdf.samples.GenericTest;
  24. import com.itextpdf.test.annotations.type.SampleTest;
  25.  
  26. import java.io.File;
  27.  
  28. import org.junit.experimental.categories.Category;
  29.  
  30. @Category(SampleTest.class)
  31. public class MultipleImagesInCell extends GenericTest {
  32.     public static final String DEST
  33.             = "./target/test/resources/sandbox/tables/multiple_images_in_cell.pdf";
  34.     public static final String IMG1
  35.             = "./src/test/resources/img/brasil.png";
  36.     public static final String IMG2
  37.             = "./src/test/resources/img/dog.bmp";
  38.     public static final String IMG3
  39.             = "./src/test/resources/img/fox.bmp";
  40.  
  41.  
  42.     public static void main(String[] args) throws Exception {
  43.         File file = new File(DEST);
  44.         file.getParentFile().mkdirs();
  45.         new MultipleImagesInCell().manipulatePdf(DEST);
  46.     }
  47.  
  48.     @Override
  49.     protected void manipulatePdf(String dest) throws Exception {
  50.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  51.         Document doc = new Document(pdfDoc);
  52.  
  53.         Image img1 = new Image(ImageDataFactory.create(IMG1));
  54.         Image img2 = new Image(ImageDataFactory.create(IMG2));
  55.         Image img3 = new Image(ImageDataFactory.create(IMG3));
  56.  
  57.         Table table = new Table(1);
  58.         table.setWidthPercent(50);
  59.         table.addCell("Different images, one after the other vertically:");
  60.         Cell cell = new Cell();
  61.  
  62.         // There's no image autoscaling by default
  63.         cell.add(img1.setAutoScale(true));
  64.         cell.add(img2.setAutoScale(true));
  65.         cell.add(img3.setAutoScale(true));
  66.         table.addCell(cell);
  67.         doc.add(table);
  68.         doc.add(new AreaBreak());
  69.  
  70.         // In the snippet after this autoscaling is not needed
  71.         // Notice that we do not need to create new Image instances since the images had been already flushed
  72.         img1.setAutoScale(false);
  73.         img2.setAutoScale(false);
  74.         img3.setAutoScale(false);
  75.  
  76.         table = new Table(1);
  77.         table.addCell("Different images, one after the other vertically, but scaled:");
  78.         cell = new Cell();
  79.         img1.setWidthPercent(20);
  80.         cell.add(img1);
  81.         img2.setWidthPercent(20);
  82.         cell.add(img2);
  83.         img3.setWidthPercent(20);
  84.         cell.add(img3);
  85.         table.addCell(cell);
  86.         table.addCell("Different images, one after the other horizontally:");
  87.  
  88.         // Notice that the table is not flushed yet so it's strictly forbidden to change image properties yet
  89.         img1 = new Image(ImageDataFactory.create(IMG1));
  90.         img2 = new Image(ImageDataFactory.create(IMG2));
  91.         img3 = new Image(ImageDataFactory.create(IMG3));
  92.  
  93.         Paragraph p = new Paragraph();
  94.         img1.scale(0.3f, 0.3f);
  95.         p.add(img1);
  96.         p.add(img2);
  97.         p.add(img3);
  98.         table.addCell(p);
  99.         table.addCell("Text and images (mixed):");
  100.  
  101.         img2 = new Image(ImageDataFactory.create(IMG2));
  102.         img3 = new Image(ImageDataFactory.create(IMG3));
  103.  
  104.         p = new Paragraph("The quick brown ");
  105.         p.add(img3);
  106.         p.add(" jumps over the lazy ");
  107.         p.add(img2);
  108.         cell = new Cell();
  109.         cell.add(p);
  110.         table.addCell(cell);
  111.  
  112.         doc.add(table);
  113.  
  114.         doc.close();
  115.     }
  116. }
MultipleImagesInTable.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/34303448
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.io.image.ImageDataFactory;
  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.Image;
  19. import com.itextpdf.layout.element.Table;
  20. import com.itextpdf.samples.GenericTest;
  21. import com.itextpdf.test.annotations.type.SampleTest;
  22.  
  23. import java.io.File;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. @Category(SampleTest.class)
  28. public class MultipleImagesInTable extends GenericTest {
  29.     public static final String DEST
  30.             = "./target/test/resources/sandbox/tables/multiple_images_in_table.pdf";
  31.     public static final String IMG1
  32.             = "./src/test/resources/img/brasil.png";
  33.     public static final String IMG2
  34.             = "./src/test/resources/img/dog.bmp";
  35.     public static final String IMG3
  36.             = "./src/test/resources/img/fox.bmp";
  37.  
  38.     public static void main(String[] args) throws Exception {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new MultipleImagesInTable().manipulatePdf(DEST);
  42.     }
  43.  
  44.     @Override
  45.     protected void manipulatePdf(String dest) throws Exception {
  46.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  47.         Document doc = new Document(pdfDoc);
  48.  
  49.         Image img1 = new Image(ImageDataFactory.create(IMG1));
  50.         Image img2 = new Image(ImageDataFactory.create(IMG2));
  51.         Image img3 = new Image(ImageDataFactory.create(IMG3));
  52.  
  53.         Table table = new Table(1);
  54.         table.setWidthPercent(20);
  55.  
  56.         img1.setAutoScale(true);
  57.         img2.setAutoScale(true);
  58.         img3.setAutoScale(true);
  59.  
  60.         table.addCell(img1);
  61.         table.addCell("Brazil");
  62.         table.addCell(img2);
  63.         table.addCell("Dog");
  64.         table.addCell(img3);
  65.         table.addCell("Fox");
  66.  
  67.         doc.add(table);
  68.  
  69.         doc.close();
  70.     }
  71. }
PositionContentInCell.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 the following question:
  10.  * http://stackoverflow.com/questions/31169268/cell-background-image-with-text-itextsharp
  11.  */
  12.  
  13. package com.itextpdf.samples.sandbox.tables;
  14.  
  15. import com.itextpdf.io.image.ImageDataFactory;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.layout.Canvas;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.Cell;
  21. import com.itextpdf.layout.element.Image;
  22. import com.itextpdf.layout.element.Paragraph;
  23. import com.itextpdf.layout.element.Table;
  24. import com.itextpdf.layout.property.Leading;
  25. import com.itextpdf.layout.property.Property;
  26. import com.itextpdf.layout.property.TextAlignment;
  27. import com.itextpdf.layout.renderer.CellRenderer;
  28. import com.itextpdf.layout.renderer.DocumentRenderer;
  29. import com.itextpdf.layout.renderer.DrawContext;
  30. import com.itextpdf.samples.GenericTest;
  31. import com.itextpdf.test.annotations.type.SampleTest;
  32.  
  33. import org.junit.experimental.categories.Category;
  34.  
  35. import java.io.File;
  36.  
  37. @Category(SampleTest.class)
  38. public class PositionContentInCell extends GenericTest {
  39.     public static final String DEST = "./target/test/resources/sandbox/tables/position_content_in_cell.pdf";
  40.     public static final String IMG = "./src/test/resources/img/info.png";
  41.  
  42.     public enum POSITION {TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT}
  43.  
  44.     public static void main(String[] args) throws Exception {
  45.         File file = new File(DEST);
  46.         file.getParentFile().mkdirs();
  47.         new PositionContentInCell().manipulatePdf(DEST);
  48.     }
  49.  
  50.     @Override
  51.     protected void manipulatePdf(String dest) throws Exception {
  52.         // 1. Create a Document which contains a table:
  53.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  54.         Document doc = new Document(pdfDoc);
  55.         Table table = new Table(2);
  56.         Cell cell1 = new Cell();
  57.         Cell cell2 = new Cell();
  58.         Cell cell3 = new Cell();
  59.         Cell cell4 = new Cell();
  60.         // 2. Inside that table, make each cell with specific height:
  61.         cell1.setHeight(50);
  62.         cell2.setHeight(50);
  63.         cell3.setHeight(50);
  64.         cell4.setHeight(50);
  65.         // 3. Each cell has the same background image
  66.         // 4. Add text in front of the image at specific position
  67.         cell1.setNextRenderer(new ImageAndPositionRenderer(cell1,
  68.                 new Image(ImageDataFactory.create(IMG)), "Top left", POSITION.TOP_LEFT));
  69.         cell2.setNextRenderer(new ImageAndPositionRenderer(cell2,
  70.                 new Image(ImageDataFactory.create(IMG)), "Top right", POSITION.TOP_RIGHT));
  71.         cell3.setNextRenderer(new ImageAndPositionRenderer(cell3,
  72.                 new Image(ImageDataFactory.create(IMG)), "Bottom left", POSITION.BOTTOM_LEFT));
  73.         cell4.setNextRenderer(new ImageAndPositionRenderer(cell4,
  74.                 new Image(ImageDataFactory.create(IMG)), "Bottom right", POSITION.BOTTOM_RIGHT));
  75.         // Wrap it all up!
  76.         table.addCell(cell1);
  77.         table.addCell(cell2);
  78.         table.addCell(cell3);
  79.         table.addCell(cell4);
  80.         doc.add(table);
  81.  
  82.         doc.close();
  83.     }
  84.  
  85.  
  86.     private class ImageAndPositionRenderer extends CellRenderer {
  87.         private Image img;
  88.         private String content;
  89.         private POSITION position;
  90.  
  91.         public ImageAndPositionRenderer(Cell modelElement, Image img, String content, POSITION position) {
  92.             super(modelElement);
  93.             this.img = img;
  94.             this.content = content;
  95.             this.position = position;
  96.         }
  97.  
  98.         @Override
  99.         public void draw(DrawContext drawContext) {
  100.             super.draw(drawContext);
  101.             img.scaleToFit(getOccupiedAreaBBox().getWidth(), getOccupiedAreaBBox().getHeight());
  102.  
  103.             drawContext.getCanvas().addXObject(img.getXObject(),
  104.                     getOccupiedAreaBBox().getX() +
  105.                             (getOccupiedAreaBBox().getWidth()
  106.                                     - img.getImageWidth() * (float) img.getProperty(Property.HORIZONTAL_SCALING)) / 2,
  107.                     getOccupiedAreaBBox().getY() +
  108.                             (getOccupiedAreaBBox().getHeight()
  109.                                     - img.getImageHeight() * (float) img.getProperty(Property.VERTICAL_SCALING)) / 2,
  110.                     img.getImageWidth() * (float) img.getProperty(Property.HORIZONTAL_SCALING));
  111.             drawContext.getCanvas().stroke();
  112.  
  113.             Paragraph p = new Paragraph(content);
  114.             Leading leading = p.getDefaultProperty(Property.LEADING);
  115.             Float defaultFontSize = new DocumentRenderer(new Document(drawContext.getDocument())).getPropertyAsFloat(Property.FONT_SIZE);
  116.  
  117.             float x;
  118.             float y;
  119.             TextAlignment alignment;
  120.             switch (position) {
  121.                 case TOP_LEFT:
  122.                     x = getOccupiedAreaBBox().getLeft() + 3;
  123.                     y = getOccupiedAreaBBox().getTop() - defaultFontSize * leading.getValue();
  124.                     alignment = TextAlignment.LEFT;
  125.                     break;
  126.                 case TOP_RIGHT:
  127.                     x = getOccupiedAreaBBox().getRight() - 3;
  128.                     y = getOccupiedAreaBBox().getTop() - defaultFontSize * leading.getValue();
  129.                     alignment = TextAlignment.RIGHT;
  130.                     break;
  131.                 case BOTTOM_LEFT:
  132.                     x = getOccupiedAreaBBox().getLeft() + 3;
  133.                     y = getOccupiedAreaBBox().getBottom() + 3;
  134.                     alignment = TextAlignment.LEFT;
  135.                     break;
  136.                 case BOTTOM_RIGHT:
  137.                     x = getOccupiedAreaBBox().getRight() - 3;
  138.                     y = getOccupiedAreaBBox().getBottom() + 3;
  139.                     alignment = TextAlignment.RIGHT;
  140.                     break;
  141.                 default:
  142.                     x = 0;
  143.                     y = 0;
  144.                     alignment = TextAlignment.CENTER;
  145.             }
  146.             new Canvas(drawContext.getCanvas(), drawContext.getDocument(), getOccupiedAreaBBox()).showTextAligned(p, x, y, alignment);
  147.         }
  148.     }
  149. }
PositionContentInCell2.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 the following question:
  10.  * http://stackoverflow.com/questions/31169268/cell-background-image-with-text-itextsharp
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.io.image.ImageDataFactory;
  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.Cell;
  19. import com.itextpdf.layout.element.Image;
  20. import com.itextpdf.layout.element.Table;
  21. import com.itextpdf.layout.property.TextAlignment;
  22. import com.itextpdf.layout.renderer.CellRenderer;
  23. import com.itextpdf.layout.renderer.DrawContext;
  24. import com.itextpdf.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. import java.io.File;
  30.  
  31.  
  32. @Category(SampleTest.class)
  33. public class PositionContentInCell2 extends GenericTest {
  34.     public static final String DEST = "./target/test/resources/sandbox/tables/position_content_in_cell2.pdf";
  35.     public static final String IMG = "./src/test/resources/img/info.png";
  36.  
  37.     public static void main(String[] args) throws Exception {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new PositionContentInCell2().manipulatePdf(DEST);
  41.     }
  42.  
  43.     @Override
  44.     protected void manipulatePdf(String dest) throws Exception {
  45.         // 1. Create a Document which contains a table:
  46.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  47.         Document doc = new Document(pdfDoc);
  48.         Table table = new Table(2);
  49.         Cell cell1 = new Cell();
  50.         Cell cell2 = new Cell();
  51.         Cell cell3 = new Cell();
  52.         Cell cell4 = new Cell();
  53.         Cell cell5 = new Cell();
  54.         Cell cell6 = new Cell();
  55.         Cell cell7 = new Cell();
  56.         Cell cell8 = new Cell();
  57.         // 2. Inside that table, make each cell with specific height:
  58.         cell1.setHeight(50);
  59.         cell2.setHeight(50);
  60.         cell3.setHeight(50);
  61.         cell4.setHeight(50);
  62.         cell5.setHeight(50);
  63.         cell6.setHeight(50);
  64.         cell7.setHeight(50);
  65.         cell8.setHeight(50);
  66.         // 3. Each cell has the same background image
  67.         // 4. Add text in front of the image at specific position
  68.         cell1.setNextRenderer(new ImageAndPositionRenderer(cell1, 0, 1,
  69.                 new Image(ImageDataFactory.create(IMG)), "Top left", TextAlignment.LEFT));
  70.         cell2.setNextRenderer(new ImageAndPositionRenderer(cell2, 1, 1,
  71.                 new Image(ImageDataFactory.create(IMG)), "Top right", TextAlignment.RIGHT));
  72.         cell3.setNextRenderer(new ImageAndPositionRenderer(cell3, 0.5f, 1,
  73.                 new Image(ImageDataFactory.create(IMG)), "Top center", TextAlignment.CENTER));
  74.         cell4.setNextRenderer(new ImageAndPositionRenderer(cell4, 0.5f, 0,
  75.                 new Image(ImageDataFactory.create(IMG)), "Bottom center", TextAlignment.CENTER));
  76.         cell5.setNextRenderer(new ImageAndPositionRenderer(cell5, 0.5f, 0.5f,
  77.                 new Image(ImageDataFactory.create(IMG)), "Middle center", TextAlignment.CENTER));
  78.         cell6.setNextRenderer(new ImageAndPositionRenderer(cell6, 0.5f, 0.5f,
  79.                 new Image(ImageDataFactory.create(IMG)), "Middle center", TextAlignment.CENTER));
  80.         cell7.setNextRenderer(new ImageAndPositionRenderer(cell7, 0, 0,
  81.                 new Image(ImageDataFactory.create(IMG)), "Bottom left", TextAlignment.LEFT));
  82.         cell8.setNextRenderer(new ImageAndPositionRenderer(cell8, 1, 0,
  83.                 new Image(ImageDataFactory.create(IMG)), "Bottom right", TextAlignment.RIGHT));
  84.         // Wrap it all up!
  85.         table.addCell(cell1);
  86.         table.addCell(cell2);
  87.         table.addCell(cell3);
  88.         table.addCell(cell4);
  89.         table.addCell(cell5);
  90.         table.addCell(cell6);
  91.         table.addCell(cell7);
  92.         table.addCell(cell8);
  93.         doc.add(table);
  94.  
  95.         doc.close();
  96.     }
  97.  
  98.  
  99.     private class ImageAndPositionRenderer extends CellRenderer {
  100.         private Image img;
  101.         private String content;
  102.         private TextAlignment alignment;
  103.         private float wPct;
  104.         private float hPct;
  105.  
  106.         public ImageAndPositionRenderer(Cell modelElement, float wPct, float hPct,
  107.                                         Image img, String content, TextAlignment alignment) {
  108.             super(modelElement);
  109.             this.img = img;
  110.             this.content = content;
  111.             this.alignment = alignment;
  112.             this.wPct = wPct;
  113.             this.hPct = hPct;
  114.         }
  115.  
  116.         @Override
  117.         public void draw(DrawContext drawContext) {
  118.             super.draw(drawContext);
  119.  
  120.             drawContext.getCanvas().addXObject(img.getXObject(), getOccupiedAreaBBox());
  121.             drawContext.getCanvas().stroke();
  122.  
  123.             float x = getOccupiedAreaBBox().getX() + wPct * getOccupiedAreaBBox().getWidth();
  124.             float y = getOccupiedAreaBBox().getY() + hPct * (getOccupiedAreaBBox().getHeight() - drawContext.getCanvas().getGraphicsState().getLeading());
  125.             new Document(drawContext.getDocument()).showTextAligned(content, x, y, alignment);
  126.  
  127.         }
  128.     }
  129. }
SimpleTable8.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 the following question:
  10.  * http://stackoverflow.com/questions/29752875/generate-pdf-from-java-code-that-dynamically-continue-file-template-with-itext
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfReader;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  18. import com.itextpdf.layout.Document;
  19. import com.itextpdf.layout.element.Cell;
  20. import com.itextpdf.layout.element.Image;
  21. import com.itextpdf.layout.element.Table;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. import java.io.File;
  28.  
  29. @Category(SampleTest.class)
  30. public class SimpleTable8 extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/tables/simple_table8.pdf";
  32.     public static final String SRC = "./src/test/resources/pdfs/";
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new SimpleTable8().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.  
  45.         Table table = new Table(3);
  46.         PdfReader reader = new PdfReader(SRC + "header_test_file.pdf");
  47.         PdfDocument srcDoc = new PdfDocument(reader);
  48.         PdfFormXObject header = srcDoc.getFirstPage().copyAsFormXObject(pdfDoc);
  49.         Cell cell = new Cell(1, 3).add(new Image(header).setAutoScale(true));
  50.         table.addCell(cell);
  51.         for (int row = 1; row
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