PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 5:51 pm

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 
Offline

Joined: Thu Aug 03, 2017 5:17 am
Posts: 1
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


Top
 Profile  
Reply with quote  
PostPosted: Thu Aug 03, 2017 6:56 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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.

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


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