I am trying to create a tagged PDF programmatically that includes images that need alt text. I have been following the guides/references to get the taggedContent and create a figure element.
My problem is that the SetImage() method appears to require an image file on disk (needing a string path to the image). I do not see any support for handing it a stream (have the image in memory). Is there support for using a in-memory image instead of one from disk?
@trstephenerickson
Currently, the SetImage()
method in Aspose.PDF requires a file path to an image on disk, and there is no direct support for using an in-memory image stream. To work around this limitation, you can save the in-memory image to a temporary file and then use that file path with the SetImage()
method.
Here’s a simple example in C#:
using Aspose.Pdf;
using System.IO;
// Create a new PDF document
Document pdfDocument = new Document();
// Create a figure element
Figure figure = new Figure();
// Assume 'imageStream' is your in-memory image stream
using (FileStream tempFile = new FileStream("tempImage.png", FileMode.Create, FileAccess.Write))
{
imageStream.CopyTo(tempFile);
}
// Set the image from the temporary file
figure.SetImage("tempImage.png");
// Add alt text
figure.AlternativeText = "Your alt text here";
// Add the figure to the document
pdfDocument.Pages[1].Paragraphs.Add(figure);
// Save the document
pdfDocument.Save("output.pdf");
// Clean up the temporary file
File.Delete("tempImage.png");
This code snippet demonstrates how to create a temporary file from your in-memory image and then use it to set the image in the PDF. After saving the PDF, it cleans up by deleting the temporary file.