7
votes

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.

3

3 Answers

1
votes

@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.

1
votes

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();
0
votes

Why you are hardcoding psrc.RawKind = 260; To set a RawKind for a Paper Source there is PaperSourceKind enum available. try the following code

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