diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs index 6999d9048e66c..1a78f2e4ebe7e 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs @@ -22,13 +22,13 @@ public class StandardPrintController : PrintController public override void OnStartPrint(PrintDocument document, PrintEventArgs e) { Debug.Assert(_dc == null && _graphics == null, "PrintController methods called in the wrong order?"); - Debug.Assert(_modeHandle != null); base.OnStartPrint(document, e); // the win32 methods below SuppressUnmanagedCodeAttributes so assertin on UnmanagedCodePermission is redundant if (!document.PrinterSettings.IsValid) throw new InvalidPrinterException(document.PrinterSettings); + Debug.Assert(_modeHandle != null, "_modeHandle should have been set by PrintController.OnStartPrint"); _dc = document.PrinterSettings.CreateDeviceContext(_modeHandle); Interop.Gdi32.DOCINFO info = new Interop.Gdi32.DOCINFO(); info.lpszDocName = document.DocumentName; diff --git a/src/libraries/System.Drawing.Common/tests/Printing/PrintDocumentTests.cs b/src/libraries/System.Drawing.Common/tests/Printing/PrintDocumentTests.cs index 64bff89aaa013..7a96b12c43244 100644 --- a/src/libraries/System.Drawing.Common/tests/Printing/PrintDocumentTests.cs +++ b/src/libraries/System.Drawing.Common/tests/Printing/PrintDocumentTests.cs @@ -23,11 +23,12 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // +using System.IO; using Xunit; namespace System.Drawing.Printing.Tests { - public class PrintDocumentTests + public class PrintDocumentTests : FileCleanupTestBase { private readonly PageSettings _pageSettings = new PageSettings() { @@ -184,6 +185,26 @@ public void EndPrint_SetValue_ReturnsExpected() } } + [ConditionalFact(nameof(CanPrintToPdf))] + public void Print_DefaultPrintController_Success() + { + bool endPrintCalled = false; + var endPrintHandler = new PrintEventHandler((sender, e) => endPrintCalled = true); + using (var document = new PrintDocument()) + { + document.PrinterSettings.PrinterName = PrintToPdfPrinterName; + document.PrinterSettings.PrintFileName = GetTestFilePath(); + document.PrinterSettings.PrintToFile = true; + document.EndPrint += endPrintHandler; + document.Print(); + document.EndPrint -= endPrintHandler; + } + + // File may not have finished saving to disk when Print returns, + // so we check for EndPrint being called instead of file existence. + Assert.True(endPrintCalled); + } + [ActiveIssue("https://github.com/dotnet/runtime/issues/26428")] [ConditionalFact(Helpers.AnyInstalledPrinters, Helpers.IsDrawingSupported)] public void PrintPage_SetValue_ReturnsExpected() @@ -253,6 +274,23 @@ private void AssertDefaultPageSettings(PageSettings pageSettings) Assert.True(pageSettings.PrinterSettings.IsDefaultPrinter); } + private const string PrintToPdfPrinterName = "Microsoft Print to PDF"; + private static bool CanPrintToPdf() + { + if (!PlatformDetection.IsWindows || !PlatformDetection.IsDrawingSupported) + return false; + + foreach (string name in PrinterSettings.InstalledPrinters) + { + if (name == PrintToPdfPrinterName) + { + return true; + } + } + + return false; + } + private class TestPrintController : PrintController { public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e)