PDFsharp, error displaying a JPG in PDF
Asked Answered
M

3

8

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

I'm using the next code to perform 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 I open the PDF file, the image appear wrong, corrupt, broken... I don't know how to explain it, you can download the original photo and the PDF generated in the next public Dropbox folder to see the result.

This error is not consistent, some photos have this exact problem, some others don't and I don't know why. Maybe is the format in the file or something similar? If that is the problem, which formats are valid?

Any help will be appreciated.

Edit: Something I noted is that the wrong image looks different depending on with which program I visualize the PDF. For example, if you see the PDF using the visualizer of Dropbox (using the link i provided) the image looks fine; if I use the Chrome PDF Viewer, the image is wrong but only appear in black and white and with stripes but still visible; if I use Adobe Acrobat Reader DC the image is still wrong but completely unrecognized.

Edit 2: I changed to PDFSharp v1.50.4000 (beta 3) to see if maybe its a problem of the library but the problem is still the same. The code, with the new version, is as follow:

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);
Martinic answered 26/2, 2016 at 20:4 Comment(2)
Considering your edit there probably are problems in the jpg itself... Can you share it for inspection?Enchorial
@Enchorial The image can be found on Dropbox (see link in text below code box).Kalina
M
9

This is the solution i got, thanks to TH-Soft from PDFsharp forum for show me the path:

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 I add the image to the PDF, I convert the image to PNG so the format "issues" that the image have are removed.

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

Martinic answered 7/3, 2016 at 14:53 Comment(4)
Worked for me (+1). As a [stream] you can use File.OpenRead(imgFilePath)Kronos
in my case image is not displayed at all and this did not helpedAlida
@JadaVonRuth this is an old question. What version are you using? Can you share the image/file?Martinic
@prueba-prueba I know its old question. I was using .net core port of the library (PdfSharpCore), there was an issue in linux docker, gdi+ library was already there, problem was something else. Finally I resolved it using another .net core port of pdfsharp.Alida
K
0

Handling of JPEG images works better when you use PDFsharp 1.50 or later and use XImage.FromStream instead of Image.FromStream plus XImage.FromGdiPlusImage.

PDFsharp needs a copy of the JPEG file. Using XImage.FromStream ensures PDFsharp gets the original data.

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

Kalina answered 26/2, 2016 at 20:20 Comment(3)
Hi, bro. That didn't work, I downloaded the PDFsharp project 1.5 beta3b (last version i found) and add it to the project but the image still has the same problem.Martinic
Yes, this image is not handled correctly, strange JPEG format. Looks correct when PDF file is viewed with Firefox. Will investigate this further next week. If you have to use this specific image: open with an image processor (e.g. MS Paint), then use File => Save As and the new image will work with PDFsharp.Kalina
Thanks, @ThomasH, that solved the problem. I still don't know if this could be a final answer or is just a "patch" for this question. If you post this as an answer i glady will mark it as the solution.Martinic
M
0

Your image is a CMYK JPEG. When it is embedded in the PDF file its colorspace is set to RGB and this causes the incorrect decoding.
I do not know if you can set the image colorspace in your code to CMYK or if this is something that needs to be fixed in PDFsharp.

Muldon answered 29/2, 2016 at 15:10 Comment(3)
PDFsharp relies on GDI+ or WPF to determine whether a JPEG is RGB or CMYK.Kalina
Do you think we should report this problem to the PDFsharp developers?Martinic
Unfortunately both GDI+ and WPF are buggy when dealing with CMYK JPEGs.Muldon

© 2022 - 2024 — McMap. All rights reserved.