Advertisement
kiwiwings

XSLF - remove table column

Dec 25th, 2017
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.35 KB | None | 0 0
  1. import java.awt.geom.Rectangle2D;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.util.Iterator;
  6. import java.util.List;
  7.  
  8. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  9. import org.apache.poi.xslf.usermodel.XSLFSlide;
  10. import org.apache.poi.xslf.usermodel.XSLFTable;
  11. import org.apache.poi.xslf.usermodel.XSLFTableCell;
  12. import org.apache.poi.xslf.usermodel.XSLFTableRow;
  13. import org.openxmlformats.schemas.drawingml.x2006.main.CTTableCell;
  14.  
  15. public class XslfRemoveTableColumn {
  16.     public static void main(String args[]) throws IOException {
  17.         // prepare sample table/slide with merged cells
  18.         XMLSlideShow ppt = new XMLSlideShow();
  19.         XSLFSlide sl = ppt.createSlide();
  20.         XSLFTable tab = sl.createTable();
  21.         initRow(tab, "111111");
  22.         initRow(tab, "122122");
  23.         initRow(tab, "112222");
  24.         initRow(tab, "133322");
  25.         initRow(tab, "333122");
  26.         tab.setAnchor(new Rectangle2D.Double(100, 100, 200, 200));
  27.        
  28.         try (FileOutputStream fos = new FileOutputStream("bla.pptx")) {
  29.             ppt.write(fos);
  30.         }
  31.         ppt.close();
  32.        
  33.         try (FileInputStream fis = new FileInputStream("bla.pptx")) {
  34.             ppt = new XMLSlideShow(fis);
  35.         }
  36.  
  37.         tab = (XSLFTable)ppt.getSlides().get(0).getShapes().get(0);
  38.        
  39.         // actually remove the column
  40.         removeColumn(tab, 3);
  41.        
  42.         try (FileOutputStream fos = new FileOutputStream("bla2.pptx")) {
  43.             ppt.write(fos);
  44.         }
  45.         ppt.close();
  46.     }
  47.    
  48.     private static void removeColumn(XSLFTable table, int col) {
  49. nextRow:for (final XSLFTableRow row : table) {
  50.             Iterator<XSLFTableCell> it = row.iterator();
  51.             int gridSpan;
  52.             for (int idx=0, idxXml=0; it.hasNext(); idx+=gridSpan, idxXml++) {
  53.                 final XSLFTableCell c = it.next();
  54.                 gridSpan = c.getGridSpan();
  55.                 if (idx <= col && col < idx+gridSpan) {
  56.                     if (gridSpan > 1) {
  57.                         ((CTTableCell)c.getXmlObject()).setGridSpan(gridSpan-1);
  58.                         // check for merged cells
  59.                         for (; it.hasNext() && idx<col; idx++, idxXml++) {
  60.                             final XSLFTableCell mergedC = it.next();
  61.                             if (!mergedC.isMerged()) {
  62.                                 continue nextRow;
  63.                             }
  64.                         }
  65.                     }
  66.                     row.getXmlObject().removeTc(idxXml);
  67.                     it.remove();
  68.                     break;
  69.                 }
  70.             }
  71.         }
  72.         table.getCTTable().getTblGrid().removeGridCol(col);
  73.     }
  74.    
  75.     private static List<XSLFTableCell> initRow(XSLFTable table, String iv) {
  76.         XSLFTableRow row = table.addRow();
  77.         for (int idx=0; idx<iv.length(); idx++) {
  78.             XSLFTableCell c = row.addCell();
  79.             c.setText(""+idx);
  80.             int span = iv.charAt(idx)-'0';
  81.             if (span > 1) {
  82.                 ((CTTableCell)c.getXmlObject()).setGridSpan(span);
  83.             }
  84.             for (int spanIdx=0; spanIdx<span-1; spanIdx++, idx++) {
  85.                 c = row.addCell();
  86.                 ((CTTableCell)c.getXmlObject()).setHMerge(true);
  87.             }
  88.         }
  89.         return row.getCells();
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement