import org.apache.poi.sl.usermodel.TableCell; import org.apache.poi.sl.usermodel.TextParagraph; import org.apache.poi.xslf.usermodel.*; import java.awt.*; import java.awt.geom.Rectangle2D; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * Created by jchamarthi on 18/9/15. */ public class CreateTable { public static void main(String[] args) throws IOException { XMLSlideShow ppt = new XMLSlideShow(); createTitleSlide(ppt); createTitleAndContentSlide(ppt); FileOutputStream out = new FileOutputStream(new File("table.pptx")); ppt.write(out); out.close(); } private static void createTitleSlide(XMLSlideShow ppt) { XSLFSlideMaster slideMaster = ppt.getSlideMasters().get(0); XSLFSlide slide = ppt.createSlide(slideMaster.getLayout(SlideLayout.TITLE)); //slide.getBackground().setFillColor(Color.BLUE); for(XSLFTextShape shape : slide.getPlaceholders()) { switch (shape.getTextType()) { case CENTERED_TITLE: shape.clearText(); XSLFTextParagraph paragraph = shape.addNewTextParagraph(); XSLFTextRun textRun = paragraph.addNewTextRun(); textRun.setFontColor(Color.BLACK); textRun.setFontSize(24.00); textRun.setBold(true); textRun.setFontFamily("Arial"); textRun.setText("Success Plan summary export when Status = Draft"); break; case SUBTITLE: slide.removeShape(shape); break; default: break; } } } private static void createTitleAndContentSlide(XMLSlideShow ppt) { XSLFSlideMaster slideMaster = ppt.getSlideMasters().get(0); XSLFSlide slide = ppt.createSlide(slideMaster.getLayout(SlideLayout.TITLE_AND_CONTENT)); for(XSLFTextShape shape : slide.getPlaceholders()) { switch (shape.getTextType()) { case TITLE: shape.clearText(); XSLFTextParagraph paragraph = shape.addNewTextParagraph(); paragraph.setTextAlign(TextParagraph.TextAlign.LEFT); XSLFTextRun textRun = paragraph.addNewTextRun(); textRun.setFontColor(Color.BLACK); textRun.setFontSize(18.00); textRun.setBold(true); textRun.setFontFamily("Arial"); textRun.setText("Success Plan Name - This can be of 80 characters. This can be of 80 characters"); break; case CONTENT: slide.removeShape(shape); createTable(slide, 50, 100, 600, 100, 3, 2, false, false); createTable(slide, 50, 200, 600, 300, 10, 3, false, false); break; default: break; } } } private static void createTable(XSLFSlide slide, double x, double y, double w, double h, int numRows, int numColumns, boolean setBorder, boolean addHeader) { XSLFTable tbl = slide.createTable(); tbl.setAnchor(new Rectangle2D.Double(x, y, w, h)); //Header if(addHeader) { XSLFTableRow headerRow = tbl.addRow(); for (int i = 0; i < numColumns; i++) { XSLFTableCell th = headerRow.addCell(); XSLFTextParagraph p = th.addNewTextParagraph(); p.setTextAlign(TextParagraph.TextAlign.LEFT); XSLFTextRun r = p.addNewTextRun(); r.setText("Plan Header " + (i + 1)); r.setBold(true); th.setBorderColor(TableCell.BorderEdge.top,Color.BLACK); th.setBorderColor(TableCell.BorderEdge.bottom,Color.BLACK); th.setBorderColor(TableCell.BorderEdge.left,Color.BLACK); th.setBorderColor(TableCell.BorderEdge.right,Color.BLACK); } } // rows for(int rownum = 0; rownum < numRows; rownum ++){ XSLFTableRow tr = tbl.addRow(); tr.setHeight(5); // header for(int i = 0; i < numColumns; i++) { XSLFTableCell cell = tr.addCell(); if(rownum == 0 || setBorder) { cell.setBorderColor(TableCell.BorderEdge.top,Color.BLACK); } if(rownum == numRows - 1 || setBorder) { cell.setBorderColor(TableCell.BorderEdge.bottom,Color.BLACK); } cell.setBorderColor(TableCell.BorderEdge.left,Color.BLACK); cell.setBorderColor(TableCell.BorderEdge.right,Color.BLACK); XSLFTextParagraph p = cell.addNewTextParagraph(); XSLFTextRun r = p.addNewTextRun(); r.setText("Plan Status:::::::::::::::::::::::::::::::::::ggggggggggggggggggg"); System.out.println( cell.getTextHeight()); } } } } ------------------------------------------------------------------------------- 176.0 176.0 Exception in thread "main" java.lang.NullPointerException at org.apache.poi.xslf.usermodel.XSLFTable.updateCellAnchor(XSLFTable.java:296) at org.apache.poi.xslf.usermodel.XSLFTableCell.getAnchor(XSLFTableCell.java:662) at org.apache.poi.sl.draw.DrawShape.getAnchor(DrawShape.java:146) at org.apache.poi.sl.draw.DrawTextParagraph.getWrappingWidth(DrawTextParagraph.java:422) at org.apache.poi.sl.draw.DrawTextParagraph.breakText(DrawTextParagraph.java:243) at org.apache.poi.sl.draw.DrawTextShape.drawParagraphs(DrawTextShape.java:158) at org.apache.poi.sl.draw.DrawTextShape.getTextHeight(DrawTextShape.java:219) at org.apache.poi.sl.draw.DrawTextShape.getTextHeight(DrawTextShape.java:201) at org.apache.poi.xslf.usermodel.XSLFTextShape.getTextHeight(XSLFTextShape.java:564) at CreateTable.createTable(CreateTable.java:117) at CreateTable.createTitleAndContentSlide(CreateTable.java:69) at CreateTable.main(CreateTable.java:19) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)