Search code examples
c#wpfdate-formatcultureprintdialog

PrintDialog altering culture in WPF


We are currently a printing method from within WPF that utilises the standard print dialog. We've began having issues with our security tokens after a print. Debugging showed that the culture is en-GB and date format is dd/MM/yyyy. After the dialog.PrintDocument line the date format returned from DateTime.Now changes to mm/dd/yyyy?

Debugging the current threads culture shows a change from 'en-GB' to '', and as expected the date format is altered to US mm/dd.

I've been unsuccessful finding any information on this so I'm hoping the Hive mind can help!

void PrintPdf_Click(object sender, RoutedEventArgs e)
{ 
    CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture; 
    PrintDialog dialog = new PrintDialog(); 

    if (dialog.ShowDialog() != true) 
        return; 
    Console.WriteLine(DateTime.Now); 
    dialog.PrintDocument(_pdfDocumentView.PrintDocument.DocumentPaginator, "Print"); 
    Console.WriteLine(DateTime.Now); 
    Thread.CurrentThread.CurrentCulture = currentCulture; 
    Console.WriteLine(DateTime.Now); 
}

Solution

  • Problem was down to Syncfusion property accessor for PrintDocument. First thing it does inside there is change the current culture to Invariant! Setting it back afterwards was the only solution due to not being able to spin up a new thread at that point.