PackageRelationship coreDocumentRelationship = pkg
.getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT)
.getRelationship(0);
// Get core document part from the relationship.
PackagePart coreDocumentPart = pkg.getPart(coreDocumentRelationship);
InputStream inStream = coreDocumentPart.getInputStream();
SAXReader docReader = new SAXReader();
Document doc = docReader.read(inStream);
Namespace namespaceWordProcessingML = new Namespace("w",
"http://schemas.openxmlformats.org/wordprocessingml/2006/main");
Element bodyElement = doc.getRootElement().element(
new QName("body", namespaceWordProcessingML));
// Retrieves paragraph childs from body element
List paragraphs = bodyElement.content();
// Build a new paragraph element
Element paragraph = DocumentHelper.createElement(new QName("p",
namespaceWordProcessingML));
Element run = paragraph.addElement(new QName("r",
namespaceWordProcessingML));
Element text = run
.addElement(new QName("t", namespaceWordProcessingML));
text.setText("New paragraph added with OpenXML4J !");
// Add the newly created paragraph at the last position of paragraph
// elements, just before the w:sectPr element
paragraphs.add(paragraphs.size() - 1, paragraph);
// Save back the content into the part
StreamHelper.saveXmlInStream(doc, coreDocumentPart.getOutputStream());
pkg.save(new File(demoCore.getTestRootPath() + "sample_output.docx"));
}