How do I put an image in Itextsharp?

0

I'm trying to put an image in the pdf, but the error because the Image class of ItextSharp only accepts Uri, and what I have is an incoming stream that already converted to System.Drawing.Image.

 public static Document GeraPdf(Stream stream)
    {
        Document doc = new Document(PageSize.A4);
        doc.SetMargins(40, 40, 40, 80);
        doc.AddCreationDate();

        string caminho = @"C:\Users\ASUS\Desktop\eae.pdf";

        PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(caminho, FileMode.Create));

        doc.Open();

        string dados = "";
        System.Drawing.Image img1 = System.Drawing.Image.FromStream(stream);

       Image ImagemPdf= Image.GetInstance(img1,ImageFormat.Jpeg);//Aqui esta o Erro


        Paragraph paragrafo = new Paragraph(dados, new Font(Font.NORMAL, 14));

        paragrafo.Alignment = Element.ALIGN_JUSTIFIED;

        paragrafo.Add("TESTE TESTE TESTE");

        doc.Add(paragrafo);

        doc.Close();
        return doc;
    }
    
asked by anonymous 31.01.2018 / 17:31

1 answer

1

It is not only a URI , you can use the input parameter stream itself ... but System.Image.Image should not give error (if mime is correct). I finally noticed that you did not add the image to the document.

   public static Document GeraPdf(Stream stream)
    {
        Document doc = new Document(PageSize.A4);
        doc.SetMargins(40, 40, 40, 80);
        doc.AddCreationDate();

        string caminho = @"C:\temp\eae.pdf";

        PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(caminho, FileMode.Create));

        doc.Open();

        string dados = "";       

        Image ImagemPdf = Image.GetInstance(stream);
        //Configura os atributos que achar pertinente;
        doc.Add(ImagemPdf);

        Paragraph paragrafo = new Paragraph(dados, new Font(Font.NORMAL, 14));

        paragrafo.Alignment = Element.ALIGN_JUSTIFIED;

        paragrafo.Add("TESTE TESTE TESTE");        
        doc.Add(paragrafo);

        doc.Close();
        return doc;
    }
    
31.01.2018 / 20:30