How to print without changing default setting in system.

Raja Priya Meganathan 1 Reputation point
2021-03-03T13:32:03.173+00:00

Hello Team,

The system default print setting will be changing when my customer has used to print a document in my application. Attached the screenshot for the printer setting default screen.

I have used the below code to print my documents in my project, I need to use the below code and print my documents without change the system default printer setting.

In my project, I will print the image in the below code. Any have an idea please share how to print without change the system default print setting.

PrintDocument pd = new PrintDocument();  
        PrintController printController = new StandardPrintController();  
        pd.PrintController = printController;  
        pd.PrinterSettings.PrinterName = LabelPrinter.Text.ToString(); //Printer Name  
        pd.PrinterSettings.Copies = Convert.ToInt16(printqty);  
        pd.DefaultPageSettings.Landscape = false;  
        pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);  
        pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);  
	pd.PrintPage += (sndr, args) =>    
    	{  
        	System.Drawing.Image i = System.Drawing.Image.FromFile(FileName); //image  

		//Adjust the size of the image to the page to print the full image without loosing any part of the image.  
		System.Drawing.Rectangle m = args.MarginBounds;  

		//Logic below maintains Aspect Ratio.  
		if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height) // image is wider  
        	 {  
            		m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width);  
        	 }  

        	else  
        	 {  
            		m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height);  
        	 }  

		//Calculating optimal orientation.  
		pd.DefaultPageSettings.Landscape = m.Width > m.Height;  
        	pd.DefaultPageSettings.Landscape = false;                                                             
		args.Graphics.DrawImage(i, m);  
       };  
       pd.Print();  

73766-screenshot-3.png

Notes:
Code - C# windows application.
System - Windows 10.

Developer technologies Windows Forms
Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2021-03-03T15:05:48.407+00:00

    That's because you are changing the defaults explicitly in code. On line 8 you are getting the default page settings from the printer settings that were copied from the default printer settings. If you want to change the defaults for all pages, but not the system settings, then set the properties on the DefaultPageSettings of PrintDocument, not the DefaultPageSettings of the PrinterSettings.

    //pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);  //Changes default margins for the printer settings
    pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);  //Changes margins for all pages of the current print document
    

    See if that resolves your issue.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.