PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Jun 16, 2024 7:10 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
PostPosted: Thu May 23, 2024 10:07 pm 
Offline

Joined: Mon Mar 16, 2020 4:23 pm
Posts: 7
I am trying to convert images to PDF (scaling to full page), while keeping the file size moderate. I understand that PdfSharpCore embeds the full image, so it has to be resized first. Consider the following code (can be run directly from LinqPad):

Code:
const string ImagePath = @"C:\pexels-eberhardgross-443446.jpg";
const string PdfPath = @"C:\foo.pdf";

using (var imageStream = File.OpenRead(ImagePath))
{
    var image = new MagickImage(imageStream);
    image.Resize(new Percentage(20));
    image.Quality = 50;

    using (var resizedImageStream = new MemoryStream())
    {
        image.Write(resizedImageStream);
        resizedImageStream.Position = 0;

        using (var document = new PdfDocument())
        {
            PdfPage page = document.AddPage();
            page.Size = PdfSharpCore.PageSize.A4;
            using (XImage img = XImage.FromStream(() => resizedImageStream))
            {
                XGraphics gfx = XGraphics.FromPdfPage(page);
                var pageWidth = gfx.PageSize.Width * 0.95;
                var pageHeight = gfx.PageSize.Height * 0.95;

                // compute image width / height maintaining image aspect ratio
                var width = pageWidth;
                var height = (((double)width / (double)img.PixelWidth) * img.PixelHeight);
                if (height > pageHeight)
                {
                    height = pageHeight;
                    width = (((double)height / (double)img.PixelHeight) * img.PixelWidth);
                }

                gfx.DrawImage(img, (gfx.PageSize.Width - width) / 2, (gfx.PageSize.Height - height) / 2, width, height);
            }
            using (var pdfStream = File.OpenWrite(PdfPath))
            {
                document.Save(pdfStream);
            }
        }
    }
}


You can find the sample image I am using here.

Even when I resize to 1%, the file size of the resulting PDF file stays the same (about 3MB), although it only contains an image that has such low resolution that you can't see anything but pixels anymore.

What am I doing wrong?


Top
 Profile  
 
PostPosted: Fri May 24, 2024 8:21 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 940
Location: CCAA
Marvino wrote:
What am I doing wrong?
You are using the wrong forum.
PDFsharp handles images differently from PdfSharpCore.

See also:
https://docs.pdfsharp.net/General/Issue-Reporting.html

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


Top
 Profile  
 
PostPosted: Fri May 24, 2024 11:46 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 940
Location: CCAA
According to a deleted post on SO, this is no longer reproducible.

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC


Who is online

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