Skip to content

[release/9.0] Fix Color value in PageSettings #13389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Rectangle Bounds
public bool Color
{
get => _color.IsDefault
? _printerSettings.GetModeField(ModeField.Color, (short)DEVMODE_COLOR.DMCOLOR_MONOCHROME) == (short)DEVMODE_COLOR.DMCOLOR_MONOCHROME
? _printerSettings.GetModeField(ModeField.Color, (short)DEVMODE_COLOR.DMCOLOR_MONOCHROME) == (short)DEVMODE_COLOR.DMCOLOR_COLOR
: (bool)_color;
set => _color = value;
}
Expand Down
42 changes: 42 additions & 0 deletions src/System.Drawing.Common/tests/Helpers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Drawing.Printing;
using System.Text;
using Windows.Win32;
Expand All @@ -21,6 +22,47 @@ public unsafe static class Helpers

public static bool AreAnyPrintersInstalled() => s_anyInstalledPrinters;

private const string PrintToPdfPrinterName = "Microsoft Print to PDF";

/// <summary>
/// Checks if PDF printing is supported by verifying installed printers.
/// </summary>
/// <returns><see langword="true"/> if a PDF printer is installed; otherwise, <see langword="false"/>.</returns>
public static bool CanPrintToPdf()
{
foreach (string name in InstalledPrinters)
{
if (name.StartsWith(PrintToPdfPrinterName, StringComparison.Ordinal))
{
return true;
}
}

return false;
}

/// <summary>
/// Attempts to get the name of the PDF printer installed on the system.
/// </summary>
/// <param name="printerName">
/// When this method returns, contains the name of the PDF printer if found; otherwise, <see langword="null"/>.
/// </param>
/// <returns><see langword="true"/> if a PDF printer is found; otherwise, <see langword="false"/>.</returns>
public static bool TryGetPdfPrinterName([NotNullWhen(true)] out string? printerName)
{
foreach (string name in InstalledPrinters)
{
if (name.StartsWith(PrintToPdfPrinterName, StringComparison.Ordinal))
{
printerName = name;
return true;
}
}

printerName = null;
return false;
}

public static string GetTestBitmapPath(string fileName) => GetTestPath("bitmaps", fileName);
public static string GetTestFontPath(string fileName) => GetTestPath("fonts", fileName);
public static string GetTestColorProfilePath(string fileName) => GetTestPath("colorProfiles", fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,21 @@ public void Clone_Success()
Assert.Equal(ps.PaperSource.Kind, clone.PaperSource.Kind);
Assert.Equal(ps.PaperSource.SourceName, clone.PaperSource.SourceName);
}

[ConditionalFact(Helpers.AnyInstalledPrinters)]
public void PrintToPDF_DefaultPageSettings_IsColor()
{
// Regression test for https://github.com/dotnet/winforms/issues/13367
if (!Helpers.TryGetPdfPrinterName(out string? printerName))
{
return;
}

PrinterSettings printerSettings = new()
{
PrinterName = printerName
};

printerSettings.DefaultPageSettings.Color.Should().BeTrue("PDF printer should support color printing.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ public void EndPrint_SetValue_ReturnsExpected()
Assert.False(flag);
}

[ConditionalFact(nameof(CanPrintToPdf))]
[ConditionalFact(typeof(Helpers), nameof(Helpers.CanPrintToPdf))]
public void Print_DefaultPrintController_Success()
{
if (!CanPrintToPdf())
if (!Helpers.TryGetPdfPrinterName(out string? printerName))
{
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would be a bug if CanPrintToPdf is true.

}
Expand All @@ -172,7 +172,7 @@ public void Print_DefaultPrintController_Success()
PrintEventHandler endPrintHandler = new((sender, e) => endPrintCalled = true);
using (PrintDocument document = new())
{
document.PrinterSettings.PrinterName = GetPdfPrinterName();
document.PrinterSettings.PrinterName = printerName;
document.PrinterSettings.PrintFileName = GetTestFilePath();
document.PrinterSettings.PrintToFile = true;
document.EndPrint += endPrintHandler;
Expand Down Expand Up @@ -247,33 +247,6 @@ private void AssertDefaultPageSettings(PageSettings pageSettings)
Assert.True(pageSettings.PrinterSettings.IsDefaultPrinter);
}

private const string PrintToPdfPrinterName = "Microsoft Print to PDF";
private static bool CanPrintToPdf()
{
foreach (string name in Helpers.InstalledPrinters)
{
if (name.StartsWith(PrintToPdfPrinterName, StringComparison.Ordinal))
{
return true;
}
}

return false;
}

private static string GetPdfPrinterName()
{
foreach (string name in Helpers.InstalledPrinters)
{
if (name.StartsWith(PrintToPdfPrinterName, StringComparison.Ordinal))
{
return name;
}
}

throw new InvalidOperationException("No PDF printer installed");
}

private class TestPrintController : PrintController
{
public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e)
Expand Down