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.”
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:
Ensure that the memory stream (memStream) contains a valid image format that Aspose.PDF can process (e.g., JPEG, PNG).
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.
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?
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.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.