Fit text in cell

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

5th November 2015
admin-marketing

Switch code for this example

TruncateTextInCell.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/22093488/itext-how-do-i-get-the-rendered-dimensions-of-text
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.ExceptionConverter;
  10. import com.itextpdf.text.Font;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.Rectangle;
  13. import com.itextpdf.text.pdf.BaseFont;
  14. import com.itextpdf.text.pdf.ColumnText;
  15. import com.itextpdf.text.pdf.PdfContentByte;
  16. import com.itextpdf.text.pdf.PdfPCell;
  17. import com.itextpdf.text.pdf.PdfPCellEvent;
  18. import com.itextpdf.text.pdf.PdfPTable;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20.  
  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24.  
  25. import sandbox.WrapToTest;
  26.  
  27. @WrapToTest
  28. public class TruncateTextInCell {
  29.    
  30.     public static final String DEST = "results/tables/truncate_cell_content.pdf";
  31.  
  32.     public class TruncateContent implements PdfPCellEvent {
  33.         protected String content;
  34.         public TruncateContent(String content) {
  35.             this.content = content;
  36.         }
  37.         public void cellLayout(PdfPCell cell, Rectangle position,
  38.                 PdfContentByte[] canvases) {
  39.             try {
  40.                 BaseFont bf = BaseFont.createFont();
  41.                 Font font = new Font(bf, 12);
  42.                 float availableWidth = position.getWidth();
  43.                 int contentLength = content.length();
  44.                 int leftChar = 0;
  45.                 int rightChar = contentLength - 1;
  46.                 availableWidth -= bf.getWidthPoint("...", 12);
  47.                 while (leftChar < contentLength && rightChar != leftChar) {
  48.                     availableWidth -= bf.getWidthPoint(content.charAt(leftChar), 12);
  49.                     if (availableWidth > 0)
  50.                         leftChar++;
  51.                     else
  52.                         break;
  53.                     availableWidth -= bf.getWidthPoint(content.charAt(rightChar), 12);
  54.                     if (availableWidth > 0)
  55.                         rightChar--;
  56.                     else
  57.                         break;
  58.                 }
  59.                 String newContent = content.substring(0, leftChar) + "..." + content.substring(rightChar);
  60.                 PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
  61.                 ColumnText ct = new ColumnText(canvas);
  62.                 ct.setSimpleColumn(position);
  63.                 ct.addElement(new Paragraph(newContent, font));
  64.                 ct.go();
  65.             } catch (DocumentException e) {
  66.                 throw new ExceptionConverter(e);
  67.             } catch (IOException e) {
  68.                 throw new ExceptionConverter(e);
  69.             }
  70.         }
  71.     }
  72.    
  73.     public static void main(String[] args) throws IOException, DocumentException {
  74.         File file = new File(DEST);
  75.         file.getParentFile().mkdirs();
  76.         new TruncateTextInCell().createPdf(DEST);
  77.     }
  78.    
  79.     public void createPdf(String dest) throws IOException, DocumentException {
  80.         Document document = new Document();
  81.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  82.         document.open();
  83.         PdfPTable table = new PdfPTable(5);
  84.         table.setWidthPercentage(100);
  85.         PdfPCell cell;
  86.         for (int r = 'A'; r
ClipCenterCellContent.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/22095320/can-i-tell-itext-how-to-clip-text-to-fit-in-a-cell
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.ExceptionConverter;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.ColumnText;
  13. import com.itextpdf.text.pdf.PdfContentByte;
  14. import com.itextpdf.text.pdf.PdfPCell;
  15. import com.itextpdf.text.pdf.PdfPCellEvent;
  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 ClipCenterCellContent {
  28.    
  29.     public static final String DEST = "results/tables/clip_center_cell_content.pdf";
  30.  
  31.     public class CenterContent implements PdfPCellEvent {
  32.         protected Paragraph content;
  33.         public CenterContent(Paragraph content) {
  34.             this.content = content;
  35.         }
  36.         public void cellLayout(PdfPCell cell, Rectangle position,
  37.                 PdfContentByte[] canvases) {
  38.             try {
  39.                 PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
  40.                 ColumnText ct = new ColumnText(canvas);
  41.                 ct.setSimpleColumn(new Rectangle(0, 0, position.getWidth(), -1000));
  42.                 ct.addElement(content);
  43.                 ct.go(true);
  44.                 float spaceneeded = 0 - ct.getYLine();
  45.                 System.out.println(String.format("The content requires %s pt whereas the height is %s pt.", spaceneeded, position.getHeight()));
  46.                 float offset = (position.getHeight() - spaceneeded) / 2;
  47.                 System.out.println(String.format("The difference is %s pt; we'll need an offset of %s pt.", -2f * offset, offset));
  48.                 PdfTemplate tmp = canvas.createTemplate(position.getWidth(), position.getHeight());
  49.                 ct = new ColumnText(tmp);
  50.                 ct.setSimpleColumn(0, offset, position.getWidth(), offset + spaceneeded);
  51.                 ct.addElement(content);
  52.                 ct.go();
  53.                 canvas.addTemplate(tmp, position.getLeft(), position.getBottom());
  54.             } catch (DocumentException e) {
  55.                 throw new ExceptionConverter(e);
  56.             }
  57.         }
  58.     }
  59.    
  60.     public static void main(String[] args) throws IOException, DocumentException {
  61.         File file = new File(DEST);
  62.         file.getParentFile().mkdirs();
  63.         new ClipCenterCellContent().createPdf(DEST);
  64.     }
  65.    
  66.     public void createPdf(String dest) throws IOException, DocumentException {
  67.         Document document = new Document();
  68.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  69.         document.open();
  70.         PdfPTable table = new PdfPTable(5);
  71.         table.setWidthPercentage(100);
  72.         PdfPCell cell;
  73.         for (int r = 'A'; r
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