Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Aug 29, 2024 6:02 pm

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;
}

sundaresan
 
Posts: 4
Joined: Fri Jul 19, 2024 10:53 pm

Fri Aug 30, 2024 7:16 am

Hi,

Thank you for your inquiry.I made some adjustments based on your code and tested it, and found that when using (Spire.Doc.free -5.2.0), only the first OLE document can be added and opened, which is consistent with your feedback. Subsequently, I tested our commercial version (Spire.Doc for Java-12.8.4) and found that it could add and open multiple OLE documents normally. Sorry that we only upgrade our free version irregularly, so we recommend using our commercial version for testing, which includes more fixes and optimizations.
At the same time, I have attached my test code and test result documents on different versions for you to review.If you have any question, please feel free to contact us.
Code: Select all
public static void main(String[] args) throws IOException {
        String filePath = "Generated.docx";
        //Load the document into memory
        InputStream inputStream = new FileInputStream(filePath);
        byte[] fileBytes;
        try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {
            int nRead;
            byte[] data = new byte[1024];
            while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, nRead);
            }
            fileBytes = buffer.toByteArray();
        } finally {
            inputStream.close();
        }
        //Attach multiple OLE documents
        byte[] docxBytes1 = attachOLEObject("page.pdf", fileBytes, "pdf","pdf_icon.png");
        byte[] docxBytes2 = attachOLEObject("lists.pdf", docxBytes1, "pdf","pdf_icon.png");
        byte[] docxBytes3 = attachOLEObject("in.xlsx", docxBytes2, "xlsx","excel_icon.png");
        byte[] docxBytes4 = attachOLEObject("in.xlsx", docxBytes3, "xlsx","excel_icon.png");
        //Save to local
        ByteArrayInputStream inStream = new ByteArrayInputStream(docxBytes4);
        Document oleDoc = new Document();
        oleDoc.loadFromStream(inStream, FileFormat.Docx);
        oleDoc.saveToFile("Generated_business.docx", FileFormat.Docx);
    }

    private static byte[] attachOLEObject(String olePath, byte[] docxBytes, String oleObjectType,String iconPath) {
        byte[] docxBytesWithOLE = new byte[0];
        try {
            {
                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(iconPath);
                //Insert a PDF file to the Word document as an OLE object
                InputStream objStream = new FileInputStream(olePath);
                par.appendOleObject(objStream, pdfIcon, oleObjectType).setDisplayAsIcon(true);
                Paragraph par2 = section.addParagraph();
                par2.appendText("ole object");
                ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
                oleDoc.saveToFile(outputStream1, FileFormat.Docx);
                docxBytesWithOLE = outputStream1.toByteArray();
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return docxBytesWithOLE;
    }

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 164
Joined: Mon Jul 15, 2024 5:40 am

Return to Spire.Doc

cron