Error while inserting Image in document (DocumentBuilder.InsertImage)

Hi,

We are upgrading Aspose.Words.dll to 15.5.0.0 from 14.4.0.0

To resolve hyperlink issue which is suggested in below post
https://forum.aspose.com/t/43239


after upgrade hyperlinks are working fine and navigating to the embedded URL

But now InsertImage is giving error which was working fine with version 14.4.0.0

Aspose.Words.Drawing.Shape shape = new Aspose.Words.Drawing.Shape(document, ShapeType.Image);
shape.ImageData.SourceFullName = @"D:\googlelogo.png";
shape.ImageData.SetImage(@"D:\googlelogo.png");

byte[] imgBytes = shape.ImageData.ImageBytes;
MemoryStream ms = new MemoryStream(imgBytes);
Image sourceImage = Image.FromStream(ms);
docBuilder.InsertImage(sourceImage);

InsertImage call is throwing exception

An unhandled exception of type 'System.NullReferenceException' occurred in Aspose.Words.dll
Additional information: Object reference not set to an instance of an object.

Any suggestion to resolve this issue?

Thanks,
Deepak Dhirhe

Hi Deepak,

Thanks for your inquiry. Firstly, I suggest you please upgrade to the latest version of Aspose.Words for .NET 15.9.0 from here:
http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/entry661987.aspx

Secondly, I suggest you please move the cursor to the desired location where you want to insert the image and then use DocumentBuilder.InsertImage to insert the image. Hope this helps you. Please let us know if you have any more queries.


Hi Tahir,

Thanks for response. I updated with the latest 15.9.0.0 Aspose Words dll but issue is still there. Also i am already moving cursor to run inside the cell.

Here is the tester sample code

Aspose.Words.License cellsLicense = new Aspose.Words.License();
cellsLicense.SetLicense(System.AppDomain.CurrentDomain.BaseDirectory + "Aspose.Total.lic");
Document document = new Document();

Body body = new Body(document);
Section section = new Section(document);
section.AppendChild(body);
document.AppendChild(section);

DocumentBuilder docBuilder = new DocumentBuilder(document);
Table table = new Table(document);
table.SetBorder(BorderType.None, LineStyle.Double, 2, Color.Black, false);
Row row = new Row(document);
row.RowFormat.Borders.LineStyle = LineStyle.None;
row.RowFormat.HeightRule = HeightRule.Exactly;

//First Cell of the row
Run run1 = new Run(document);
Cell cell1 = new Cell(document);
Paragraph para1 = new Paragraph(document);
cell1.AppendChild(para1);
cell1.FirstParagraph.AppendChild(run1);
row.Cells.Add(cell1);
docBuilder.MoveTo(run1);
docBuilder.InsertHtml("Google Link");

//Second Cell of the row
Run run2 = new Run(document);
Cell cell2 = new Cell(document);
Paragraph para2 = new Paragraph(document);
cell2.AppendChild(para2);
cell2.FirstParagraph.AppendChild(run2);
row.Cells.Add(cell2);
docBuilder.MoveTo(run2);

Aspose.Words.Drawing.Shape shape = new Aspose.Words.Drawing.Shape(document, ShapeType.Image);
shape.ImageData.SourceFullName = @"D:\googlelogo.png";
shape.ImageData.SetImage(@"D:\googlelogo.png");
byte[] imgBytes = shape.ImageData.ImageBytes;
MemoryStream ms = new MemoryStream(imgBytes);
Image sourceImage = Image.FromStream(ms);
docBuilder.InsertImage(sourceImage);

table.Rows.Add(row);
body.AppendChild(table);
document.Save(@"D:\AsposeLinkImageTester.doc");


Please suggest if we need to do any changes. (Above code was working fine with 14.4.0).

Thanks in advance.
Deepak Dhirhe

Hi Tahir,


I also discovered that adding row to the table before inserting image resolves the issue. (As suggested by you in second snippet).

Thank you very much…

Hi Deepak,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.