PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Fri May 16, 2025 6:04 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Fri May 09, 2025 3:11 pm 
Offline

Joined: Fri May 09, 2025 2:57 pm
Posts: 1
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;
}


Attachments:
Screenshot 2025-05-09 110945.jpg
Screenshot 2025-05-09 110945.jpg [ 118.22 KiB | Viewed 1074 times ]
Top
 Profile  
Reply with quote  
PostPosted: Tue May 13, 2025 6:28 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1044
Location: CCAA
Hi!
RedBirdOBX wrote:
PDFSharp6.1.1
If PDFsharp 6.2.0 Preview 3 cannot open the JPEG files either, then we will need the files for further investigation.
If the JPEG files do not have a JFIF header, then we will also need a file format specification.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group