Generated.zip
Generated.zip
Generated.zip
Team,
Iam using java spire.doc.free - 5.2.0 software for attaching OLE document to word docx file. The requirement is to attach multiple pdfs , xlsx to word document. i was able to attach and open the first attachment, but iam not able to attach the second document.
Attached the Code and generated document , Please let us know if you require more information from me.Thanks
private byte[] attachOLEObject(String bdsNo, String docType, byte[] docxBytes, OleObjectType oleObjectType) {
byte[] docxBytesWithOLE = new byte[0];
try {
log.info(" inside attachOLEObject");
Optional<FileInfo> fileInfo = fileInfoService.downloadFile(bdsNo, docType);
if (fileInfo.isEmpty()) {
Generated.zip
log.error(docType + " document not found for the bdsNo: " + bdsNo);
ByteArrayInputStream inStream = new ByteArrayInputStream(docxBytes);
Document oleDoc = new Document();
oleDoc.loadFromStream(inStream, FileFormat.Docx);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
oleDoc.saveToFile(outputStream, FileFormat.Docx);
return outputStream.toByteArray();
} else {
log.info(docType + " document found");
ByteArrayInputStream inStream = new ByteArrayInputStream(docxBytes);
Document oleDoc = new Document();
oleDoc.loadFromStream(inStream, FileFormat.Docx);
//Get the last section
Section section = oleDoc.getLastSection();
//Add a paragraph
Paragraph par = section.addParagraph();
//Load an image which will be inserted to Word document representing the embedded file
DocPicture pdfIcon = new DocPicture(oleDoc);
pdfIcon.loadImage(getClass().getClassLoader().getResourceAsStream("icons8-pdf-40.png"));
//Insert a PDF file to the Word document as an OLE object
par.appendOleObject(fileInfo.get().getFilebyteArr(), pdfIcon, oleObjectType).setDisplayAsIcon(true);
Paragraph par2 = section.addParagraph();
par2.appendText(fileInfo.get().getFileName());
ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
oleDoc.saveToFile(outputStream1, FileFormat.Docx);
docxBytesWithOLE = outputStream1.toByteArray();
}
} catch (Exception e) {
log.error(" Error in attaching OLE document " + docType + " " + e.getMessage());
}
return docxBytesWithOLE;
}
Login to view the files attached to this post.