Rotating cell content

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

16th May 2016
admin-marketing

Switch code for this example

RotatedCell.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/37246838
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Element;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.pdf.PdfPCell;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17.  
  18. /**
  19.  * @author Bruno Lowagie (iText Software)
  20.  */
  21. public class RotatedCell {
  22.     public static final String DEST = "results/tables/rotated_cell.pdf";
  23.  
  24.     public static void main(String[] args) throws IOException,
  25.             DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new RotatedCell().createPdf(DEST);
  29.     }
  30.     public void createPdf(String dest) throws IOException, DocumentException {
  31.         Document document = new Document();
  32.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  33.         document.open();
  34.         PdfPTable table = new PdfPTable(8);
  35.         for (int i = 0; i < 8; i++) {
  36.             PdfPCell cell =
  37.                 new PdfPCell(new Phrase(String.format("May %s, 2016", i + 15)));
  38.             cell.setRotation(90);
  39.             cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  40.             table.addCell(cell);
  41.         }
  42.         for(int i = 0; i < 16; i++){
  43.             table.addCell("hi");
  44.         }
  45.         document.add(table);
  46.         document.close();
  47.     }
  48.    
  49. }
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