/** * 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 a new CTWorkbook with all values set to default */ @Override protected void onDocumentCreate() { ctDocument = CTDocument1.Factory.newInstance(); ctDocument.addNewBody(); settings = (XWPFSettings) createRelationship(XWPFRelation.SETTINGS, XWPFFactory.getInstance()); POIXMLProperties.ExtendedProperties expProps = getProperties().getExtendedProperties(); expProps.getUnderlyingProperties().setApplication(DOCUMENT_CREATOR); }
/** * commit and saves the document */ @Override protected void commit() throws IOException { XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS); xmlOptions.setSaveSyntheticDocumentElement(new QName(CTDocument1.type.getName().getNamespaceURI(), "document")); PackagePart part = getPackagePart(); OutputStream out = part.getOutputStream(); ctDocument.save(out, xmlOptions); out.close(); }
/** * 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; }
/** * commit and saves the document */ @Override protected void commit() throws IOException { XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS); xmlOptions.setSaveSyntheticDocumentElement(new QName(CTDocument1.type.getName().getNamespaceURI(), "document")); Map<String, String> map = new HashMap<String, String>(); map.put("http://schemas.openxmlformats.org/officeDocument/2006/math", "m"); map.put("urn:schemas-microsoft-com:office:office", "o"); map.put("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "r"); map.put("urn:schemas-microsoft-com:vml", "v"); map.put("http://schemas.openxmlformats.org/markup-compatibility/2006", "ve"); map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w"); map.put("urn:schemas-microsoft-com:office:word", "w10"); map.put("http://schemas.microsoft.com/office/word/2006/wordml", "wne"); map.put("http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", "wp"); xmlOptions.setSaveSuggestedPrefixes(map); PackagePart part = getPackagePart(); OutputStream out = part.getOutputStream(); ctDocument.save(out, xmlOptions); out.close(); }
/** * Create a new CTWorkbook with all values set to default */ @Override protected void onDocumentCreate() { ctDocument = CTDocument1.Factory.newInstance(); ctDocument.addNewBody(); settings = (XWPFSettings) createRelationship(XWPFRelation.SETTINGS, XWPFFactory.getInstance()); POIXMLProperties.ExtendedProperties expProps = getProperties().getExtendedProperties(); expProps.getUnderlyingProperties().setApplication(DOCUMENT_CREATOR); }
/** * 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; }
/** * commit and saves the document */ @Override protected void commit() throws IOException { XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS); xmlOptions.setSaveSyntheticDocumentElement(new QName(CTDocument1.type.getName().getNamespaceURI(), "document")); PackagePart part = getPackagePart(); OutputStream out = part.getOutputStream(); ctDocument.save(out, xmlOptions); out.close(); }
/** * Create a new CTWorkbook with all values set to default */ @Override protected void onDocumentCreate() { ctDocument = CTDocument1.Factory.newInstance(); ctDocument.addNewBody(); settings = (XWPFSettings) createRelationship(XWPFRelation.SETTINGS,XWPFFactory.getInstance()); POIXMLProperties.ExtendedProperties expProps = getProperties().getExtendedProperties(); expProps.getUnderlyingProperties().setApplication(DOCUMENT_CREATOR); }
/** * 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)); }