Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   .NET (https://www.clubdelphi.com/foros/forumdisplay.php?f=17)
-   -   Imprimir con C# (https://www.clubdelphi.com/foros/showthread.php?t=92958)

REHome 27-03-2018 21:51:10

Imprimir con C#
 
Buenas:

Quiero imprimir un folio, una hoja con la consola en C#, no con Windows Form.
Aquí hay un ejemplo pero es con Windows Form en el cual no me interesa.
https://msdn.microsoft.com/es-es/lib...code-snippet-2

He modificado el código así un poco.
Código:

using System;
using System.IO;


namespace Impresora_Consola_01
{
    class Program
    {
        static void Main(string[] args)
        {
            void Printing(string printer)
            {
                try
                {
                    streamToPrint = new StreamReader(@"Hola amigo.");
                    try
                    {
                        printFont = new Font("Arial", 10);
                        PrintDocument pd = new PrintDocument();
                        pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
                        // Specify the printer to use.
                        pd.PrinterSettings.PrinterName = printer;

                        if (pd.PrinterSettings.IsValid)
                        {
                            pd.Print();
                        }
                        else
                        {
                            Console.WriteLine("Printer is invalid.");
                        }
                    }
                    finally
                    {
                        streamToPrint.Close();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
}

Cuando ejecutes el ejecutable, lo que tiene que hacer, buscar una impresora que tenga dispuesta, y imprima solo un folio. El mensaje que imprime una hoja es este:

Cita:

Hola amigo.

Solo te he gastado un folio.
¿Es posible hacer en modo consola o tiene que ser si o si con Windows Form?

Saludos.

blackx5n 28-03-2018 05:35:55

Hola

Si se puede usar la consola para imprimir no es necesario usar windows forms.

El codigo que posteaste esta incompleto.

Seria de esta forma:

Código:

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;

namespace prrooror
{
    class Program
    {
        // Variables Globales
        private Font printFont;
        private StreamReader streamToPrint;
       
        // Funcion Obtiene el objecto Graphics desde PrintPageEventArgs
        // Se genera para cada pagina que se imprima
        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {           
            Graphics g = ev.Graphics;
        }
       
        // Funcion Imprimir
          void Printing(string printer)
          {
                try
                {
                    streamToPrint = new StreamReader(@"Hola amigo.");
                    try
                    {
                        printFont = new Font("Arial", 10);
                        PrintDocument pd = new PrintDocument();
                        pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
                       
                        // Especifica la impresora a usar
                        pd.PrinterSettings.PrinterName = printer;

                        if (pd.PrinterSettings.IsValid)
                        {
                            pd.Print();
                        }
                        else
                        {
                            Console.WriteLine("Impresion Invalida.");
                        }
                    }
                    finally
                    {
                        streamToPrint.Close();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            } // Fin function
       
   
       
        public static void Main(string[] args)
        {
            Console.WriteLine("Modo Consola");
           
            Program o=new Program();
           
            o.Printing("HP LaserJet Pro P1102w");
           
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        } // Fin programa principal
    } // Fin clase
} // Fin namespace

En la funcion tienes que especificar el nombre de la impresora.

Saludos :p


La franja horaria es GMT +2. Ahora son las 12:46:54.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi