/** * add a new paragraph at position of the cursor * * @param cursor * @return the inserted paragraph */ public XWPFParagraph insertNewParagraph(XmlCursor cursor) { if (isCursorInHdrF(cursor)) { String uri = CTP.type.getName().getNamespaceURI(); String localPart = "p"; cursor.beginElement(localPart, uri); cursor.toParent(); CTP p = (CTP) cursor.getObject(); XWPFParagraph newP = new XWPFParagraph(p, this); XmlObject o = null; while (!(o instanceof CTP) && (cursor.toPrevSibling())) { o = cursor.getObject(); } if ((!(o instanceof CTP)) || (CTP) o == p) { paragraphs.add(0, newP); } else { int pos = paragraphs.indexOf(getParagraph((CTP) o)) + 1; paragraphs.add(pos, newP); } int i = 0; cursor.toCursor(p.newCursor()); while (cursor.toPrevSibling()) { o = cursor.getObject(); if (o instanceof CTP || o instanceof CTTbl) i++; } bodyElements.add(i, newP); cursor.toCursor(p.newCursor()); cursor.toEndToken(); return newP; } return null; }
public void testGetText() { CTTbl table = CTTbl.Factory.newInstance(); CTRow row = table.addNewTr(); CTTc cell = row.addNewTc(); CTP paragraph = cell.addNewP(); CTR run = paragraph.addNewR(); CTText text = run.addNewT(); text.setStringValue("finally I can write!"); XWPFTable xtab = new XWPFTable(null, table); assertEquals("finally I can write!\n", xtab.getText()); }