/** * Return the {@link CTSectPr} object that corresponds with the * last section in this document. * * @return {@link CTSectPr} object */ private CTSectPr getSection() { CTBody ctBody = getDocument().getBody(); return (ctBody.isSetSectPr() ? ctBody.getSectPr() : ctBody.addNewSectPr()); }
/** * Appends a new paragraph to this document * * @return a new paragraph */ public XWPFParagraph createParagraph() { XWPFParagraph p = new XWPFParagraph(ctDocument.getBody().addNewP(), this); bodyElements.add(p); paragraphs.add(p); return p; }
/** * */ public void createTOC() { CTSdtBlock block = this.getDocument().getBody().addNewSdt(); TOC toc = new TOC(block); for (XWPFParagraph par : paragraphs) { String parStyle = par.getStyle(); if (parStyle != null && parStyle.startsWith("Heading")) { try { int level = Integer.parseInt(parStyle.substring("Heading".length())); toc.addRow(level, par.getText(), 1, "112723803"); } catch (NumberFormatException e) { LOG.log(POILogger.ERROR, "can't format number in TOC heading", e); } } } }
/** * remove a BodyElement from bodyElements array list * * @param pos * @return true if removing was successfully, else return false */ public boolean removeBodyElement(int pos) { if (pos >= 0 && pos < bodyElements.size()) { BodyElementType type = bodyElements.get(pos).getElementType(); if (type == BodyElementType.TABLE) { int tablePos = getTablePos(pos); tables.remove(tablePos); ctDocument.getBody().removeTbl(tablePos); } if (type == BodyElementType.PARAGRAPH) { int paraPos = getParagraphPos(pos); paragraphs.remove(paraPos); ctDocument.getBody().removeP(paraPos); } bodyElements.remove(pos); return true; } return false; }
private void setFooterReference(Enum type, XWPFHeaderFooter wrapper) { CTHdrFtrRef ref = doc.getDocument().getBody().getSectPr().addNewFooterReference(); ref.setType(type); ref.setId(doc.getRelationId(wrapper)); }
private XWPFParagraph getWatermarkParagraph(String text, int idx) { CTP p = CTP.Factory.newInstance(); byte[] rsidr = doc.getDocument().getBody().getPArray(0).getRsidR(); byte[] rsidrdefault = doc.getDocument().getBody().getPArray(0).getRsidRDefault(); p.setRsidP(rsidr); p.setRsidRDefault(rsidrdefault);
/** * Create an empty table with one row and one column as default. * * @return a new table */ public XWPFTable createTable() { XWPFTable table = new XWPFTable(ctDocument.getBody().addNewTbl(), this); bodyElements.add(table); tables.add(table); return table; }
XWPFDocument document = new XWPFDocument(); try{ CTSectPr sectpr = document.getDocument().getBody().addNewSectPr(); if(!sectpr.isSetPgSz()) { sectpr.addNewPgSz();
/** * inserts an existing XWPFTable to the arrays bodyElements and tables * @param pos * @param table */ public void insertTable(int pos, XWPFTable table) { bodyElements.add(pos, table); int i; for (i = 0; i < ctDocument.getBody().getTblList().size(); i++) { CTTbl tbl = ctDocument.getBody().getTblArray(i); if (tbl == table.getCTTbl()) { break; } } tables.add(i, table); }
/** * inserts an existing XWPFTable to the arrays bodyElements and tables * * @param pos * @param table */ @Override public void insertTable(int pos, XWPFTable table) { bodyElements.add(pos, table); int i = 0; for (CTTbl tbl : ctDocument.getBody().getTblArray()) { if (tbl == table.getCTTbl()) { break; } i++; } tables.add(i, table); }
private void compute( CTDocument1 document ) throws Exception { XmlCursor cursor = document.getBody().newCursor(); cursor.selectPath( "./*" ); while ( cursor.toNextSelection() ) { XmlObject o = cursor.getObject(); if ( o instanceof CTP ) { CTP paragraph = (CTP) o; CTSectPr sectPr = getSectPr( paragraph ); if ( sectPr != null ) { addSection( sectPr, true ); } } } /* * for ( IBodyElement bodyElement : document.getBodyElements() ) { if ( bodyElement.getElementType() == * BodyElementType.PARAGRAPH ) { XWPFParagraph paragraph = (XWPFParagraph) bodyElement; CTSectPr sectPr = * getSectPr( paragraph ); if ( sectPr != null ) { addSection( sectPr, true ); } } } */ addSection( bodySectPr, false ); }
private void setHeaderReference(Enum type, XWPFHeaderFooter wrapper) { CTHdrFtrRef ref = doc.getDocument().getBody().getSectPr().addNewHeaderReference(); ref.setType(type); ref.setId(doc.getRelationId(wrapper)); }
} else { int newPIdx = doc.getParagraphs().indexOf(refParagraph) + 1; CTP[] pList = doc.getDocument().getBody().getPArray(); newPIdx = Math.min(newPIdx, pList.length);
/** * remove a BodyElement from bodyElements array list * @param pos * @return true if removing was successfully, else return false */ public boolean removeBodyElement(int pos){ if(pos >= 0 && pos < bodyElements.size()) { BodyElementType type = bodyElements.get(pos).getElementType(); if(type == BodyElementType.TABLE){ int tablePos = getTablePos(pos); tables.remove(tablePos); ctDocument.getBody().removeTbl(tablePos); } if(type == BodyElementType.PARAGRAPH){ int paraPos = getParagraphPos(pos); paragraphs.remove(paraPos); ctDocument.getBody().removeP(paraPos); } bodyElements.remove(pos); return true; } return false; }
/** * Create an empty table with a number of rows and cols specified * * @param rows * @param cols * @return table */ public XWPFTable createTable(int rows, int cols) { XWPFTable table = new XWPFTable(ctDocument.getBody().addNewTbl(), this, rows, cols); bodyElements.add(table); tables.add(table); return table; }
private void changeOrientation(XWPFDocument document, String orientation){ CTDocument1 doc = document.getDocument(); CTBody body = doc.getBody(); CTSectPr section = body.addNewSectPr(); XWPFParagraph para = document.createParagraph(); CTP ctp = para.getCTP(); CTPPr br = ctp.addNewPPr(); br.setSectPr(section); CTPageSz pageSize = section.getPgSz(); if(orientation.equals("landscape")){ pageSize.setOrient(STPageOrientation.LANDSCAPE); pageSize.setW(BigInteger.valueOf(842 * 20)); pageSize.setH(BigInteger.valueOf(595 * 20)); } else{ pageSize.setOrient(STPageOrientation.PORTRAIT); pageSize.setH(BigInteger.valueOf(842 * 20)); pageSize.setW(BigInteger.valueOf(595 * 20)); } }
/** * inserts an existing XWPFTable to the arrays bodyElements and tables * * @param pos * @param table */ @Override public void insertTable(int pos, XWPFTable table) { bodyElements.add(pos, table); int i = 0; for (CTTbl tbl : ctDocument.getBody().getTblArray()) { if (tbl == table.getCTTbl()) { break; } i++; } tables.add(i, table); }
private void compute( CTDocument1 document ) throws Exception { XmlCursor cursor = document.getBody().newCursor(); cursor.selectPath( "./*" ); while ( cursor.toNextSelection() ) { XmlObject o = cursor.getObject(); if ( o instanceof CTP ) { CTP paragraph = (CTP) o; CTSectPr sectPr = getSectPr( paragraph ); if ( sectPr != null ) { addSection( sectPr, true ); } } } /* * for ( IBodyElement bodyElement : document.getBodyElements() ) { if ( bodyElement.getElementType() == * BodyElementType.PARAGRAPH ) { XWPFParagraph paragraph = (XWPFParagraph) bodyElement; CTSectPr sectPr = * getSectPr( paragraph ); if ( sectPr != null ) { addSection( sectPr, true ); } } } */ addSection( bodySectPr, false ); }
sectPr = ctBody.isSetSectPr() ? ctBody.getSectPr() : ctBody.addNewSectPr();