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

PDF File size is big with JPG in it
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3636
Page 1 of 1

Author:  jayhu25 [ Thu Aug 03, 2017 5:46 am ]
Post subject:  PDF File size is big with JPG in it

Hello

I am using PDFSharp (latest obtained via NuGet on VS2015) to create PDF that will have 1 JPG image/ page. Typically I would have 3-4 JPG image files and I need to create a PDF with each page having a single image. The size of the JPG files are around ~600-700KB, however when I add them to the page and save, a single page PDF size comes out to be around 3MB, so for a 3 page document its around 12MB. I am testing this in Release mode.

Below is the code I am using:-

Code:
            s_document = new PdfDocument();
            s_document.Info.Title = "Image Test PDF";
            s_document.Info.Author = "Jay2017";
            s_document.Info.Subject = "Auto generated PDF";
           
            //Get all the JPG Sets for the current scan
            DirectoryInfo di = new DirectoryInfo(szPath);
            int i = 0;
           
            FileInfo[] fInfo = di.GetFiles(filename + "_?.JPG");
            if (fInfo.Length > 0)
            {
                foreach (var fi in fInfo)
                {

                    Console.WriteLine(fi.Name);
                    PdfPage page = s_document.AddPage();
                    XGraphics gfx = XGraphics.FromPdfPage(page);
                    page.Size = PdfSharp.PageSize.A4;
                   

                    Image imgSrc = Image.FromFile(szPath + "\\" + fi.Name);

                    //If image width is larger than the threshodl value, rotate the image 90 degrees right.
                    if (imgSrc.Width > nImageWidth)
                    {
                       
                        imgSrc.RotateFlip(RotateFlipType.Rotate90FlipNone);
                       
                    }
                    else
                    {
                        noScale = true;
                    }
                   

                    XImage image;

                   
                    image = (XImage)imgSrc;


                    i++;

                    if (noScale) //Most likely ID document, so try to center it on page without scaling
                    {
                        //Page center = Pagewidth/2, Image half point = Imagewidth/2, Image left = PageHalfWidth-ImageHalfWidth
                        gfx.DrawImage(image, (page.Width/2)-(image.PointWidth/2), (page.Height/2)-(image.PointHeight/2));
                    }
                    else
                    {
         //Regular A4 document, draw to full page
                         gfx.DrawImage(image, new XRect(0, 0, page.Width, page.Height));
                       
                    }
                   
                    imgSrc = null;
                    image = null;
          }


Anyone can point me in the right direction to get the PDF file size down.

Regards
Jay

Author:  TH-Soft [ Thu Aug 03, 2017 6:56 am ]
Post subject:  Re: PDF File size is big with JPG in it

Hi!

Create an XImage directly from your JPEG file (XImage.FromFile) and do the rotation in PDFsharp using transformations.
This will preserve the original JPEG file and prevent the size increase.

I assume that calling "imgSrc.RotateFlip" leads to having a bitmap image in the PDF file.
You could call the JPEG encoder before passing the image to PDFsharp - this will reduce the file size, but JPEG compression leads to JPEG artefacts - and compressing the image twice will increase the artefacts.

Your thread title is wrong as you do not have a JPEG in your PDF.

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