Hi Team,
I need to split a doc/docx document according to some page range like 5 to 15 in a document of 50 pages into a new file.
So i need a new file having 10 pages starting from page no 5 content to the 15th page.
I got reply from Aspose words that to follow this link
Java Example to Split Document Pages to Separate Word Documents
But this link is basically for splitting one page per document and that also is not working because in every new file i see all the total pages of original document. However, this is not my requirement.
I want the above requirement to be met. Please provide me the help.
Version of aspose words is 14.8.0.
Regards
Ishan
Hi Ishan,
Thanks for your inquiry. First off, please upgrade to the latest version of Aspose.Words 15.4.0 from the following link as it contains many improvements and bug fixes:
Download Aspose.Words for Java API
Use Aspose.Words for Java API from a Maven based project
In case the problem still remains, please attach your problematic 50 page sample Word document here for testing. We will investigate the issue on our end and provide you more information.
Best regards,
Hello,
PFA the document its not getting split.
Thanks
Hi Ishan,
Thanks for your inquiry. Please find attached example project and try using the following code:
Java Code to Split Word File according to Page Range into a Separate Document
String folderName = "E:\\Temp\\abc\\abc\\";
File[] files = new File(folderName).listFiles();
for (File file : files) {
if (file.isFile()) {
SplitDocumentToPages(file);
}
}
public static void SplitDocumentToPages(File docName) throws Exception {
String folderName = docName.getParent();
String fileName = docName.getName();
String extensionName = fileName.substring(fileName.lastIndexOf("."));
String outFolder = new File(folderName, "Out").getAbsolutePath();
System.out.println("Processing document: " + fileName + extensionName);
Document doc = new Document(docName.getAbsolutePath());
// Create and attach collector to the document before page layout is built.
LayoutCollector layoutCollector = new LayoutCollector(doc);
// This will build layout model and collect necessary information.
doc.updatePageLayout();
// Split nodes in the document into separate pages.
DocumentPageSplitter splitter = new DocumentPageSplitter(layoutCollector);
// Save each page to the disk as a separate document.
for (int page = 1; page <= doc.getPageCount(); page++) {
Document pageDoc = splitter.getDocumentOfPage(page);
pageDoc.save("E:\\Temp\\out_" + page + ".doc");
}
// Detach the collector from the document.
layoutCollector.setDocument(null);
}
You can specify page range that you want to create individual documents of in the for loop. After that you can concatenate these documents using Document.AppendDocument method. I hope, this helps.
Best regards,
Hi,
I have tried this code already. It doesn’t work at all. IT gives the output as same file as original with same no of pages not the original pages
Hi Ishan,
Thanks for your inquiry. Your document ‘100kb.doc’ has 6 pages in it and when running the code shared in my previous post using Aspose.Words for Java 15.4.0, it generated 6 different documents (see attached .zip file). Are you using 15.4.0 on your end? Please upgrade to the latest version. I hope, this helps.
Best regards,