Hello.
We are currently evaluating Aspose Total in order to directly convert Excel and Powerpoint directly to Word and then merge/append the resulting documents with other Word files so that we can a single word document for further modification. So our goal is to read a given list of files of different types (Word, Excel and Powerpoint) and end up with a single word document with all their content.
To do this operation we create a Document and then a DocumentBuilder:
Document wordDocument = new Document();
DocumentBuilder wordBuilder = new DocumentBuilder(wordDocument);
Then for each file we call the respective method to load, convert to Word if necessary and save the result to a temporary ByteArrayOutputStream. We then use the method insertDocument of the wordBuilder to “insert” the content in the stream:
// In case of excel file
var workbook = new Workbook(new ByteArrayInputStream(file.getBytes()));
// For each sheet set up the page orientation and the cells size
for (int i = 0; i < workbook.getWorksheets().getCount(); i++)
{
workbook.getWorksheets().get(i).getPageSetup().setOrientation(PageOrientationType.LANDSCAPE);
workbook.getWorksheets().get(i).getPageSetup().setFitToPagesWide(1);
workbook.getWorksheets().get(i).getPageSetup().setFitToPagesTall(0);
}
// Create the output stream to save the Excel conversion to word
ByteArrayOutputStream conversionStream = new ByteArrayOutputStream();
workbook.save(conversionStream, com.aspose.cells.SaveFormat.DOCX);
var docAttachment = new Document(new ByteArrayInputStream(conversionStream.toByteArray()));
List<Table> tables = Arrays.stream(docAttachment.getChildNodes(NodeType.TABLE, true).toArray())
.filter(Table.class::isInstance)
.map(Table.class::cast)
.collect(Collectors.toList());
for (Table table: tables) {
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);
}
wordBuilder.insertDocument(docAttachment, ImportFormatMode.KEEP_SOURCE_FORMATTING);
conversionStream.close();
For PowerPoint:
Presentation pres = new Presentation(new ByteArrayInputStream(file.getBytes()));
wordBuilder.getPageSetup().setOrientation(Orientation.LANDSCAPE);
try
{
for (ISlide slide : pres.getSlides())
{
// generates and inserts slide image
BufferedImage bitmap = slide.getThumbnail(1, 1);
wordBuilder.insertImage(bitmap);
if (slide.getSlideNumber() != pres.getSlides().size() - 1)
{
wordBuilder.insertBreak(BreakType.PAGE_BREAK);
}
}
}
finally
{
if (pres != null) pres.dispose();
}
For Word:
var docAttachment = new Document(new ByteArrayInputStream(file.getBytes()));
wordBuilder.insertDocument(docAttachment, ImportFormatMode.KEEP_SOURCE_FORMATTING);
Once all the files are processed we save the Document to a ByteArrayOutputStream and return it to be then downloaded:
ByteArrayOutputStream wordDocumentOutStream = new ByteArrayOutputStream();
wordDocument.save(wordDocumentOutStream, SaveFormat.DOCX);
wordDocumentOutStream.close();
return wordDocumentOutStream;
As the code above demonstrates, for Excel and Powerpoint we want to end up with the corresponding pages in a landscape layout, also we expect to preserve the styles of each document independently, without affecting the others.
Unfortunately the results are not consistent and vary with the order in which each type of file is processed. We share here the sample documents we used SampleFiles.zip (831.5 KB)
(one file for each type) and the results achieved:
- Word first, then PPT then XLS: The content of the Word file is broken with some empty pages in between the original pages and with the images overflowing. Also the orientation should be set in PORTRAIT mode and after the second page it is in LANDSCAPE mode. For the PPT and XLS the orientation is correct, but they are now also displaying the footer from the merged Word file, which is not what we desire. Here is the final word: _MergedDocuments_WordPPTXLS.docx (1.1 MB)
*XLS first, then PPT then Word: In this case all pages display the footer from the merged Word (although the word is inserted correctly this time), however neither XLS or PPT have orientation in LANDSCAPE._MergedDocuments_XLSPPTWord.docx (1.1 MB) - Other orders produce results similar to the previous points above.
Could you please explain why this is happening?
Alternatively, we tried creating a document for each conversion and then use wordDocument.append(convertedDoc) instead of the wordBuilder.insertDocument. With this approach the results were more consistent, although the footer from the original Word is still set to all pages and there is always an empty page between documents.
Thank you for your help.