Hi,
Thank you for your inquiry.
According to the message you provided, I can't reproduce your issue.However, you can add image to the document in bytes using the following two methods.
Method 1:
- Code: Select all
Document document = new Document();
//Load Image Bytes
String imagePath = "data/1.png";
FileInputStream imageStream = new FileInputStream(imagePath);
Section section = document.addSection();
byte[] content = new byte[imageStream.available()];
imageStream.read(content);
//if the content is not empty and add it to the document
if (content != null) {
Paragraph paragraph = section.addParagraph();
paragraph.getFormat().setLeftIndent(50);
DocPicture picture = new DocPicture(document);
picture.loadImage(content);
picture.setWidth(50);
picture.setHeight(90);
picture.setTextWrappingStyle(TextWrappingStyle.Tight);
paragraph.getChildObjects().insert(0,picture);
}
//Save Document
FileOutputStream fileOutputStream = new FileOutputStream("output/document.docx");
document.saveToStream(fileOutputStream, FileFormat.Docx);
//Close Stream
fileOutputStream.close();
imageStream.close();
document.close();
Method 2:
- Code: Select all
Document document = new Document();
//Load Image Bytes
String imagePath = "data/1.png";
FileInputStream imageStream = new FileInputStream(imagePath);
Section section = document.addSection();
byte[] content = new byte[imageStream.available()];
imageStream.read(content);
//if the content is not empty and add it to the document
if (content != null) {
Paragraph paragraph = section.addParagraph();
paragraph.getFormat().setLeftIndent(50);
paragraph.appendPicture(content);
}
//Save Document
FileOutputStream fileOutputStream = new FileOutputStream("output/document1.docx");
document.saveToStream(fileOutputStream, FileFormat.Docx);
//Close Stream
fileOutputStream.close();
imageStream.close();
document.close();
If my code doesn’t help you, please offer your test file and image to help us do further investigation, you can attach here or send it to us via email (
[email protected]). Thank you in advance.
Sincerely,
Ula
E-iceblue support team