PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Aug 18, 2017 8:47 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Mon Feb 29, 2016 6:34 pm 
Offline

Joined: Mon Feb 29, 2016 6:26 pm
Posts: 2
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.


Attachments:
File comment: Image i'm trying to add.
E MDF104P2.jpg
E MDF104P2.jpg [ 108.66 KiB | Viewed 1524 times ]
Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 02, 2016 7:11 am 
Offline
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 266
Location: CCAA
Hi!
prueba2306 wrote:
This error is not consistent, some photos have this exact problem, some others don't and I don't know why.
The photo you supply seems to be in CMYK format.
But when this photo is opened with GDI+ or WPF, both will report it as an RGB photo.

PDFsharp supports CMYK images - if GDI+ and WPF report them as CMYK.

The photo was created with an Adobe tool. And things often become complicated when Adobe tools are involved.

PDFsharp correctly copies the image into the PDF file. Adobe Reader relies on additional information supplied with the image - and this information indicates RGB (as reported by the framework. Adobe Reader shows an incorrect image.

Firefox shows the PDF file correctly.


We saw your post on SO and are aware of the issue. There won't be a quick fix. We wrote own code to analyse JFIF files so PDFsharp no longer has to rely on GDI+ and WPF to analyse JPEG images in JFIF format.
The file you provide is not in JFIF format, it is EXIF. We don't have code for that format yet.


In general it would be better to use XImage.FromStream() instead of XImage.FromGdiPlusImage - which does not work with the image you supplied.

A possible workaround: use "foto.Save([stream], ...)" after calling "Image foto = Image.FromStream([stream]);" and pass that stream to XImage.FromStream.
This allows you to get a JPEG image in RGB format that will work with PDFsharp.

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


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 07, 2016 2:59 pm 
Offline

Joined: Mon Feb 29, 2016 6:26 pm
Posts: 2
This is the solution i got, thanks to TH-Soft for show me the path:

Code:
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).


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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:  
Powered by phpBB® Forum Software © phpBB Group