PDFsharp, error displaying jpg to pdf

I am trying to perform a simple action: adding a photo (JPG file) inside a PDF file generated from scratch using PDFsharp v1.32.2608.0 using .NET Framework 4.0 and MVC.NET

I am using the following code to complete this action:

PdfDocument doc = new PdfDocument(); PdfPage pag = doc.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(pag); Image foto = Image.FromStream([stream]); XImage xfoto = XImage.FromGdiPlusImage(foto); gfx.DrawImage(xfoto, 30, 130, 380, 250); MemoryStream stream = new MemoryStream(); doc.Save(stream, false); 

The problem is that when you open a PDF file, the image seems to be wrong, damaged, broken ... I donโ€™t know how to explain it, you can download the original photo and PDF generated in the next public Dropbox folder to see the result.

This error is incompatible, some photos have this exact problem, others do not, and I donโ€™t know why. Maybe this is a file format or something like that? If this is a problem, then which formats are valid?

Any help would be appreciated.

Edit: something I noticed is that the wrong image looks different depending on which program I render the PDF with. For example, if you see a PDF document using the Dropbox visualizer (using the provided link), the image looks great; if I use the Chrome PDF Viewer, the image is erroneous, but is displayed only in black and white and with stripes, but still visible; if I use Adobe Acrobat Reader DC, the image is still erroneous, but completely unrecognized.

Edit 2: I changed to PDFSharp v1.50.4000 (beta 3) to see maybe the library problem, but the problem remains the same. The code with the new version is as follows:

 PdfDocument doc = new PdfDocument(); PdfPage pag = doc.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(pag); XImage xfoto = XImage.FromStream([stream]); gfx.DrawImage(xfoto, 30, 130, 380, 250); MemoryStream stream = new MemoryStream(); doc.Save(stream, false); 
+6
source share
3 answers

This is the solution I got thanks to TH-Soft from the PDFsharp forum to show me the way:

 PdfDocument doc = new PdfDocument(); PdfPage pag = doc.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(pag); MemoryStream strm = new MemoryStream(); Image img = Image.FromStream([stream]); img.Save(strm, System.Drawing.Imaging.ImageFormat.Png); XImage xfoto = XImage.FromStream(strm); gfx.DrawImage(xfoto, 30, 130, 380, 250); MemoryStream stream = new MemoryStream(); doc.Save(stream, false); 

Before adding the image to PDF, I convert the image to PNG so that the format "problems" that the image is deleted.

Of course, this is not the best solution, and PDFsharp should manage this format problem, but it will not happen soon (at least it is not managed in PDFsharp 1.5 beta3).

+5
source

JPEG image processing improves when using PDFsharp 1.50 or newer and use XImage.FromStream instead of Image.FromStream plus XImage.FromGdiPlusImage .

PDFsharp requires a copy of the JPEG file. Using XImage.FromStream ensures that PDFsharp receives the raw data.

Your code will work fine with PDFsharp 1.32 if you stick with Windows XP. Later versions of Windows have the problem you see, but with PDFsharp 1.50 it should work again.

0
source

Your image is a CMYK JPEG. When it is embedded in a PDF file, its color space is set to RGB, which causes incorrect decoding.
I do not know if you can set the color space of the image in your code to CMYK or if this is what needs to be fixed in PDFsharp.

0
source

All Articles