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);