Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active November 11, 2022 04:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/163f03e6cd53fd757db6b4dc041e797c to your computer and use it in GitHub Desktop.
Save aspose-com-gists/163f03e6cd53fd757db6b4dc041e797c to your computer and use it in GitHub Desktop.
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