How to put a page to print inside the application code?

4

I need to put a print page inside my code, ie I will not print an existing document, nor the application screen, I want to mount the code with the elements I want.

I have an array and I just want to add text that is in it to my document.

I'm a layman and started like this (I do not 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 make print something to increase the solution and set paper size, insert images and etc ...

I tested it 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);
    }
    
asked by anonymous 08.10.2015 / 21:14

1 answer

1

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

Whenclickingthebutton,makethefollowingcode:

privatevoidBtnImprimir_Click(objectsender,EventArgse){using(PrintDocumentprint=newPrintDocument())using(PrintPreviewDialogdialog=newPrintPreviewDialog()){print.PrintPage+=Print_PrintPage;dialog.Document=print;dialog.ShowDialog();}}privatevoidPrint_PrintPage(objectsender,PrintPageEventArgse){Graphicsg=e.Graphics;Imageimage=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 Print_PrintPage method is in charge of assembling your report on the screen (that is, your Data Array has to be done therein as per code showing the creation of each row), and the end result of that simple report is :

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

    
10.10.2015 / 03:31