I've seen many posts with regards to setting printer tray in c# for word document. I need a solution for Excel.
A better solution, if possible, for any document. Some kind of method i can pass a file path and the tray.
EDIT So far I've tried the following but no visible changes have been made in 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'm hardcoding RawKind since the tray somehow does not show in the papersources.
And currently when i print eg. Excel document i show the PrinterDialog, get the name of the selected printer and pass it to interop Excel active printer property. But now i need to print mass of documents and i need to set the selected printer and it's property specially the tray programmatically.
@sysboard, I see from the MSDN page on the PrinterSettings class that the DefaultPageSettings property does not have a set method, only a get method. I am not sure that this is accessible from external classes...You might look into the PageSettings class as it looks like it has an overloaded constructor that allows you to pass a specified printer, and it has a set method on PaperSource.
You can get the available papersources using the folowing 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 options to the user and get the input as to which paper source to use, say its the first one, you can do :
PrintDocument doc = new PrintDocument();
doc.DefaultPageSettings.PaperSource = psList[0];
doc.Print();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With