How to convert image stream into pdf

I’m using the sample as
using (var documentForImage = new Aspose.Pdf.Document())
{
// Add page
var page = documentForImage.Pages.Add();
var image = new Aspose.Pdf.Image();
// Load BMP file
image.ImageStream = memStream;// memStream is image memory stream

                                page.Paragraphs.Add(image);
                                // Save PDF document
                                documentForImage.Save(outputPdfStream, Aspose.Pdf.SaveFormat.Pdf);
                            }

and: add Aspose.drawing 25.2.0; and aspose.pdf 25.2
But get the error: failed with the error “The type initializer for ‘Aspose.Pdf.XmpField’ threw an exception.”

@lenam

To convert an image stream into a PDF using Aspose.PDF, you can follow the example below. It demonstrates how to create a PDF document, add a page, and insert an image from a memory stream into that page.

Here’s a sample code snippet that illustrates this process:

using System.IO;
using Aspose.Pdf;
using System.Drawing;

public class ImageToPDF
{
    public static void ConvertImageStreamToPDF(MemoryStream memStream, Stream outputPdfStream)
    {
        // Create a new PDF document
        Document documentForImage = new Document();
        
        // Add a new page to the document
        Page page = documentForImage.Pages.Add();
        
        // Create an image object
        Aspose.Pdf.Image image = new Aspose.Pdf.Image();
        
        // Set the image stream to the memory stream containing the image
        image.ImageStream = memStream;
        
        // Add the image to the page's paragraphs
        page.Paragraphs.Add(image);
        
        // Save the PDF document to the output stream
        documentForImage.Save(outputPdfStream, Aspose.Pdf.SaveFormat.Pdf);
    }
}

Important Notes:

  1. Ensure that the memory stream (memStream) contains a valid image format that Aspose.PDF can process (e.g., JPEG, PNG).
  2. If you encounter the error “The type initializer for ‘Aspose.Pdf.XmpField’ threw an exception,” it may be related to version compatibility or missing dependencies. Make sure you are using compatible versions of Aspose.PDF and Aspose.Drawing libraries.

This code will successfully convert an image from a memory stream into a PDF document. If you continue to experience issues, consider checking the library versions or reviewing the documentation for any additional requirements.

these are my reference
PackageReference Include=“Aspose.Cells” 25.2.0
PackageReference Include=“Aspose.Drawing” 25.2.0
PackageReference Include=“Aspose.Email” 25.2.0
PackageReference Include=“Aspose.Imaging” 25.2.0
PackageReference Include=“Aspose.PDF” 25.2.0
PackageReference Include=“Aspose.Slides.NET” 25.2.0
PackageReference Include=“Aspose.Words” 25.2.0

@lenam

We were not able to notice this error in our environment while testing the scenario. Are you working in Linux? Can you please try using Aspose.Pdf.Drawing for .NET instead of Aspose.PDF for .NET?

Thanks for help,
I have resolved by using Aspose.Word to insert image into page, and then convert it to PDF.

@lenam

Its nice to know that you have found a workaround. However, you can share further information with us if you plan to use Aspose.PDF for same purpose and still notices this issue. We will further proceed to assist you accordingly.