/** * 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; }
/** * 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; }
/** * 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; }
/** * Replace content of table in array tables at position pos with a * * @param pos * @param table */ public void setTable(int pos, XWPFTable table) { tables.set(pos, table); ctDocument.getBody().setTblArray(pos, table.getCTTbl()); }
/** * copies content of a paragraph to a existing paragraph in the list paragraphs at position pos * * @param paragraph * @param pos */ public void setParagraph(XWPFParagraph paragraph, int pos) { paragraphs.set(pos, paragraph); ctDocument.getBody().setPArray(pos, paragraph.getCTP()); /* TODO update body element, update xwpf element, verify that * incoming paragraph belongs to this document or if not, XML was * copied properly (namespace-abbreviations, etc.) */ }
/** * 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); }
/** * 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; }
/** * */ 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); } } } }
/** * 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()); }
/** * verifies that cursor is on the right position * * @param cursor */ private boolean isCursorInBody(XmlCursor cursor) { XmlCursor verify = cursor.newCursor(); verify.toParent(); boolean result = (verify.getObject() == this.ctDocument.getBody()); verify.dispose(); return result; }
private void setFooterReference(Enum type, XWPFHeaderFooter wrapper) { CTHdrFtrRef ref = doc.getDocument().getBody().getSectPr().addNewFooterReference(); ref.setType(type); ref.setId(doc.getRelationId(wrapper)); }
private void setHeaderReference(Enum type, XWPFHeaderFooter wrapper) { CTHdrFtrRef ref = doc.getDocument().getBody().getSectPr().addNewHeaderReference(); ref.setType(type); ref.setId(doc.getRelationId(wrapper)); }
if (doc.getDocument().getBody().getSectPr() != null) { headerFooterPolicy = new XWPFHeaderFooterPolicy(this);
CTBody ctBody = doc.getDocument().getBody(); sectPr = ctBody.isSetSectPr() ? ctBody.getSectPr()
/** * 文档合并 * @param docMerge 待合并文档 * @return 合并后的文档 * @throws Exception * @since 1.3.0 */ public NiceXWPFDocument merge(NiceXWPFDocument docMerge) throws Exception{ if (null == docMerge) return this; CTBody body = this.getDocument().getBody(); String srcString = body.xmlText(); String prefix = srcString.substring(0,srcString.indexOf(">")+1); String mainPart = srcString.substring(srcString.indexOf(">")+1,srcString.lastIndexOf("<")); String sufix = srcString.substring( srcString.lastIndexOf("<") ); String addPart = extractMergePart(docMerge); CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + sufix); body.set(makeBody); return generate(); }
private String extractMergePart(NiceXWPFDocument docMerge) throws InvalidFormatException { CTBody bodyMerge = docMerge.getDocument().getBody(); Map<String, String> styleIdsMap = mergeStyles(docMerge); Map<BigInteger, BigInteger> numIdsMap = mergeNumbering(docMerge);
CTP ctp = paragraph.getCTP(); CTBody body = this.getDocument().getBody(); String srcString = body.xmlText(); String prefix = srcString.substring(0, srcString.indexOf(">") + 1);
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);
XWPFDocument document = new XWPFDocument(); try{ CTSectPr sectpr = document.getDocument().getBody().addNewSectPr(); if(!sectpr.isSetPgSz()) { sectpr.addNewPgSz();
/** * Replace content of table in array tables at position pos with a * * @param pos * @param table */ public void setTable(int pos, XWPFTable table) { tables.set(pos, table); ctDocument.getBody().setTblArray(pos, table.getCTTbl()); }