Fit text in cell

31st May 2016
admin-marketing

Switch code for this example

ClipCenterCellContent.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/22095320/can-i-tell-itext-how-to-clip-text-to-fit-in-a-cell
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  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.kernel.pdf.canvas.PdfCanvas;
  18. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  19. import com.itextpdf.layout.Canvas;
  20. import com.itextpdf.layout.Document;
  21. import com.itextpdf.layout.element.Cell;
  22. import com.itextpdf.layout.element.Paragraph;
  23. import com.itextpdf.layout.element.Table;
  24. import com.itextpdf.layout.layout.LayoutArea;
  25. import com.itextpdf.layout.layout.LayoutContext;
  26. import com.itextpdf.layout.layout.LayoutResult;
  27. import com.itextpdf.layout.renderer.CellRenderer;
  28. import com.itextpdf.layout.renderer.DrawContext;
  29. import com.itextpdf.layout.renderer.IRenderer;
  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 ClipCenterCellContent extends GenericTest {
  39.     public static final String DEST = "./target/test/resources/sandbox/tables/clip_center_cell_content.pdf";
  40.  
  41.     public static void main(String[] args) throws Exception {
  42.         File file = new File(DEST);
  43.         file.getParentFile().mkdirs();
  44.         new ClipCenterCellContent().manipulatePdf(DEST);
  45.     }
  46.  
  47.     @Override
  48.     protected void manipulatePdf(String dest) throws Exception {
  49.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  50.         Document doc = new Document(pdfDoc);
  51.  
  52.         Table table = new Table(5);
  53.         Cell cell;
  54.         for (int r = 'A'; r
TruncateTextInCell.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/22093488/itext-how-do-i-get-the-rendered-dimensions-of-text
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.kernel.font.PdfFont;
  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.Paragraph;
  20. import com.itextpdf.layout.element.Table;
  21. import com.itextpdf.layout.layout.LayoutContext;
  22. import com.itextpdf.layout.layout.LayoutResult;
  23. import com.itextpdf.layout.property.Property;
  24. import com.itextpdf.layout.renderer.CellRenderer;
  25. import com.itextpdf.layout.renderer.IRenderer;
  26. import com.itextpdf.samples.GenericTest;
  27. import com.itextpdf.test.annotations.type.SampleTest;
  28.  
  29. import org.junit.experimental.categories.Category;
  30.  
  31. import java.io.File;
  32. import java.io.IOException;
  33.  
  34. @Category(SampleTest.class)
  35. public class TruncateTextInCell extends GenericTest {
  36.     public static final String DEST = "./target/test/resources/sandbox/tables/truncate_text_in_cell.pdf";
  37.  
  38.     public static void main(String[] args) throws Exception {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new TruncateTextInCell().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.         Table table = new Table(5);
  50.         Cell cell;
  51.         for (int r = 'A'; r  0)
  52.                     rightChar--;
  53.                 else
  54.                     break;
  55.             }
  56.             String newContent = content.substring(0, leftChar) + "..." + content.substring(rightChar);
  57.             Paragraph p = new Paragraph(newContent);
  58.  
  59.             IRenderer pr = p.createRendererSubTree().setParent(this);
  60.             this.childRenderers.add(pr);
  61.             return super.layout(layoutContext);
  62.         }
  63.     }
  64. }
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