Convert multiple images to single pdf using memory streams (no saving to disk)

I used this example to convert an image to PDF using a memory stream. It works fine. However, if I try to do it in a loop to add multiple images to a single PDF with memory streams, I run into a few issues and cannot get it to work. I've tried using separate memory streams, but the file ends up corrupt. I've also tried reusing the same memory stream, but even though I close, dispose and set it it to null, it always puts the same picture in the PDF multiple times.

I've searched the web for hours to try to find a sample that will work, but have not had any luck. If anyone has a code sample that works, please pass it my way. Remember, I am NOT saving ANY files to disk. I want to do everything in memory.

Here is the code that results in the same image being used multiple times:

Dim pdf1 As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()
Dim license As Aspose.Pdf.License = New Aspose.Pdf.License() license.SetLicense("Aspose.Total.lic") pdf1.Conformance = Aspose.Pdf.Generator.PdfConformance.PdfA1B

       <br>Dim sec1 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add()
       <br>For Each mobileEntryImage As MobileEntryImage In mobileEntryImages<br>   Dim image1 As Aspose.Pdf.Generator.Image = New Aspose.Pdf.Generator.Image(sec1)<br>   Dim meiStream As MemoryStream = New MemoryStream(mobileEntryImage.FileContent)<br>   image1.ImageInfo.ImageStream = meiStream<br>   sec1.Paragraphs.Add(image1)<br>   pdf1.Save("C:\HelloWorld.pdf")<br>   meiStream.Close()<br>   meiStream.Dispose()<br>   meiStream = Nothing<br>   image1 = Nothing<br></code><code>Next</code></p><p><code></code> </p>

The resulting output is attached.

Note - If I call the Save method outside of the loop, the resulting output is corrupt and cannot open.

I got it to work using the following code:

Dim pdfMemoryStream As MemoryStream = New MemoryStream()
Dim pdf1 As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()
Dim license As Aspose.Pdf.License = New Aspose.Pdf.License()
license.SetLicense("Aspose.Total.lic") pdf1.Conformance = Aspose.Pdf.Generator.PdfConformance.PdfA1B
Dim sec1 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add()

For Each mobileEntryImage As MobileEntryImage In mobileEntryImages
Dim image1 As Aspose.Pdf.Generator.Image = New Aspose.Pdf.Generator.Image(sec1)
Dim meiStream As MemoryStream = New MemoryStream(mobileEntryImage.FileContent)
image1.ImageInfo.ImageStream = meiStream
sec1.Paragraphs.Add(image1)
Next
pdf1.Save(pdfMemoryStream)

Dim fileContent As Byte() = pdfMemoryStream.ToArray()
pdfMemoryStream.Close()

Hi Jeremy,


Thanks for contacting support.

I am glad to hear that your problem is resolved. Please continue using our product and in the event of any further query, please feel free to contact.