PDFsharp & MigraDoc Forum
https://forum.pdfsharp.net/

Safari print scaling at 93% with MigraDoc
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4810
Page 1 of 1

Author:  zooberoo [ Sun Mar 16, 2025 9:39 pm ]
Post subject:  Safari print scaling at 93% with MigraDoc

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

Author:  zooberoo [ Mon Mar 17, 2025 10:15 pm ]
Post subject:  Re: Safari print scaling at 93% with MigraDoc

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())
{
    ...

Author:  TH-Soft [ Wed Mar 19, 2025 6:48 am ]
Post subject:  Re: Safari print scaling at 93% with MigraDoc

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.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/