0

I'm searching for a way to print a jpeg to the default printer on page post of my asp.net core razorpages web app (the user shouldn't have to navigate the print window, it should automatically print when the page is submitted). I've tried client-side printing with windows.print(), but this isn't suitable because I'm trying to print from a data Url for a jpeg. For server-side printing, Microsoft docs says System.Drawing.Printing should only be used for windows forms and directs to System.Printing. However, using System.Printing is not recognized in the page model, but using System.Drawing.Printing is, and Microsoft docs also says "Classes within the System.Printing namespace are not supported for use within a Windows service or ASP.NET application or service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions."
What is the namespace for an asp.net web application then?

I've been trying the methods suggested by other users, but I can't get the below code to work. The error message says the filename is too long (Visitor.Badge is a data Url), and when I change the file name to a local file just to test the method, the error message says it can't find the file (even though the path is correct for that file). Is printing a jpeg from a data Url even possible? Is there a different way I should be printing or is this just a syntax issue? I'm a novice at asp.net, so I appreciate the help!

private void Print();
{
    ProcessStartInfo info = new ProcessStartInfo();
    info.Verb = "print";
    info.FileName = Visitor.Badge;
    //Testing method with local file:
    //info.FileName = @"C:\Users\JohnSmith\Documents\ExampleFile.jpeg";
    info.CreateNoWindow = true;
    info.WindowStyle = ProcessWindowStyle.Hidden;

    Process p = new Process();
    p.StartInfo = info;
    p.Start();

    p.WaitForInputIdle();
    System.Threading.Thread.Sleep(3000);
    if (false == p.CloseMainWindow())
        p.Kill();
}
PacifismPostMortem
  • 105
  • 1
  • 4
  • 9
  • 1
    _"... I'm trying to print from a data Url for a jpeg"_ you can show the image in an iframe and trigger print. https://stackoverflow.com/questions/9616426/javascript-print-iframe-contents-only/9616706 – abdusco Jul 29 '21 at 16:41
  • (Don't use Yoda expressions (false == thing). Bad C#, that is) – Neil Jul 29 '21 at 16:44
  • @Neil "Don't" is a strong word. There's nothing wrong with it, except that it's not conventional. It might even be better, in fact, because it prevents accidental assignment. `var ok = false; if (ok = true) {...}` this compiles with a warning. Granted you would use `if (ok) { ... }`, but still, `if (true = ok) { ... }` would prevent that bug. – abdusco Jul 29 '21 at 16:56
  • @abdusco Yoda expressions are a hangover from C and C++ and are a very bad habit to bring in to C#. As you say, there is a compiler warning, which alerts you to this, so really is no benefit reason to using it. – Neil Jul 29 '21 at 19:00
  • @Neil Compiler warns for `if (ok = true)` (buggy) not `if (true == ok)`. Yoda conditional is OK. – abdusco Jul 29 '21 at 19:07
  • 1
    you can't trigger a print from client-side anymore. A few years back the browsers removed that functionality. (There may have been people abusing it to print ads or something like that.) You can only open the print dialog in the browser. For server-side stuff you could probably use .NET to print ( https://learn.microsoft.com/en-us/dotnet/api/system.drawing.printing.printdocument.print?view=net-5.0 ) – pcalkins Jul 30 '21 at 19:30

0 Answers0