Hello all -
First post and pretty new to working with this library and manipulating PDFs in general.
The goal is to retrieve 2 or more documents from Azure Container Storage and create a single PDF. The files in Azure could be PDFs, PNGs, or JPGs. My code seems to work fine with retrieved PDFs and PNGs but not JPGs.
This snippet of code which reads a response when calling an Azure Container Storage (for a PDF or img) and puts it into a stream. It then attempts to create a PDF Page and put in the PDF Document. PDFs and PNGS work OK but not JPGs. Any advice, feedback or code snippets, or troubleshooting would be much appreciated.
Throws error at "XImage image = XImage.FromStream(contentStream)"
.NET 8
PDFSharp6.1.1
Code:
// create pdf document & page for the img
PdfDocument pdfDoc = new PdfDocument();
PdfPage pdfPage = pdfDoc.AddPage();
// prepare drawing surface for the page
XGraphics gfx = XGraphics.FromPdfPage(pdfPage);
using (var memoryStream = new MemoryStream())
{
await documentStream.CopyToAsync(memoryStream);
byte[] byteArray = memoryStream.ToArray();
// https://github.com/empira/PDFsharp/issues/90
var contentStream = new MemoryStream(byteArray, 0, byteArray.Length, writable: true, publiclyVisible: true);
// load the image from the stream into an XImage object
XImage image = XImage.FromStream(contentStream);
// draw the image at (0,0) on the pdf page (no scaling or resizing)
// FUTURE: we may need to scale or resize imgs
gfx.DrawImage(image, 0, 0);
var pdfStream = new MemoryStream();
pdfDoc.Save(pdfStream, false);
pdfStream.Position = 0;
return pdfStream;
}