Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Split Word Documents using Java
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Open a Word document
Document doc = new Document("Word.docx");
// Split nodes in the document into separate pages
DocumentPageSplitter splitter = new DocumentPageSplitter(doc);
// Get part of the document
Document pageDoc = splitter.getDocumentOfPageRange(3,6);
pageDoc.save("SplitDocumentByPageRange.docx");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Open a Word document
Document doc = new Document("Word.docx");
// Split nodes in the document into separate pages
DocumentPageSplitter splitter = new DocumentPageSplitter(doc);
// Save each page as a separate document
for (int page = 1; page <= doc.getPageCount(); page++)
{
Document pageDoc = splitter.getDocumentOfPage(page);
pageDoc.save("SplitDocumentByPage_" + page + ".docx");
}
// Load a Word DOCX document
Document doc = new Document("word.docx");
for (int i = 0; i < doc.getSections().getCount(); i++) {
// Split a document into smaller parts, in this instance split by section
Section section = doc.getSections().get(i).deepClone();
// Create a new document
Document newDoc = new Document();
newDoc.getSections().clear();
// Add section
Section newSection = (Section) newDoc.importNode(section, true);
newDoc.getSections().add(newSection);
// Save each section as a separate document
newDoc.save("splitted_" + i + ".docx");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment