PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Jul 18, 2024 9:21 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Mon Mar 05, 2012 2:35 pm 
Offline

Joined: Mon Mar 05, 2012 2:24 pm
Posts: 1
Hi!

I'm working on a project in C# which involves converting different types of docs into one single PDF.
For image conversion, i use PDFSharp (which is great btw), but I noticed an issue.

So, I have this function:
Code:
public static void ImageToPdf(string fileNameInput, string fileNameOutput)
        {
            try
            {
                PdfDocument doc = new PdfDocument();
                TiffImageSplitter tiff = new TiffImageSplitter();

                int pageCount = tiff.getPageCount(fileNameInput);

                for (int i = 0; i < pageCount; i++)
                {
                    PdfPage page = new PdfPage();

                    System.Drawing.Image tiffImg = tiff.getTiffImage(fileNameInput, i);

                    XImage img = XImage.FromGdiPlusImage(tiffImg);

                    // Scale img
                    double originalAspectRatio = (double)img.PixelWidth / (double)img.PixelHeight;
                    double customAspectRatio = 1;

                    if (originalAspectRatio >= customAspectRatio)
                    {
                        page.Width = 700;
                        page.Height = img.PointHeight / (img.PointWidth / 700);
                    }
                    else
                    {
                        page.Width = img.PointWidth / (img.PointHeight / 700);
                        page.Height = 700;
                    }

                    doc.Pages.Add(page);

                    XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[i]);

                    // Draw it scaled
                    xgr.DrawImage(img, 0, 0, page.Width, page.Height);
                }

                doc.Save(fileNameOutput);
                doc.Close();
            }
            catch { throw; }
        }


The function works fine, but (there's always a but!) with or without image scaling (resizing), the PDF file size remains the same (ex: for about 4 .jpg's of ~1Mb each => ~30mb pdf, and the same size even if the images were scaled!)

Did I missed smth? Or...

I'm expecting that after the images have been scaled, the pdf file size to be smaller. That's the main reason why I'm scaling them.

Thanks,
Teslo.


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 05, 2012 3:51 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
Teslo wrote:
I'm expecting that after the images have been scaled, the pdf file size to be smaller.
PDF files have no pixels. Showing the same image on a smaller area does not change the file size, that's by design.

You can resize the images before you pass them to PDFsharp - define how many DPI you would like and if the image is larger, reduce the DPI.
For JPEG images you can also reduce the quality level to reduce the file size.

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

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