Setting the printer tray when printing an Excel document

I saw a lot of messages about setting up the printer tray in C # for a text document. I need a solution for Excel.

The best solution, if possible, for any document. Some method I can pass the file path and tray.

EDIT So far I have tried the following, but there have been no changes to the printer settings.

PrinterSettings ps = new PrinterSettings(); ps.PrinterName = @"\\localhost\HP-4515n"; var dps = ps.DefaultPageSettings; dps.PaperSource.RawKind = 260; 

OR

 PrinterSettings ps = new PrinterSettings(); ps.PrinterName = @"\\localhost\HP-4515n"; PaperSource psrc = new PaperSource(); psrc.RawKind = 260; psrc.SourceName = "unknown"; dps.PaperSource = psrc; 

EDIT 2

I am hardcoding RawKind, since the tray is somehow not showing up in the documents.

And now, when I type, for example. In an Excel document, I show PrinterDialog, get the name of the selected printer and pass it on to interact with the active property of the Excel printer. But now I need to print a ton of documents, and I need to install the selected printer and its property specifically in the tray programmatically.

+7
c # printing interop
source share
3 answers

@sysboard, I see on the MSDN page in the PrinterSettings class that the DefaultPageSettings property does not have a set method, only a get method. I'm not sure if this is accessible from external classes ... You can look into the PageSettings class , because it has an overloaded constructor that allows you to pass the specified printer, and it has the method set to PaperSource.

+1
source share

You can get available documents using the following code:

 PrintDocument printDoc1 = new PrintDocument(); List<PaperSource> psList = new List<PaperSource>(); PaperSource pkSource; for (int i = 0; i < printDoc1.PrinterSettings.PaperSources.Count; i++) { pkSource = printDoc1.PrinterSettings.PaperSources[i]; psList.Add(pkSource); } 

Now present these parameters to the user and enter information about which paper source to use, say, its first, you can do:

 PrintDocument doc = new PrintDocument(); doc.DefaultPageSettings.PaperSource = psList[0]; doc.Print(); 
+1
source share

Why are you using hardcoding psrc.RawKind = 260; To set RawKind for a paper source, the PaperSourceKind enumeration is available. try the following code

 PrintDocument doc = new PrintDocument(); PaperSource pSource = new PaperSource(); pSource.RawKind = (int)PaperSourceKind.Middle; doc.DefaultPageSettings.PaperSource = pSource; 
0
source share

All Articles