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:
Code:
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 this public Dropbox folder
https://www.dropbox.com/sh/8oqqdmec46bvpu6/AAAJBzfE0XE8KqV8unr4T_1Fa 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?
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) or with Firefox Viewer 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.
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:
Code:
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);
You can check a possible reasons that this problem happens in this link
http://stackoverflow.com/questions/35660744/pdfsharp-error-displaying-a-jpg-in-pdf.