We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Error when adding an image from a memorystream

When I save the pdf I get this error: Invalid image or template handle in PlaceImage method.



I use code like this to add an image to a table cell in a header:

// I have the contents of a jpg file in a byte[] array, then I do this:

MemoryStream ms = new MemoryStream(fileBytes);

imageht.ImageInfo.ImageFileType = ImageFileType.Jpeg;

imageht.ImageInfo.ImageStream = ms;

currentCellht.Paragraphs.Add(imageht);

imageht.ImageInfo.Alignment = AlignmentType.Left;

imageht.Margin.Left = 0;

imageht.Margin.Top = 10;



Later when I save the pdf I get that error.

Hi,

Please try using the following code snippet, I hope it would help you out with your query. I am not sure about the image source (input for Byte array) but in the following code I've used FileStream to read the image file and pass the contents to Byte array, which is then converted into MemoryStream and added to the table cell being displayed in header section of the Pdf file.

[C#]

//add header to section
Pdf pdf = new Pdf();
Aspose.Pdf.Section section = new Aspose.Pdf.Section(pdf);
pdf.Sections.Add(section);

Aspose.Pdf.HeaderFooter hf1 = new Aspose.Pdf.HeaderFooter(section);
section.OddHeader = section.EvenHeader = hf1;
//construct the table

Aspose.Pdf.Table table1 = new Aspose.Pdf.Table();
table1.ColumnWidths = "170cm";

FileStream fs = File.OpenRead(@"d:\pdftest\Aspose.jpg");
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
MemoryStream ms = new MemoryStream(data);
Aspose.Pdf.Image imageht = new Aspose.Pdf.Image(section);
imageht.ImageInfo.ImageFileType = ImageFileType.Jpeg;
imageht.ImageInfo.ImageStream = ms;

Text text1 = new Text();
text1.Segments.Add("This image is from MemoryStream");
Segment seg2 = text1.Segments.Add();
//indicate seg2's InlineParagraph is a image.
seg2.InlineParagraph = imageht;

//Create rows in the table and then cells in the rows
Aspose.Pdf.Row row1 = table1.Rows.Add();
Aspose.Pdf.Cell cell1 = row1.Cells.Add();
//Add the text paragraph to the table
row1.Cells[0].Paragraphs.Add(text1);

//add the table to the header
hf1.Paragraphs.Add(table1);
pdf.Save(@"d:/pdftest/HeaderFooter_withTable.pdf");
ms.Close();

In case you still face any problem, please feel free to share.

Thanks for your quick reply. I have since found I get that error only when I save to page.response, like this:

pdf.Save(“test1.pdf”, Aspose.Pdf.SaveType.OpenInAcrobat,Page.Response);

I don’t get the error when I save to a file. So it seems the error is associated with trying to open directly in acrobat via a response object.

Hi,

I am not sure why you are facing this problem, because when I've tested the above specified code while using

pdf.Save("output.pdf", Aspose.Pdf.SaveType.OpenInBrowser, Response); or

pdf.Save("output.pdf", Aspose.Pdf.SaveType.OpenInAcrobat, Response); the Pdf file is being generated correctly.

Please share the project or code snippet that you are using so that we can test the issue at our end.,

I received the error 3 times today. I believe it only happens with particular files.

My script actually saves the file in one place and sends it to the browser a few lines down.

This works: pdf.Save(“d:\output.pdf”)
This sometimes errors: pdf.Save(“output.pdf”, SaveType.OpenInBrowser, Response)

I received the error 3 times today. I believe it only happens with particular files (possibly files with images).

My script actually saves the file in one place and sends it to the browser a few lines down.

This works: pdf.Save(“d:\output.pdf”)
This sometimes errors: pdf.Save(“output.pdf”, SaveType.OpenInBrowser, Response)

Hi,

We checked the error message in the code. It seems this error has nothing to do with web browser. Please try saving the PDF into a memory stream and then send the memory stream to browser by yourself. Please let us know if it works.

It looks like I will have to do that workaround. Save the file first then stream it out. It would be nice if the savetype.openinbrowser,response feature worked properly, though.

If this works:
pdf.Save("d:\output.pdf")

Then this should work:
pdf.Save("output.pdf", SaveType.OpenInBrowser, Response)

System.ArgumentException: Invalid image or template handle in PlaceImage method.

Generated: Tue, 13 Oct 2009 16:56:25 GMT

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.ArgumentException: Invalid image or template handle in PlaceImage method.
   at Aspose.Pdf.Figure.؛.ط(Int32 ؏, Single[] ظ)
   at Aspose.Pdf.Xml.ᒩ.ᒬ(Pdf Ә, ק Ԧ, Image ؞)
   at Aspose.Pdf.Xml.᜗.᜚(ק Ԧ, Pdf Ә, Row ผ, Cell ල, ޸ ޫ)
   at Aspose.Pdf.Xml.ᜌ.ᜐ(ק Ԧ, Pdf Ә, Row ผ, ޸ ޫ)
   at Aspose.Pdf.Xml.ᘹ.ᘽ(ק Ԧ, Pdf Ә, Table ठ, ޸ ޫ)
   at Aspose.Pdf.Xml.ᒱ.ᒳ(ק Ԧ, Pdf Ә, ޸ ޫ, Boolean ᒴ)
   at Aspose.Pdf.Xml.᪢.᪣(ק Ԧ, Pdf Ә)
   at Aspose.Pdf.Xml.᧭.᧲(ק Ԧ, Pdf Ә)

Hi,

It is difficult for us to find the reason since we can’t reproduce this error. We need your help. Can you please tell me if the workaround really work for you? Do you get this error if you save the PDF into MemoryStream?

I get the same error. My steps include:

  • Reading an image from Amazon’s S3 which returns a stream
    • Covert stream into an system image with System.Drawing.Image.FromStream()
  • Later, convert the system image into an memory stream with System.Drawing.Image.Save()
  • Create an Aspose.Pdf.Image and Paragraph.Add() it to my project
    • Assign the memory stream of the system image to Aspose.Pdf.Image.ImageInfo.ImageStream
    • Assign the Aspose.Pdf.Image.ImageInfo.ImageFileType to png
  • Aspose.Pdf.Pdf.Save() my project to another MemoryStream which causes the error:
System.ArgumentException occurred
Message=“Invalid image or template handle in PlaceImage method.”
Source=“Aspose.Pdf”
StackTrace:
at Aspose.Pdf.Figure…(Int32 , Single[] )
at Aspose.Pdf.Xml…(Pdf , , Image )
at Aspose.Pdf.Xml…( , Pdf , Row , Cell , )
at Aspose.Pdf.Xml…( , Pdf , Row , )
at Aspose.Pdf.Xml…( , Pdf , Table , )
at Aspose.Pdf.Xml…( , Pdf , , Boolean )
at Aspose.Pdf.Xml…( , Pdf )
at Aspose.Pdf.Xml…( , Pdf )
at Aspose.Pdf.Pdf.Save(Stream stream)

I also have the same result that it works perfectly fine if I do not Pdf.Save() to a stream.

I just found a work around so we do not need to save the file on the system.

MemoryStream outputStream = new MemoryStream();

//get the buffer
using (MemoryStream ms = new MemoryStream(pdf.GetBuffer()))
{
//copy buffer into your output stream
byte[] buffer = new byte[bufferSize];
int read;
while ((read = ms.Read(buffer, 0, bufferSize)) > 0)
outputStream.Write(buffer, 0, read);
}


Also, if you are copying to more than more streams, you probably will want to reset the position:
ms.position = 0;