Printing a word document from C# – how the **** do you change the paper type???????
You wouldn’t have thought it would be that hard to print word documents from within C# – hook up the project to the Word Interop dlls – open the document and print. Easy.
In fairness – a lot of it is like this – and setting up a printer is pretty easy;
PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = printerName;
word.ActivePrinter = printerName;
word.PrintOut(....)
Even selecting a given tray is manageable – either via the ‘WdPaperTray’ enum – or even searching for it via a string.
But try as I might – I cannot find a way to change the paper type – and by that I mean from ‘Plain’ to ‘Preprinted’.
The basic problem is that I need to print to (say) ‘Tray 1’ which contains ‘Preprinted’ paper – and is set up as so on the printer. Attempting to print a plain paper printout gets stuck at the printer with a Paper type is mismatched error – which is fair enough.
Does anyone know how to do this????
A long time after it was asked but here is how I have just done it.
printerURL is the \printerserverprinter address
paperTrayFirst and paperTrayNext are
“Tray 1” “Tray 2” etc
docSource is the full path/name of the document.
private void DoPrint(string printerURL, string paperTrayFirst, string paperTrayNext, string docSource)
{
PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = printerURL;
pd.DocumentName = docSource;
PaperSource trayToUseFirst = null;
PaperSource trayToUseNext = null;
if(paperTrayFirst == “-“) paperTrayFirst = “Tray 1”;
if(paperTrayNext == “-“) paperTrayNext = “Tray 1”;
foreach (PaperSource source in pd.PrinterSettings.PaperSources)
{
if (source.SourceName.Trim().Equals(paperTrayFirst))
{
trayToUseFirst = source;
}
if (source.SourceName.Trim().Equals(paperTrayNext))
{
trayToUseNext = source;
}
}
//if (pCap.StaplingCapability.Contains(Stapling.StapleDualLeft))
//{
// printTicket.Stapling = Stapling.StapleDualLeft;
//}
int rawKindFirst = Convert.ToInt32(trayToUseFirst.GetType().GetField(“kind”, BindingFlags.Instance | BindingFlags.NonPublic).GetValue(trayToUseFirst));
m_wApp.ActiveDocument.PageSetup.FirstPageTray = (WdPaperTray)rawKindFirst;
int rawKindNext = Convert.ToInt32(trayToUseNext.GetType().GetField(“kind”, BindingFlags.Instance | BindingFlags.NonPublic).GetValue(trayToUseNext));
m_wApp.ActiveDocument.PageSetup.OtherPagesTray = (WdPaperTray)rawKindNext;
object printBackground = false;
m_wApp.PrintOut(ref printBackground, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
An inelegant solution that worked for my on windows 7. Add the printer again to your computer. So you essentially have the same printer added twice with a different name. You may have to add it as a network printer through another machine. Set the default preferences for the new printer to what you want your app to print off.
Then just change the active printer to the printer name with the right default preferences.
WordApp.ActivePrinter = “TrayThreeDefaultPrinter”;
I use this code to change the tray, maybe it could help you.
string _paperSource = “TRAY 2”; // Printer Tray
string _paperName = “8×17”; // Printer paper name
//Tested code comment. The commented code was the one I test, but when
//writing the post I realize that could be done with less code.
//PaperSize pSize = new PaperSize() //Tested code 🙂
//PaperSource pSource = new PaperSource(); //Tested code 🙂
/// Find selected paperSource and paperName.
foreach (PaperSource _pSource in printDoc.PrinterSettings.PaperSources)
if (_pSource.SourceName.ToUpper() == _paperSource.ToUpper())
{
printDoc.DefaultPageSettings.PaperSource = _pSource;
//pSource = _pSource; //Tested code 🙂
break;
}
foreach (PaperSize _pSize in printDoc.PrinterSettings.PaperSizes)
if (_pSize.PaperName.ToUpper() == _paperName.ToUpper())
{
printDoc.DefaultPageSettings.PaperSize = _pSize;
//pSize = _pSize; //Tested code 🙂
break;
}
//printDoc.DefaultPageSettings.PaperSize = pSize; //Tested code 🙂
//printDoc.DefaultPageSettings.PaperSource = pSource; //Tested code 🙂