How to assemble a page for printing within the application code?

Asked

Viewed 3,589 times

4

I need to assemble a printing page inside my code, that is I will not print an existing document, nor the application screen, I want to assemble the code execution with the elements I want.

I have an array and I just want to add texts that are in it to my document.

I am a layman in this and started like this (I don’t know if it’s the right approach):

    public void Imprimir(Array dados)
    {
        var documento = new System.Drawing.Printing.PrintDocument();

        foreach(var dado in dados)
        {
            // aqui popularia o documento com os textos pra no final so chamar o .Print()
            // não sei como fazer isso
        }

        documento.Print();
    }

At first I just want to print something to increase the solution and define paper size, insert images and etc...

I tested this way and printed the blank page:

    public void Imprimir(Array dados)
    {
        var documento = new System.Drawing.Printing.PrintDocument();

        foreach(var dado in dados)
        {
            documento.PrintPage += meuDocumento;
        }

        documento.Print();
    }

    private void meuDocumento(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.PageUnit = GraphicsUnit.Inch;
        e.Graphics.DrawString("Linha 1", new Font("arial", 10), Brushes.Black, 100, 2);
        e.Graphics.DrawString("Linha 2", new Font("arial", 10), Brushes.Black, 200, 2);
    }

1 answer

2


Create a form with a button to execute the creation and printing procedure on your report screen:

inserir a descrição da imagem aqui

When clicking the button make the following code:

private void BtnImprimir_Click(object sender, EventArgs e)
{
    using (PrintDocument print = new PrintDocument())
    using (PrintPreviewDialog dialog = new PrintPreviewDialog())
    {
        print.PrintPage += Print_PrintPage;
        dialog.Document = print;
        dialog.ShowDialog();
    }

}

private void Print_PrintPage(object sender, PrintPageEventArgs e)
{
    Graphics g = e.Graphics;
    Image image = Image
        .FromFile(string.Format("{0}{1}", 
        Application.StartupPath, 
        "\\Arquivos\\ti.png"));

    using (Font font = new Font("Arial", 16))
    {
        g.DrawString("StackOverFlow", font, Brushes.Black, 10,20);
        g.DrawString("StackOverFlow", font, Brushes.Black, 10, 40);
        g.DrawString("StackOverFlow", font, Brushes.Black, 10, 60);
        g.DrawString("StackOverFlow", font, Brushes.Black, 10, 80);
        g.DrawString("StackOverFlow", font, Brushes.Black, 10, 100);
        g.DrawString("StackOverFlow", font, Brushes.Black, 10, 120);
        g.DrawString("StackOverFlow", font, Brushes.Black, 10, 140);
        g.DrawString("StackOverFlow", font, Brushes.Black, 10, 160);
        g.DrawImage(image, 10, 183);
    }
}

The method Print_PrintPage, is in charge of assembling your report on the screen (i.e., your data array has to be done there as code demonstrates the creation of each line) and the end result of this simple report is:

In that link (C# - Printing in a Windows Forms application) has more settings and you can modernize to your liking ...

inserir a descrição da imagem aqui

  • I can pass other parameters to this method Print_printpage??

  • Print_printpage is a standard Printdocument action method... It’s not a good solution. But it tells you what your idea is, what you want to do ?

  • It is pq as I will assemble the print there I already have a list in the previous method I want to iterate inside the Print_printpage to generate the print document.

  • Call the method within the Print_PrintPage, got it?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.