Report from a form, without a database

0

My question is, can I generate a report (I do not know if I can call it that) or proof that it is something like this:

Report: Payment date
Client: xxxxxxxxx xxxxxx xxxx
Date of payment: xx / xx / xxxx

Signature


It would be a pretty simple layout basically this I did above without using the database anyway, it would fill in the values and print what is filled in and order.

The layout of the program is this:

HowcanIdothis?Andifitispossible,thenIresearchedsomeandIdidnotquiteunderstand,sincemyVisualStudiodoesnothaveCrystalReports

UPDATE

ClassImpression.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Drawing.Printing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceImpressao{publicstaticclassFuncoes{///<summary>///Geraaimpressãodeumtexto///</summary>///<paramname="_textoImpressao">string para impressão</param>
        /// <param name="_font">Fonte da impressão</param>
        /// <param name="_impressora">Nome da impressora, informar null ou "" para padrão do windows</param>
        public static void ImprimirString(string _textoImpressao, Font _font, string _impressora)
        {
            string[] linhas = _textoImpressao.Split('\n');
            Queue<string> filaLinhas = new Queue<string>();
            foreach (string l in linhas)
            {
                filaLinhas.Enqueue(l);
            }

            PrintDocument p = new PrintDocument();
            p.PrintPage += delegate (object sender1, PrintPageEventArgs ev)
            {
                Font printFont = _font;
                float linesPerPage = 0;
                float yPos = 0;
                int count = 0;
                float leftMargin = ev.MarginBounds.Left;
                float topMargin = ev.MarginBounds.Top;
                string line = null;

                //Calcular o número de linhas por página
                linesPerPage = ev.MarginBounds.Height /
                   printFont.GetHeight(ev.Graphics);

                // Imprime cada linha do texto
                while (count < linesPerPage && filaLinhas.Count > 0)
                {
                    line = filaLinhas.Dequeue();
                    yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                    ev.Graphics.DrawString(line, printFont, Brushes.Black, 0, yPos, new StringFormat());
                    count++;
                }

                // Se existir mais linhas, gera outra página.
                if (line != null && filaLinhas.Count > 0)
                    ev.HasMorePages = true;
                else
                    ev.HasMorePages = false;
            };

            PrintDialog diag = new PrintDialog();
            diag.Document = p;
            diag.PrinterSettings.PrinterName = _impressora;
            if (diag.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                p.Print();
            }
        }
    }
}

Generate print button: (I try to call the class but do not think)

 private void button1_Click(object sender, EventArgs e)
        {
            Impressao b = new Impressao();
            DateTime dataPagamento = DateTime.Now;
            string clienteNome = "Fulano da Silva Sauro";
            string texto = "Relatório de Pagamento" + "\n";
            texto += "Cliente: " + clienteNome + "\n";
            texto += "Data Pagamento: " + dataPagamento.ToString("dd/MM/yyyy HH:mm");

            FuncoesImprimir.ImprimirString(texto, new Font("Consolas", 12f, FontStyle.Regular), null);
        }

    
asked by anonymous 30.10.2017 / 19:22

1 answer

4

Thinking about a simple, text-only print and that no additional crystal reports are needed, you can use this function that generates the printout by C # itself. Just enter the text that will be printed, the font, and the printer that will be used to print (optional):

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SeuNameSpace
{
    public static class Funcoes
    {
        /// <summary>
        /// Gera a impressão de um texto
        /// </summary>
        /// <param name="_textoImpressao">string para impressão</param>
        /// <param name="_font">Fonte da impressão</param>
        /// <param name="_impressora">Nome da impressora, informar null ou "" para padrão do windows</param>
        public static void ImprimirString(string _textoImpressao, Font _font, string _impressora)
        {
            string[] linhas = _textoImpressao.Split('\n');
            Queue<string> filaLinhas = new Queue<string>();
            foreach (string l in linhas)
            {
                filaLinhas.Enqueue(l);
            }

            PrintDocument p = new PrintDocument();
            p.PrintPage += delegate(object sender1, PrintPageEventArgs ev)
            {
                Font printFont = _font;
                float linesPerPage = 0;
                float yPos = 0;
                int count = 0;
                float leftMargin = ev.MarginBounds.Left;
                float topMargin = ev.MarginBounds.Top;
                string line = null;

                //Calcular o número de linhas por página
                linesPerPage = ev.MarginBounds.Height /
                   printFont.GetHeight(ev.Graphics);

                // Imprime cada linha do texto
                while (count < linesPerPage && filaLinhas.Count > 0)
                {
                    line = filaLinhas.Dequeue();
                    yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                    ev.Graphics.DrawString(line, printFont, Brushes.Black, 0, yPos, new StringFormat());
                    count++;
                }

                // Se existir mais linhas, gera outra página.
                if (line != null && filaLinhas.Count > 0)
                    ev.HasMorePages = true;
                else
                    ev.HasMorePages = false;
            };

            PrintDialog diag = new PrintDialog();
            diag.Document = p;
            diag.PrinterSettings.PrinterName = _impressora;
            if (diag.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                p.Print();
            }
        }
    }
}

To use the code:

        DateTime dataPagamento = DateTime.Now;
        string clienteNome = "Fulano da Silva Sauro";
        string texto = "Relatório de Pagamento" + "\n";
        texto += "Cliente: " + clienteNome + "\n";
        texto += "Data Pagamento: " + dataPagamento.ToString("dd/MM/yyyy HH:mm");

        FuncoesImprimir.ImprimirString(texto, new Font("Consolas", 12f, FontStyle.Regular), null);
    
30.10.2017 / 19:33