Click here to Skip to main content
15,904,416 members
Articles / Web Development / ASP.NET
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
3.60/5 (11 votes)
8 Feb 2017CPOL1 min read 32.6K   11   1
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:

C#
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:

C#
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)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
General[My vote of 1] BAD ARTICLE! Pin
Rodrigo Barros Pascual9-Feb-17 6:40
Rodrigo 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.