PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Sat Apr 19, 2025 2:50 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sun Mar 16, 2025 9:39 pm 
Offline

Joined: Wed Jan 29, 2025 11:55 am
Posts: 4
Hello. I am having an issue with Safari’s print scaling using MigraDoc. Documents are automatically scaled to 93% which causes them to print too small.

I appreciate that this is a Safari thing, but the same issue doesn’t occur with PdfSharp.

There seems to be something that MigraDoc does which causes Safari to think the page is too large to print at 100% and therefore scale it to 93%.

I appreciate any assistance anyone can offer.

Thanks!

The following code uses MigraDoc and scales to 93% in Safari: -

Code:
// PDFsharp-MigraDoc 6.1.1 from NuGet
// Legacy .NET Framework 4.8 application

using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using PdfSharp.Pdf;
using PdfSharp.Pdf.Advanced;
using System.IO;

namespace SafariTest
{
   public class HelloMigraDoc
   {
        public static byte[] CreateDocument()
        {
            var document = new Document();
            var section = document.AddSection();
            var paragraph = section.AddParagraph();
            paragraph.AddText("Hello World!");

            var pdfRenderer = new PdfDocumentRenderer
            {
                Document = document,
                PdfDocument =
                {
                    Options =
                    {
                        FlateEncodeMode = PdfFlateEncodeMode.BestCompression,
                        NoCompression = false,
                        CompressContentStreams = true
                    }
                }
            };

            pdfRenderer.RenderDocument();

            AddViewerPreferences(pdfRenderer); // this has no effect in Safari

            using (var stream = new MemoryStream())
            {
                pdfRenderer.PdfDocument.Save(stream, false);
                return stream.ToArray(); // results in undesired print scaling of 93% in Safari
            }
        }

        public static void AddViewerPreferences(PdfDocumentRenderer pdfRenderer)
        {
            // Turn off print scaling.
            if (!pdfRenderer.PdfDocument.Internals.Catalog.Elements.ContainsKey("/ViewerPreferences"))
                pdfRenderer.PdfDocument.Internals.Catalog.Elements["/ViewerPreferences"] = new PdfDictionary(pdfRenderer.PdfDocument);
            var viewerPreferences = pdfRenderer.PdfDocument.Internals.Catalog.Elements["/ViewerPreferences"] is PdfReference viewerPreferencesReference
                ? viewerPreferencesReference.Value as PdfDictionary
                : pdfRenderer.PdfDocument.Internals.Catalog.Elements["/ViewerPreferences"] as PdfDictionary;
            if (viewerPreferences != null) viewerPreferences.Elements["/PrintScaling"] = new PdfName("/None");
        }
    }
}


The following code uses PdfSharp and scales to 100% in Safari: -

Code:
// PDFsharp-MigraDoc 6.1.1 from NuGet
// Legacy .NET Framework 4.8 application

using System.IO;
using PdfSharp.Pdf;
using PdfSharp.Drawing;

namespace SafariTest
{
   public class HelloPdfSharp
   {
        public static byte[] CreateDocument()
        {
            using (var document = new PdfDocument())
            {
                var page = document.AddPage();
                using (var gfx = XGraphics.FromPdfPage(page))
                {
                    var font = new XFont("Verdana", 12);
                    gfx.DrawString("Hello World!", font, XBrushes.Black,
                        new XRect(72, 72, page.Width.Point - 144, page.Height.Point - 144),
                        XStringFormats.TopLeft);
                }

                using (var stream = new MemoryStream())
                {
                    document.Save(stream, false);
                    return stream.ToArray(); // results in desired print scaling of 100% in Safari
                }
            }
        }
    }
}


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 17, 2025 10:15 pm 
Offline

Joined: Wed Jan 29, 2025 11:55 am
Posts: 4
I'll answer my own question with a workaround: -

Looping through each page and setting the Size property to A4 before returning the document causes print scaling to revert to 100% in Safari. For some reason setting the page size for the section (eg. section.PageSetup.PageFormat = PageFormat.A4;) is not enough.

Code example below. I hope this helps someone.

Code:
// Set the page size to A4 for each page in the PdfDocument
foreach (var page in pdfRenderer.PdfDocument.Pages)
{
    page.Size = PageSize.A4;
}

using (var stream = new MemoryStream())
{
    ...


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 19, 2025 6:48 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1040
Location: CCAA
After reading your first post, I thought you were creating a PDF with DIN A4 pages and were sending it to a printer with Letter pages. It takes a scaling factor of 94% to print DIN A4 on Letter.

Your second post indicates that setting pages to DIN A4 fixes the issue. Now I have no idea what's going on.

Posting just code snippets without further information is not helpful.

With MigraDoc, pages are DIN A4 by default. So setting DIN A4 has no effect as it already is the implicit default setting.
With PDFsharp, pages default to DIN A4 in metric regions and Letter in imperial regions. So, when I read that PDFsharp behaves different than MigraDoc, I assume that regional settings use imperial units.

A complete MCVE using the IssueSubmissionTemplate and/or some sample PDF files might help us understand what is going on.

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

All times are UTC


Who is online

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