PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Apr 25, 2024 6:54 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Nov 21, 2007 8:34 am 
Offline

Joined: Fri Oct 05, 2007 9:08 am
Posts: 6
I am having trouble drawing images to a PDF. The images are uploaded by users of a website, so I can't just use a format that I know works.

I retrieve the images as bytes from a database and place them in Image objects. I can save these images to disk and open the files with a viewer, and everything works fine when I do this.

I then create an XImage using XImage.FromGdiPlusImage and use XGraphics.DrawImage to draw it to the PDF. When I open the PDF, sometimes I get a black image with noise where the image should have been. This happens with JPG files. I have tried to convert such a file to TIFF, and in this case the image appears fine. I have tried using Photoshop to save the JPG in a very basic and compatible way, but that didn't help.

Has anyone else experienced this and found a solution? Is there documentation which specifies which formats are acceptable to XImage? If not, any ideas for a workaround?

Thanks in advance :)


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Nov 21, 2007 9:15 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
JPEG files are simply embedded in PDF - PDFsharp only sets some flags.
There's a known bug with CMYK JPEG and greyscale JPEG images in PDFsharp 1.00 - PDFsharp 1.00 sets incorrect flags for those.

In PdfImage.cs, method "InitializeJpeg()" you need this code:
Code:
      Elements[Keys.Length] = new PdfInteger(streamLength);
      Elements[Keys.Filter] = new PdfName("/DCTDecode");
      Elements[Keys.Width] = new PdfInteger(image.Width);
      Elements[Keys.Height] = new PdfInteger(image.Height);
      Elements[Keys.BitsPerComponent] = new PdfInteger(8);
      if ((image.gdiImage.Flags & ((int)ImageFlags.ColorSpaceCmyk | (int)ImageFlags.ColorSpaceYcck)) != 0)
      {
        Elements[Keys.ColorSpace] = new PdfName("/DeviceCMYK");
        if ((image.gdiImage.Flags & (int)ImageFlags.ColorSpaceYcck) != 0)
          Elements["/Decode"] = new PdfLiteral("[1 0 1 0 1 0 1 0]");  // Invert colors? Why??
      }
      else if ((image.gdiImage.Flags & (int)ImageFlags.ColorSpaceGray) != 0)
      {
        Elements[Keys.ColorSpace] = new PdfName("/DeviceGray");
      }
      else
      {
        Elements[Keys.ColorSpace] = new PdfName("/DeviceRGB");
      }

If the problem persists, please send us sample files.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Nov 21, 2007 9:29 am 
Offline

Joined: Fri Oct 05, 2007 9:08 am
Posts: 6
Thanks for that, very helpful.

If someone reading this is not interested in making changes to the PDFsharp code, then a possible workaround is to convert all images to TIFF on the fly, before inserting them in the PDF. The NET Image class will let you do this with the Save method, specifying TIFF as the output format and reading the XImage from that file. Of course that means disk IO which is not always possible, but there you have it.

Cheers


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Mar 05, 2008 8:41 pm 
Offline

Joined: Tue Mar 04, 2008 3:31 pm
Posts: 8
Ahhh, I'm having this same problem - grayscale scanned JPGs are showing up distorted and messed up. Thanks for the tip - I'll try and decide if I want to mess with the IO of converting (can you do that in memory??), or whether it'd just be easier to modify the PDFSharp code.

If the source can be changed, is there any chance of a fix or patch that handles any image better?


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Mar 06, 2008 8:09 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
edmicman wrote:
If the source can be changed, is there any chance of a fix or patch that handles any image better?

With the code changes shown above JPEG images will be handled correctly.
There won't be a fix that can handle JPEG images better than "correctly". :lol:

If you find non-JPEG images that are not handled correctly, send us a sample and we'll try to fix it. Standard formats are already implemented.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Mar 06, 2008 2:37 pm 
Offline

Joined: Tue Mar 04, 2008 3:31 pm
Posts: 8
Thank you! I did end up getting it working....my images are displaying, and I can get them to display in the size I want. Now I just have to work on getting a better source image so it doesn't lose as much quality when I increase the size :-)

Great job on the library!


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 357 guests


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:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group