PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Image resize, no change in PDF file size
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1932
Page 1 of 1

Author:  Teslo [ Mon Mar 05, 2012 2:35 pm ]
Post subject:  Image resize, no change in PDF file size

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.

Author:  Thomas Hoevel [ Mon Mar 05, 2012 3:51 pm ]
Post subject:  Re: Image resize, no change in PDF file size

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.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/