PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 10:10 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Mon Aug 21, 2023 10:25 pm 
Offline

Joined: Mon Mar 23, 2020 1:48 pm
Posts: 5
I work on a utility that transforms vendor datasheets for inclusion in a larger PDF "bundle" (containing suppliers, etc.). As part of this, I scale pages to about 90% of the input size to leave room for markings around the edges.

Due to environment factors in our systems, I am using the latest PDFSharp/MigraDoc 1.5 from Github. The compiled (by me) DLLs are version 1.51.5185.0.

Some of the input datasheets, though, simply don't scale properly. I've looked at multiple code samples and feel I'm not making any headway.

I stripped some code down to the bare minimum and include it below. It takes a file to scale, a target page size (usually PageSize.Letter in my case), and a scale factor, typically 0.9. It should scale the input PDF and output it, preserving the page orientation and page size.

Code:
       
static public int ScalePagesSimple(string fn, PageSize pageSize, double scale)
        {
            if (scale >= 1.0)  // 1.0 means nothing to do, larger isn't supported here
            {
                return 0;
            }

            int scaledPagesCount = 0;

            var dir = System.IO.Path.GetDirectoryName(fn);
            var originalFilenameNoPath = System.IO.Path.GetFileName(fn);
            var backupFilename = System.IO.Path.GetFileNameWithoutExtension(fn) + "_beforescale.PDF";
            var backupFilePath = System.IO.Path.Combine(new string[] { dir, backupFilename });

            // backup the original file then delete it.
            if (File.Exists(backupFilePath)) File.Delete(backupFilePath);
            System.IO.File.Copy(fn, backupFilePath);

            var o = PdfReader.Open(fn, PdfDocumentOpenMode.Modify);

            // load the original document
            var inpPdfForm = XPdfForm.FromFile(fn);

            // scale each page into the output document
            for (int thisPageNumber = 0; thisPageNumber < o.PageCount; thisPageNumber++)
            {
                scaledPagesCount++;
                var inpPage = o.Pages[thisPageNumber];

                // calculate the center point on the page; unsure if this is necessary
                var newXcenter = inpPage.Width.Point / 2;
                var newYcenter = inpPage.Height.Point / 2;

                // Associate a graphics context with the designated output page,  indicating that the graphics
                // context will overwrite existing content (Replace) taken from the inpPage.
                // Then specify scaling to apply to everything drawn in the context.
                XGraphics gfx = XGraphics.FromPdfPage(inpPage, XGraphicsPdfPageOptions.Replace);
                gfx.ScaleAtTransform(scale, scale, newXcenter, newYcenter);

                // Construct a rectangle with dimensions of the page
                XRect pageRect = new XRect(0, 0, inpPage.Width.Point, inpPage.Height.Point);

                // convert the input page to a graphic and then draw it on the page.
                inpPdfForm.PageNumber = thisPageNumber + 1; // pdf forms are 1-based
                XImage inpImg = inpPdfForm;

                // Render the image of the current page in the PDF into the scaled rectange and draw it into
                // the graphics context.
                gfx.DrawImage(inpImg, 0, 0);
            }
            o.Save(fn);
           
            return scaledPagesCount;
        }


I am attaching two files showing what happens. The one with "beforescale" is the input PDF. The other is the resulting PDF after scaling to 0.9 (90%).

I'm thinking I'm not understanding something correctly about the combination of orientation/width/height/rotation, but maybe there's something more.

Any tips here are appreciated.


Attachments:
bearings.zip [211.6 KiB]
Downloaded 125 times
Top
 Profile  
Reply with quote  
PostPosted: Wed Aug 23, 2023 10:24 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 916
Location: CCAA
There is a known problem with rotation in version 1.50.

Maybe this post can help you:
viewtopic.php?p=11548#p11548

Workaround:
Code:
page.Orientation = PageOrientation.Portrait;


There are several problems with earlier versions (1.32 and older).

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


Top
 Profile  
Reply with quote  
PostPosted: Wed Aug 23, 2023 2:10 pm 
Offline

Joined: Mon Mar 23, 2020 1:48 pm
Posts: 5
TH-Soft wrote:
There is a known problem with rotation in version 1.50.

Maybe this post can help you:
viewtopic.php?p=11548#p11548

Workaround:
Code:
page.Orientation = PageOrientation.Portrait;


There are several problems with earlier versions (1.32 and older).


Thanks for this link. I've already suspected there is a bug based on what I see in the Orientation and Rotate properties. I have a tentative workaround in testing now.

I'd look into moving to the Net 6 version, but no idea if this particular behavior is fixed. Moving to an entirely different platform will be a lot of work on my end, so I'd like to avoid that.


Top
 Profile  
Reply with quote  
PostPosted: Wed Aug 23, 2023 3:18 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 916
Location: CCAA
GeekPriest wrote:
I'd look into moving to the Net 6 version, but no idea if this particular behavior is fixed.
Most likely not.
This is complicated and was not addressed in the current transition to PDFsharp 6.0.0 Preview 3.
So a workaround for 1.51 will most likely also work with 6.0.0 Preview 3.

_________________
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  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 424 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