Skip to content

[Problem/Bug]: CoreWebView2PrintSettings.ScaleFactor is not working for PrintAsync on PDF files #5561

@Fan-iX

Description

@Fan-iX

What happened?

CoreWebView2PrintSettings.ScaleFactor is not working for PrintAsync on PDF pages.

Additional info:

Importance

Blocking. My app's basic functions are not working due to this issue.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

146.0.3856.84

SDK Version

1.0.3912.50

Framework

Winforms

Operating System

Windows 11

OS Version

26200.8037

Repro steps

  1. put Program.cs and winformtest.csproj at a empty winformtest folder
  2. put test.pdf at D:/ (or change the hard-coded file url in Program.cs)
  3. run dotnet publish
  4. launch bin\publish\winformtest.exe
  5. click at Print 1 button, save the pdf as 1.pdf
  6. click at Print 2 button, save the pdf as 2.pdf
  7. 1.pdf and 2.pdf are basically the same. CoreWebView2PrintSettings.ScaleFactor makes no difference here.

original test.pdf
original pdf
Microsoft Print to PDF 1.pdf/2.pdf
print to pdf

Program.cs
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Web.WebView2.WinForms;
using Microsoft.Web.WebView2.Core;

public class Form1 : Form
{
    public Form1()
    {
        SuspendLayout();
        Button btn1 = new Button()
        {
            Text = "Print 1",
            Location = new Point(0, 0),
            Size = new Size(60, 30)
        };
        Button btn2 = new Button()
        {
            Text = "Print 2",
            Location = new Point(60, 0),
            Size = new Size(60, 30)
        };
        Controls.Add(btn1);
        Controls.Add(btn2);
        WebView2 wv = new WebView2()
        {
            Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom,
            Location = new Point(0, 30),
            Size = new Size(ClientSize.Width, ClientSize.Height - 30),
            Source = new Uri("file:///D:/test.pdf")
        };
        Controls.Add(wv);
        ResumeLayout(false);
        PerformLayout();
        btn1.Click += (s, e) =>
        {
            CoreWebView2PrintSettings printSettings = wv.CoreWebView2.Environment.CreatePrintSettings();
            printSettings.PrinterName = "Microsoft Print to PDF";
            printSettings.Orientation = CoreWebView2PrintOrientation.Landscape;
            printSettings.ScaleFactor = 0.5;
            printSettings.MarginLeft = printSettings.MarginRight = 0;
            printSettings.MarginTop = printSettings.MarginBottom = 0;
            wv.CoreWebView2.PrintAsync(printSettings);
        };

        btn2.Click += (s, e) =>
        {
            CoreWebView2PrintSettings printSettings = wv.CoreWebView2.Environment.CreatePrintSettings();
            printSettings.PrinterName = "Microsoft Print to PDF";
            printSettings.Orientation = CoreWebView2PrintOrientation.Landscape;
            printSettings.ScaleFactor = 2.0;
            printSettings.MarginLeft = printSettings.MarginRight = 0;
            printSettings.MarginTop = printSettings.MarginBottom = 0;
            wv.CoreWebView2.PrintAsync(printSettings);
        };
    }
}
public class Program
{
    [STAThread]
    public static void Main()
    {
        Application.Run(new Form1());
    }
}
winformtest.csproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <PlatformTarget>x64</PlatformTarget>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net48</TargetFramework>
    <Configuration>Release</Configuration>
    <PublishDir>bin\Publish</PublishDir>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3912.50" />
  </ItemGroup>
</Project>
test.pdf
%PDF-1.7
1 0 obj
<< /Type /Catalog /Pages 2 0 R >>
endobj
2 0 obj
<< /Type /Pages /Kids [3 0 R] /Count 1 >>
endobj
3 0 obj
<< /Type /Page
   /Parent 2 0 R
   /MediaBox [0 0 595 420]
   /Contents 4 0 R
   /Resources << >>
>>
endobj
4 0 obj
<< /Length 150 >>
stream
1 0 0 rg 0 335 85 85 re f
0 1 0 rg 510 335 85 85 re f
0 0 1 rg 0 0 85 85 re f
1 1 0 rg 510 0 85 85 re f
endstream
endobj
xref
0 5
0000000000 65535 f 
0000000009 00000 n 
0000000058 00000 n 
0000000115 00000 n 
0000000223 00000 n 
trailer
<< /Size 5 /Root 1 0 R >>
startxref
423
%%EOF

Repros in Edge Browser

No, issue does not reproduce in the corresponding Edge version

Regression

Don't know

Last working version (if regression)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions