XClave (or Chris’ Blog)

Printing a word document from C# – how the **** do you change the paper type???????

2008, January, 4 · 1 Comment

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????

Categories: .NET · C#
Tagged: , , ,

1 response so far ↓

  • JimBob // 2009, September, 23 at 10:00 | Reply

    A long time after it was asked but here is how I have just done it.

    printerURL is the \\printerserver\printer 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);

    }

Leave a Comment