Solved

Printing a Word Document to a specific printer and tray using C#

Posted on 2007-11-23
7
12,384 Views
Last Modified: 2013-12-16
Hi,

I am trying to write some code that will print a Microsoft Word Document (or in fact any document on the file system) and allow me to specify a network printer and printer tray (and maybe other settings like duplex, etc.) in C#.NET 2.0.

I would have thought this wasn't an unusual request, but I'm finding it rather difficult and can't seem to find any samples that are doing this.

Any help would be gratefully received!!

Thanks in advance!

Nathan.
0
Comment
Question by:BoiledOnion
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 11

Expert Comment

by:Agarici
ID: 20340789
did you try setting the activeprinter on the application?
http://msdn2.microsoft.com/en-us/library/aa168383(office.10).aspx

hth,
A.
0
 

Author Comment

by:BoiledOnion
ID: 20345343
Hi Agarici,

Unfortunately, as far as I am aware, this doesn't allow me to set the tray id, duplex settings, etc. Just the printer name.

Thanks,

Nathan.
0
 
LVL 11

Expert Comment

by:Agarici
ID: 20348598
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:BoiledOnion
ID: 20364051
Hi again Agarici,

Thank you for your response - I had found this link before, but I cannot work out how I would send a word document to it... This is ok for raw data, but not formatted text.

Do you have any thoughts on this?

Thanks again!

Nathan.
0
 
LVL 9

Accepted Solution

by:
WinterMuteUK earned 125 total points
ID: 20450096
Hi Boiled,

I use the following code to print to a given tray and printer, obviously in my case the tray is hard coded, but shouldn't be taxing to change!

Mine does (also) specifically print Word docs via the Word Interop code, not sure how you would cope for images etc, though presumably the trick is still to setup the PrintDocument.

See code snippet.

Cheers
Wint.
    private void Print(_Document wordDoc, string printerName)
    {
        PrintDocument pd = new PrintDocument();
        pd.PrinterSettings.PrinterName = printerName;
        pd.DocumentName = "...Wints' Test Print...";
        PaperSource tray1 = null;
 
        foreach ( PaperSource source in pd.PrinterSettings.PaperSources )
        {
            if( source.SourceName.Trim().Equals( "Tray 1" ) )
            {
                tray1 = source;
                break;
            }
        }
 
        if(tray1 == null)
            return;
 
        int rawKind = Convert.ToInt32( tray1.GetType().GetField( "kind", BindingFlags.Instance | BindingFlags.NonPublic ).GetValue( tray1 ) );
        wordDoc.PageSetup.FirstPageTray = (WdPaperTray) rawKind;
        _wrdApp.ActivePrinter = printerName;
        _wrdApp.Visible = false;
        _wrdApp.PrintOut( ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                          ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
                          ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing );
                   
    }

Open in new window

0
 

Author Closing Comment

by:BoiledOnion
ID: 31410647
Thank you for your input Wint, that was most helpful!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question