PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Jul 15, 2024 2:48 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Fri Dec 03, 2010 9:20 am 
Offline

Joined: Fri Dec 03, 2010 9:03 am
Posts: 6
Hello,

sadly your board has eaten my last post, so i have a new try:

I use PDFsharp for producing reports with images in it (1200x800) and that works very well, but the outputted file is ~3MB per image, although the loaded images are in jpeg format and have ~250kb.
The problem is the same if i use GDI+ images or XImages, using 1.31 and producing a release build.

Thanks in advance and congratulations for your great and nearly perfect api ;)

Greets David


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 03, 2010 9:21 am 
Offline

Joined: Fri Dec 03, 2010 9:03 am
Posts: 6
Sorry for the multiple posts, your board is running crazy :)


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 03, 2010 9:30 am 
Offline

Joined: Fri Dec 03, 2010 9:03 am
Posts: 6
I tried it again, with pdf sharp 2,3 MB and after a print with pdfCreator just 420kb...


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 03, 2010 9:38 am 
Offline

Joined: Fri Dec 03, 2010 9:03 am
Posts: 6
I analysed both files with an editor, and this is the part before the image with pdfsharp:
Code:
Type/XObject
/Subtype/Image
/Length 21570
/Filter/FlateDecode
/Width 1098
/Height 330
/BitsPerComponent 8
/ColorSpace/DeviceRGB
/Interpolate true
>>
stream
...


and this after pdf creator printed the document

Code:
<</Subtype/Image
/ColorSpace/DeviceCMYK
/Width 1280
/Height 960
/BitsPerComponent 8
/Interpolate true
/Filter/DCTDecode/Length 393380>>
stream
...


Do i have missed something, where i have to specify which compression (BMP, JPEG,...) is used by PDFsharp?


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 03, 2010 10:53 am 
Offline

Joined: Fri Dec 03, 2010 9:03 am
Posts: 6
So, i solved it after i looked into your source code. You treading every gdi image as an normal bitmap, thats ok if you want to display images without loss of quality, but if you want to display loaded jpeg files thats not nessesary and a waste of memory.

I added the following code to your XImage Class:

Code:
    /// <summary>
    /// Creates an image from the specified stream.
    /// </summary>
    /// <param name="stream">The stream to a BMP, PNG, GIF, JPEG, TIFF, or PDF file.</param>
    public static XImage FromStream(Stream stream)
    {
        if (stream == null)
            throw new ArgumentNullException("stream");
        if (stream.Length == 0)
            throw new ArgumentException("Provided stream is empty", "stream");
        return new XImage(stream);
    }

    /// <summary>
    /// Conversion from Image to a jpeg based (compressed) XImage.
    /// </summary>
    public static XImage FromGdiPlusToJpegImage(Image image)
    {
        if (image == null)
            throw new ArgumentNullException("image");
        using (MemoryStream memStrm = new MemoryStream())
        {
            image.Save(memStrm, ImageFormat.Jpeg);
            memStrm.Position = 0;
            return FromStream(memStrm);
        }
    }


Feel free to use/modify my addition in your future releases.

Greets David Tielke


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 06, 2010 11:03 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
DavidT27 wrote:
You treading every gdi image as an normal bitmap

You're using PDFsharp in a way it wasn't designed for.

JPEG images created from JPEG files will be stored as JPEG inside the PDF - without decompression and re-compression the original, unaltered file is stored in the PDF.

You use images that came from JPEG. With your modification they will be re-compressed to JPEG and (potentially) the quality of the image in the PDF file is not as good as the image that was originally read.
So using your modification, JPEG images with a very low quality could be much larger (and still low quality), JPEG images with a very high quality could be much smaller (with lower quality).

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 06, 2010 12:38 pm 
Offline

Joined: Fri Dec 03, 2010 9:03 am
Posts: 6
Hey,
from my point of view, pdfsharp is an api to generate pdf files with .NET, isn't it? And in my project, i receive jpegs from a webservice in a bitmap object, so what options do i have with your library to fit my needs?

Greets David


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 06, 2010 12:58 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
If the webservice returned JPEG files you'd have smaller transfers and would avoid decompression/re-compression and the quality-loss that comes with re-compressions.

I just wanted to point out that PDFsharp normally treats JPEG files as JPEG files. In typical cases, JPEG files are simply copied into the PDF file (provided the bitmap was created from a JPEG file and PDFsharp can get access to the file).

You feed bitmaps created from JPEG files into PDFsharp. These are treated as bitmaps because the webservice transfers them as bitmaps. This is not the case PDFsharp was designed for. Your modification solves your size problem and that's OK for me (and if you cannot change the webservice this probably is the best remaining solution).
The special circumstances in your case (webservice) prevent PDFsharp from accessing the original JPEG file.

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 67 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