Click here to Skip to main content
14,177,003 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

16.4K views
10 bookmarked
Posted 8 Feb 2017
Licenced CPOL

C# Print Reports/Document on Server/local using Shared/local Printers

, 8 Feb 2017
Rate this:
Please Sign up or sign in to vote.
C# Print Reports/Document on server/local using shared/local printers

Introduction

This tip will give you a brief idea about how to find printers on the network on server/local and send the document to get printed within network. (Print Reports on installed Printer using IP Address and Name)

Background

This tutorial focuses on:

  1. How to find the network printer using printer IP address and Printer Name
  2. If printer is not configured, then show the message as "Printer is not configured with IP Address and Name"
  3. How to print report using network printer runtime

Using the Code

This tutorial is basically focused on how to print the report and not on how to create it.

To create report, check the link...

Pre-requisite: Need to configure the printer on server/local.

Let’s move on to the code.

Step 1: Create New console application.

Step 2: Add System.Printing reference in your project.

Step 3: Use the below code to get the printer name:

using System.Printing;
namespace PrintApp
{
    class Printer
    {
        public string CheckPrinterConfiguration(string printerIP, string printerName)
        {
            var server = new PrintServer();
            var queues = server.GetPrintQueues(new[] 
            { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections });
            string fulllName = queues.Where(q => q.Name == printerName && 
            q.QueuePort.Name == printerIP).Select(q => q.FullName).FirstOrDefault();
            return fulllName;
        }
    }
}
  1. Get All Printers: If you want to take a list of available printers, use EnumeratedPrintQueueTypes.Local. It will get all the shared and local printers. Local printers are ("Send To OneNote 2016", "Microsoft XPS Document Writer"," Microsoft Print to PDF","Fax", etc.)
  2. Installed or Shared Printers: If you want to take installed or shared printers, use EnumeratedPrintQueueTypes.Shared.

To print report, use the below code:

public void PrintReport(string printerIP, string printerName)
        {
            string printerFullName = CheckPrinterConfiguration(printerIP, printerName);

            if (!string.IsNullOrEmpty(printerFullName))
            {
                //create Report Document
                ReportDocument report = new ReportDocument();

                //get report path
                string reportFilePath = Path.Combine(Environment.CurrentDirectory, 
                                        "Reports\\" + rptParam.ReportParameter.ReportName + ".rpt");
                report.Load(reportFilePath);

                //set datasource to report
                report.SetDataSource(data);

                //set Printer name to report
                report.PrintOptions.PrinterName = printerName;

                System.Drawing.Printing.PageSettings page = new System.Drawing.Printing.PageSettings();

                try
                {
                    //print the report
                    report.PrintToPrinter(1, true, 0, 0);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
            {
                Console.WriteLine("Printer is not configured with IP: {0} 
                and Name: {1}", printerIP, printerName);
            }
        }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Amol B Lande
Software Developer (Senior)
India India
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
General[My vote of 1] BAD ARTICLE! Pin
Rodrigo Barros Pascual9-Feb-17 6:40
memberRodrigo Barros Pascual9-Feb-17 6:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190526.1 | Last Updated 8 Feb 2017
Article Copyright 2017 by Amol B Lande
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid