I am attempting to print from a WCF service using the System.Drawing.Printing library. The problem is that I am attempting to choose a paper type (or media type) of Letterhead, Cardstock, or Pre-printed paper which does not appear to be available in this library.
System.Drawing.Printing has a PageSettings class, but I can only set the PaperSize and there is no PaperSize for Letterhead, Cardstock, Pre-printed, etc. https://msdn.microsoft.com/en-us/library/System.Drawing.Printing.PageSettings(v=vs.110).aspx
Also the PaperSource class from PrinterSettings.PaperSources does not contain any information about what kind of paper is in each tray.
Does anyone have a recommendation how to ensure the print job I send has the correct settings so that the printer will know which tray to print from?
There MUST be a way to do this. For example, I can select Letterhead when printing from Word or Excel, but only when I go to Printer Properties. Why can I not do this programmatically in .NET? Is this a managed code limitation? Do I need to access the printer driver?
Even System.Printing does not have these options available. Also MSDN states:
Caution: Classes within the System.Printing namespace are not supported for use within a Windows service or ASP.NET application or service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions.
The only other option I have available is to have the user manually set up each printer with the kind of paper in each tray in a database with some user interface. Then I would just set the tray to print from. I would like to avoid this if possible.
UPDATE DEC 14, 2015
The printer manufacturer is willing to give a paid solution but that is not feasible for the project at this time.
Rough code solution is:
private PrintJobStatusEnum SendToPrinter(PrintDocumentModel printJob, out List<string> errors)
{
errors = new List<string>();
// The print job includes the printer and page settings
var printerSettings = new PrinterSettings
{
PrinterName = "MyPrinterName",
Duplex = printJob.IsDuplex ? Duplex.Vertical : Duplex.Simplex
};
// Set the paper size
var paperKind = PaperKind.Letter;
// Find the paper size in the available sizes on the printer
var paperSizes = printerSettings.PaperSizes.Cast<PaperSize>().AsQueryable();
var paperSize = paperSizes.FirstOrDefault(p => p.Kind == paperKind);
// Set the paper source (tray)
var paperSources = printerSettings.PaperSources.Cast<PaperSource>().AsQueryable();
// The SourceName is different for many printers.
// Double-check yours through PrinterSettings.PaperSources
var paperSource = paperSources.FirstOrDefault(s => s.SourceName == "Cassette 1");
if (paperSource == null)
{
paperSource = paperSources.FirstOrDefault(s => s.Kind == PaperSourceKind.AutomaticFeed);
}
// Set up the page
var pageSettings = new PageSettings
{
Landscape = printJob.PaperOrientationLookUpId == MyConstants.PaperOrientationLandscape,
Margins = new Margins(0, 0, 0, 0), // Not sure if margins are needed
PaperSize = paperSize ?? new PaperSize("Letter", 850, 1100),
Color = printJob.IsColor,
PaperSource = paperSource,
PrinterSettings = printerSettings
};
// Send document, printer settings and page settings to print handler
List<string> printErrors;
var result = _pdfPrintHandler.Print(printerSettings, pageSettings, printJob, out printErrors);
errors.AddRange(printErrors);
return result;
}