Tamil Rk

Tamil Rk

  • NA
  • 442
  • 15.8k

How to print the files without affecting the default printer setting?

Jul 27 2020 3:14 AM
I have checked with the "Let \Windows manage my default Printer" checkbox in Printer & Scanners setting, if I used the below code for printing my document, it is changing the default printer from Printer & Scanners setting.
  1. public void Print(string FileName, int pintprintqty)  
  2. {  
  3. if (string.IsNullOrWhiteSpace(FileName)) return// Prevents execution of below statements if filename is not selected.  
  4. PrintDocument pd = new PrintDocument();  
  5. //Disable the printing document pop-up dialog shown during printing.  
  6. PrintController printController = new StandardPrintController();  
  7. pd.PrintController = printController;  
  8. pd.PrinterSettings.PrinterName = cmbLabelPrinter.Text.ToString();  
  9. pd.PrinterSettings.Copies = Convert.ToInt16(pintprintqty);  
  10. pd.DefaultPageSettings.Landscape = false;  
  11. pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);  
  12. pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);  
  13. pd.PrintPage += (sndr, args) =>  
  14. {  
  15. System.Drawing.Image i = System.Drawing.Image.FromFile(FileName);  
  16. //Adjust the size of the image to the page to print the full image without loosing any part of the image.  
  17. System.Drawing.Rectangle m = args.MarginBounds;  
  18. //Logic below maintains Aspect Ratio.  
  19. if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height) // image is wider  
  20. {  
  21. m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width);  
  22. }  
  23. else  
  24. {  
  25. m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height);  
  26. }  
  27. //Calculating optimal orientation.  
  28. pd.DefaultPageSettings.Landscape = m.Width > m.Height;  
  29. pd.DefaultPageSettings.Landscape = false//New  
  30. args.Graphics.DrawImage(i, m);  
  31. };  
  32. pd.Print();  
  33. }  
How to print the document without changing the default printer setting in "Let Windows manage my default Printer" checkbox checked mode?