Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Image to Word Docx using NPOI Library C#

I was already using the NPOI Library for creating Excel Documents(which is the .NET version of POI Java project), but now I also want to create a Word (Docx document).

I want to add an image to the document, but somehow it won't work. It looks like it's adding the image, but when I open the downloaded doc

“The file <document filename> cannot be opened because there are problems with the contents”

this is the code for adding the image in the document:

XWPFParagraph p2 = wordDoc.CreateParagraph();
XWPFRun r2 = p2.CreateRun();
r2.AddPicture(pictureData, (int)PictureType.PNG, "image1", 800, 600);

where pictureData is a memoryStream of the pictureData. the memoryStrean is correct, because I can get the base64 String from it and this shows the correct output, but all I want is this image into a word document.

So my question is, what am I missing, what am I doing wrong? Couldn't find any documentation about images into a XWPF document.

thanks in advance

like image 991
Vincent Hogendoorn Avatar asked Nov 11 '22 08:11

Vincent Hogendoorn


1 Answers

I asked this question also on codeplex.com, where you can download the library, and the coordinator for the NPOI library, tonyqus, answered with the folllowing:

This is a bug in NPOI 2.0.6. It will be fixed in NPOI 2.1

like image 181
Vincent Hogendoorn Avatar answered Nov 14 '22 22:11

Vincent Hogendoorn