PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Jul 16, 2018 8:59 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: Thu Aug 03, 2017 5:46 am 

Joined: Thu Aug 03, 2017 5:17 am
Posts: 1

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:-

            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)

                    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)
                        noScale = true;

                    XImage image;

                    image = (XImage)imgSrc;


                    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));
         //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.


Reply with quote  
PostPosted: Thu Aug 03, 2017 6:56 am 
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 381
Location: CCAA

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.

Best regards
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)

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: Bing [Bot], Google [Bot] and 6 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