View document in pdf using printdocument

Asked

Viewed 84 times

0

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

follow the code I 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);
    }

1 answer

0

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

In this code is complicated, because we can not redirect the Graphics for two graphical instances at the same time that would be in the case printing and PDF image. So let’s implement two Graphics.

I will create a Graphics b that will work in pararelo to the e.Graphics, on top of each 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);
}

Ready, 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

  • Thank you so much for the attention Cypher, sorry for the mess of the code, I’m a beginner yet, but in your code above I’m wrong in two parts this request to convert to string and float (Bitmap bmp = new Bitmap(e.Graphics.Dpix, e.Graphics.Dpiy); and this other image is not finding (Ximage img = Ximage.Fromimage(bmp);

  • You can’t find Fromimage(bmp).

Browser other questions tagged

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