Hi there
I am testing splitting Word files by pages with Aspose Word 17.2.0 and PageSplitter.
Here is the test code:
@Test
public void splitTest() throws Exception{
String fileName = "MAYJIANG_HAWAY_Improvement 2011.03.01.doc";
String wordPassword = "";
byte[] wordContent = IOUtils.toByteArray(new FileInputStream("custom/input/docx/"+fileName));
splitMethod(wordContent, wordPassword, fileName);
}
protected void splitMethod(byte[] wordContent, String wordPassword, String fileName) throws Exception {
Document wordDoc = null;
try {
if (StringUtil.isNotEmpty(wordPassword)) {
LoadOptions loadOps = new LoadOptions(wordPassword);
wordDoc = new Document(new ByteArrayInputStream(wordContent), loadOps);
} else {
wordDoc = new Document(new ByteArrayInputStream(wordContent));
}
String ext = FilenameUtils.getExtension(fileName);
LayoutCollector layoutCollector = new LayoutCollector(wordDoc);
wordDoc.updatePageLayout();
DocumentPageSplitter splitter = new DocumentPageSplitter(layoutCollector);
int totalPage = wordDoc.getPageCount();
System.out.println("totalPage:" + totalPage);
int index = 0;
while (index < totalPage) {
Document pageDoc = null;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
File outputSinglePageWordFile = new File(
"custom/input/docx/" + "split_" + fileName + "/" + (index + 1) + "." + ext);
FileUtil.md(outputSinglePageWordFile.getParentFile());
try {
pageDoc = splitter.getDocumentOfPage((index + 1));
pageDoc.save(stream, SaveFormat.fromName(ext.toUpperCase()));
} finally {
IOUtils.closeQuietly(stream);
}
IOUtils.write(stream.toByteArray(), new FileOutputStream(outputSinglePageWordFile));
index++;
}
} finally {
}
}
In the result #1, opened with MS Word, you can see there is one more redundant empty page.
I have uploaded the origin Word file and the result.
Please check the attachment, and help us solve this issue, thanks~
Craig