Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set printer tray when printing excel document

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.

like image 617
sysboard Avatar asked Nov 10 '14 20:11

sysboard


2 Answers

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

like image 96
Jacrys Avatar answered Sep 28 '22 07:09

Jacrys


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();
like image 26
jester Avatar answered Sep 28 '22 05:09

jester