Printing windows forms with direct reportviewer on the printer

1

I have an application made in windows forms and reportviewer , my problem is in printing:

  

I need to print directly to the printer without having to open the check box to select the printer

How to do this in c# ?

    private void btnImprimir_Click(object sender, EventArgs e)
    {
        PrintReport();
    }

    public void PrintReport()
    {
        for (int i = 0; i < int.Parse(txtVolume.Text); i++)
        {
            this.EtqTransportePSTableAdapter.Fill_EtqPS(this.EtqTransportePSDataSet.EtqTransportePS, txtNota.Text);
            this.reportViewer1.PrintDialog();
        }
    
asked by anonymous 12.01.2017 / 20:07

2 answers

1

Well let's go ... in my case I did printing on a dot matrix printer .. best way I found was using String Bilder ..

Print Code

private void PrencherArquivo(DataTable dt, NotaFiscal item, ClienteAG cliente)
{
    StringBuilder sb = new StringBuilder();
    sb.Append(((char)27).ToString()).Append("C").AppendLine(((char)18).ToString());
    sb.AppendLine("");
    sb.AppendLine("");
    sb.AppendLine("");
    sb.AppendLine("");
    for (int i = 1; i <= item.volqte; i++)
    {
        sb.AppendLine(cliente.nome);
        sb.AppendLine("");
        sb.AppendLine(cliente.logradouro + "-" + cliente.numeroendereco);
        sb.AppendLine(cliente.bairro + "-" + cliente.mun_ufd_sigla);
        sb.AppendLine("CEP:" + cliente.cep);
        sb.AppendLine("");
        sb.AppendLine("");
        sb.AppendLine(item.numero + "                  " + item.volpeso.ToString("N1"));
        sb.AppendLine("");
        sb.AppendLine("            VOLUME " + i + " DE " + item.volqte);
        if (item.volqte > i)
        {
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("");
        }
        else
        {
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("");
        }
    }
    sb.Append(((char)27).ToString()).AppendLine("@");
    RawPrinterHelper.SendStringToPrinter("EPSON LX-300+ /II", sb.ToString());
}

Printing code.

public static bool SendStringToPrinter(string szPrinterName, string szString)
{
    IntPtr pBytes;
    Int32 dwCount;
    // How many characters are in the string?
    dwCount = szString.Length;
    // Assume that the printer is expecting ANSI text, and then convert
    // the string to ANSI text.
    pBytes = Marshal.StringToCoTaskMemAnsi(szString);
    // Send the converted ANSI string to the printer.
    SendBytesToPrinter(szPrinterName, pBytes, dwCount);
    Marshal.FreeCoTaskMem(pBytes);
    return true;
}
    
30.03.2017 / 15:14
0

Performs the automatic local printing of the file.

public ActionResult ImprimeDocumento(string caminho)//Caminho onde está seu arquivo
    {
        if (!String.IsNullOrEmpty(caminho))
        {

            try
            {
                myPrinters.SetDefaultPrinter("Nome da sua impressora");

                Process process = new Process
                {
                    StartInfo = new ProcessStartInfo
                     {
                         CreateNoWindow = true,
                         Verb = "print",
                         FileName = caminho,
                     },
                };
                process.Start();
            }
            catch (Exception)
            {
                throw;
            }

        }
        return null;

    }
    
14.11.2018 / 14:26