View PDF document using printdocument

0

I am making a direct print to the printer using printdocument, now I need to use this document to generate a pdf, and visualize using the pdf of Windows, how do I pass my code to pdf.

follows the code that generates print with printdocumet

private void prt_implaudos_PrintPage(object sender, PrintPageEventArgs e)
    {
        string cabecalho = "CERTIFICADO DE ANÁLISE";
        string referencia = "REFERÊNCIA: ESPECIFICAÇÃO DO FORNECEDOR";
        string resultfrase = "RESULTADO: APROVADO DE ACORDO COM ESPECIFCAÇÕES DO FABRICANTE";
        string data = DateTime.Now.ToString();

        Pen linhaPreta = new Pen(Color.Black, 3);
        Point ponto1 = new Point(30, 185);
        Point ponto2 = new Point(800, 185);

        Point ponto3 = new Point(30, 280);
        Point ponto4 = new Point(800, 280);

        Point ponto5 = new Point(30, 945);
        Point ponto6 = new Point(800, 945);

        Image imagem = Image.FromFile(string.Format("{0}{1}", Application.StartupPath, "\Imagens\logo PScor1.jpg"));
        Image imagem1 = Image.FromFile(string.Format("{0}{1}", Application.StartupPath, "\Imagens\AssinaturaLaudo1.jpg"));

        e.Graphics.DrawString(data, letra, cor, new Point(710, 950));

        e.Graphics.DrawImage(imagem, 50, 0);

        e.Graphics.DrawString(cabecalho, letra3, cor, new Point(280, 0));

        e.Graphics.DrawLine(linhaPreta, ponto1, ponto2);
        e.Graphics.DrawLine(linhaPreta, ponto3, ponto4);
        e.Graphics.DrawLine(linhaPreta, ponto5, ponto6);

        int f = 60;
        foreach (string nota in implaudos1)
        {
            e.Graphics.DrawString(nota, letra, cor, new Point(600, f));
            f += 15;
        }

        int y = 60;
        foreach (string laudops in implaudos)
        {
            e.Graphics.DrawString(laudops, letra, cor, new Point(50, y));
            y += 15;
        }
        int h = 90;
        foreach (string laudos3 in implaudos3)
        {
            e.Graphics.DrawString(laudos3, letra, cor, new Point(600, h));
            h += 15;
        }
        int g = 160;
        foreach (string quimico in implaudos2)
        {
            e.Graphics.DrawString(quimico, letra, cor, new Point(50, g));
            g += 15;
        }
        int b = 190;
        foreach (string manu in manuseio)
        {
            e.Graphics.DrawString(manu, letra4, cor, new Point(50, b));
            b += 15;
        }
        int c = 220;
        foreach (string derra in derramamento)
        {
            e.Graphics.DrawString(derra, letra4, cor, new Point(50, c));
            c += 15;
        }
        int d = 250;
        foreach (string trata in tratamento)
        {
            e.Graphics.DrawString(trata, letra4, cor, new Point(50, d));
            d += 15;
        }
        int x = 290;
        foreach (string caracter in caracteristicas)
        {
            e.Graphics.DrawString(caracter, letra1, cor, new Point(50, x));
            x += 17;
        }
        foreach (string caracter1 in caracteristicas1)
        {
            e.Graphics.DrawString(caracter1, letra2, cor, new Point(50, x));
            x += 20;
        }
        int w = 290;
        foreach (string especific in especificacoes)
        {
            e.Graphics.DrawString(especific, letra1, cor, new Point(280, w));
            w += 17;
        }
        foreach (string especific1 in especificacoes1)
        {
            e.Graphics.DrawString(especific1, letra2, cor, new Point(280, w));
            w += 20;
        }
        int z = 290;
        foreach (string result in resultados)
        {
            e.Graphics.DrawString(result, letra1, cor, new Point(530, z));
            z += 17;
        }
        foreach (string result1 in resultados1)
        {
            e.Graphics.DrawString(result1, letra2, cor, new Point(530, z));
            z += 20;
        }
        int a = 290;
        foreach (string metod in metodos)
        {
            e.Graphics.DrawString(metod, letra1, cor, new Point(680, a));
            a += 17;
        }
        foreach (string metod1 in metodos1)
        {
            e.Graphics.DrawString(metod1, letra2, cor, new Point(680, a));
            a += 20;
        }
        if (Convert.ToInt32(txt_produto.Text) == 002828 && Convert.ToInt32(txt_produto.Text) == 001019)
        {
            int j = 935;
            foreach (string insumos1 in insumos)
            {
                e.Graphics.DrawString(insumos1, letra2, cor, new Point(500, j));
                j += 15;
            }
        }

        e.Graphics.DrawString(referencia, letra2, cor, new Point(50, 950));
        e.Graphics.DrawString(resultfrase, letra2, cor, new Point(50, 965));
        e.Graphics.DrawImage(imagem1, 150, 980);
    }
    
asked by anonymous 04.08.2018 / 02:54

1 answer

0

You'll need an external library to do this, called PDFsharp . It is the one that receives the most praise on the .NET platform. After adding the library to the project, you will have to convert Graphics -> Image -> PDF Document .

This code is tricky as we were not able to redirect Graphics to two graphical instances at the same time as PDF printing and image . So let's implement two Graphics .

I will create a Graphics b that will work on the e.Graphics , one on top of the other, the same code:

private void prt_implaudos_PrintPage(object sender, PrintPageEventArgs e)
{
    // imagem que será convertida para o PDF
    Bitmap bmp = new Bitmap(e.Graphics.DpiX, e.Graphics.DpiY);

    // gráficos da imagem do PDF
    Graphics bg = Graphics.FromImage(bmp);

    string cabecalho = "CERTIFICADO DE ANÁLISE";
    string referencia = "REFERÊNCIA: ESPECIFICAÇÃO DO FORNECEDOR";
    string resultfrase = "RESULTADO: APROVADO DE ACORDO COM ESPECIFCAÇÕES DO FABRICANTE";
    string data = DateTime.Now.ToString();

    Pen linhaPreta = new Pen(Color.Black, 3);
    Point ponto1 = new Point(30, 185);
    Point ponto2 = new Point(800, 185);

    Point ponto3 = new Point(30, 280);
    Point ponto4 = new Point(800, 280);

    Point ponto5 = new Point(30, 945);
    Point ponto6 = new Point(800, 945);

    Image imagem = Image.FromFile(string.Format("{0}{1}", Application.StartupPath, "\Imagens\logo PScor1.jpg"));
    Image imagem1 = Image.FromFile(string.Format("{0}{1}", Application.StartupPath, "\Imagens\AssinaturaLaudo1.jpg"));

    e.Graphics.DrawString(data, letra, cor, new Point(710, 950));
    bg.DrawString(data, letra, cor, new Point(710, 950));

    e.Graphics.DrawImage(imagem, 50, 0);
    bg.DrawImage(imagem, 50, 0);

    e.Graphics.DrawString(cabecalho, letra3, cor, new Point(280, 0));
    bg.DrawString(cabecalho, letra3, cor, new Point(280, 0));

    e.Graphics.DrawLine(linhaPreta, ponto1, ponto2);
    e.Graphics.DrawLine(linhaPreta, ponto3, ponto4);
    e.Graphics.DrawLine(linhaPreta, ponto5, ponto6);

    bg.DrawLine(linhaPreta, ponto1, ponto2);
    bg.DrawLine(linhaPreta, ponto3, ponto4);
    bg.DrawLine(linhaPreta, ponto5, ponto6);

    int f = 60;
    foreach (string nota in implaudos1)
    {
        e.Graphics.DrawString(nota, letra, cor, new Point(600, f));
        bg.DrawString(nota, letra, cor, new Point(600, f));
        f += 15;
    }

    int y = 60;
    foreach (string laudops in implaudos)
    {
        e.Graphics.DrawString(laudops, letra, cor, new Point(50, y));
        bg.DrawString(laudops, letra, cor, new Point(50, y));
        y += 15;
    }
    int h = 90;
    foreach (string laudos3 in implaudos3)
    {
        e.Graphics.DrawString(laudos3, letra, cor, new Point(600, h));
        bg.DrawString(laudos3, letra, cor, new Point(600, h));
        h += 15;
    }
    int g = 160;
    foreach (string quimico in implaudos2)
    {
        e.Graphics.DrawString(quimico, letra, cor, new Point(50, g));
        bg.DrawString(quimico, letra, cor, new Point(50, g));
        g += 15;
    }
    int b = 190;
    foreach (string manu in manuseio)
    {
        e.Graphics.DrawString(manu, letra4, cor, new Point(50, b));
        bg.DrawString(manu, letra4, cor, new Point(50, b));
        b += 15;
    }
    int c = 220;
    foreach (string derra in derramamento)
    {
        e.Graphics.DrawString(derra, letra4, cor, new Point(50, c));
        bg.DrawString(derra, letra4, cor, new Point(50, c));
        c += 15;
    }
    int d = 250;
    foreach (string trata in tratamento)
    {
        e.Graphics.DrawString(trata, letra4, cor, new Point(50, d));
        bg.DrawString(derra, letra4, cor, new Point(50, c));
        d += 15;
    }
    int x = 290;
    foreach (string caracter in caracteristicas)
    {
        e.Graphics.DrawString(caracter, letra1, cor, new Point(50, x));
        bg.DrawString(caracter, letra1, cor, new Point(50, x));
        x += 17;
    }
    foreach (string caracter1 in caracteristicas1)
    {
        e.Graphics.DrawString(caracter1, letra2, cor, new Point(50, x));
        bg.DrawString(caracter1, letra2, cor, new Point(50, x));
        x += 20;
    }
    int w = 290;
    foreach (string especific in especificacoes)
    {
        e.Graphics.DrawString(especific, letra1, cor, new Point(280, w));
        bg.DrawString(especific, letra1, cor, new Point(280, w));
        w += 17;
    }
    foreach (string especific1 in especificacoes1)
    {
        e.Graphics.DrawString(especific1, letra2, cor, new Point(280, w));
        bg.DrawString(especific1, letra2, cor, new Point(280, w));
        w += 20;
    }
    int z = 290;
    foreach (string result in resultados)
    {
        e.Graphics.DrawString(result, letra1, cor, new Point(530, z));
        bg.DrawString(result, letra1, cor, new Point(530, z));
        z += 17;
    }
    foreach (string result1 in resultados1)
    {
        e.Graphics.DrawString(result1, letra2, cor, new Point(530, z));
        bg.DrawString(result1, letra2, cor, new Point(530, z));
        z += 20;
    }
    int a = 290;
    foreach (string metod in metodos)
    {
        e.Graphics.DrawString(metod, letra1, cor, new Point(680, a));
        bg.DrawString(metod, letra1, cor, new Point(680, a));
        a += 17;
    }
    foreach (string metod1 in metodos1)
    {
        e.Graphics.DrawString(metod1, letra2, cor, new Point(680, a));
        bg.DrawString(metod1, letra2, cor, new Point(680, a));
        a += 20;
    }
    if (Convert.ToInt32(txt_produto.Text) == 002828 && Convert.ToInt32(txt_produto.Text) == 001019)
    {
        int j = 935;
        foreach (string insumos1 in insumos)
        {
            e.Graphics.DrawString(insumos1, letra2, cor, new Point(500, j));
            bg.DrawString(insumos1, letra2, cor, new Point(500, j));
            j += 15;
        }
    }

    e.Graphics.DrawString(referencia, letra2, cor, new Point(50, 950));
    e.Graphics.DrawString(resultfrase, letra2, cor, new Point(50, 965));
    e.Graphics.DrawImage(imagem1, 150, 980);

    bg.DrawString(referencia, letra2, cor, new Point(50, 950));
    bg.DrawString(resultfrase, letra2, cor, new Point(50, 965));
    bg.DrawImage(imagem1, 150, 980);
}

Okay, in the code above we convert your entire code to a Bitmap. Now let's convert Bitmap to PDF.

PdfDocument doc = new PdfDocument(); // cria o documento pdf
doc.Pages.Add(new PdfPage()); // adiciona uma nova página ao PDF
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]); // cria os gráficos da página
XImage img = XImage.FromImage(bmp); // extrai uma imagem do bitmap criado

xgr.DrawImage(img, 0, 0); // escreve a imagem no PDF na posição 0, 0
doc.Save("C:\PDF_gerado.pdf"); // salva o PDF para este caminho
doc.Close(); // fecha o arquivo
04.08.2018 / 06:44