Solved

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

Posted on 2007-11-23
7
12,163 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 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

9 Experts available now in Live!

Get 1:1 Help Now