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.

Fri Oct 13, 2023 3:54 am

Hi Team ,

I have written code to add image in the Document by passing byte value,facing below mentioned exception.

CODE :
byte[] content=[B@31ff6309
if(null != content) {
paragraph = section.addParagraph();
paragraph.getFormat().setLeftIndent(50);
DocPicture picture = new DocPicture(document);
// picture.loadImage(imagePath);
picture.setWidth(50);
picture.setHeight(90);
picture.setTextWrappingStyle( TextWrappingStyle.Tight);
paragraph.appendPicture(content);
}

EXCEPTION :
Exception in thread "main" java.lang.NullPointerException: Image bytes cannot be null or empty
at com.spire.doc.fields.DocPicture.loadImage(Unknown Source)
at com.spire.doc.documents.Paragraph.appendPicture(Unknown Source).

pr20080798
 
Posts: 159
Joined: Wed Jan 20, 2021 1:15 pm

Fri Oct 13, 2023 7:55 am

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
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Tue Nov 07, 2023 2:21 am

Hello,

Did you test the code on your side? Did it solve your problem?
Please feel free to us if you have any problem.

Best Regards,
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Return to Spire.Doc