Solved

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

Posted on 2007-11-23
7
12,223 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

911 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now