iTextSharp – Add Image from base64 string

Itexhsharp is a popular third-party component for creating PDFs with the .Net framework.

If you’re using Itextsharp for the first time, I would suggest you go through Mike’s series on Itextsharp that helps you get started, and here is the article from the series that shows different ways to add images to Itextsharp.

To add an image from the base64 string, you first need to convert the base64 string into a bytes array, and then you can call the GetInstance method of Itextsharp Image, which accepts bytes array.

Let’s say we want to add the below image to PDF:

sample image

If you doubt whether the base64string for any image is incomplete/corrupt, you can use the imagetobase64 converter and verify generated string. Check out the sample code below to convert base64string to bytes array and add it to PDF.

 private void CreatePDF()
    {     //path you want to store PDF         
        string pdfPath = string.Format(@"E:\PDF\{0}.pdf", DateTime.Now.ToString("yyyy-MM-dd hhmmss"));
        using (FileStream msReport = new FileStream(pdfPath, FileMode.Create))
        {            //step 1            
            using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 140f, 30f))
            {   // step 2                
                PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, msReport);
                //open the stream                 
                pdfDoc.Open(); iTextSharp.text.Image gif = null;
                string base64string = "yourbase 64 string";
                try
                {                    //  Convert base64string to bytes array                    
                    Byte[] bytes = Convert.FromBase64String(base64string);
                    gif = iTextSharp.text.Image.GetInstance(bytes);
                }
                catch (DocumentException dex)
                {                   //log exception here                
                }
                catch (IOException ioex)
                {                    //log exception here                
                }
                gif.Alignment = iTextSharp.text.Image.ALIGN_LEFT;
                gif.Border = iTextSharp.text.Rectangle.NO_BORDER;
                gif.BorderColor = iTextSharp.text.BaseColor.WHITE;
                gif.ScaleToFit(170f, 100f);
                pdfDoc.Add(gif);
                pdfDoc.Close();
            }
        }
    }

If base64string is not valid, it may throw an error, and if you want to continue execution, you want to assign the gif object some value in the Catch block. The sample code will create PDF in portrait mode, but if you want PDF to be in landscape mode, use the Rotate method as shown below. This is a sample code, and you can alter it as needed.

Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 140f, 30f);

Generated PDF will have an image as shown below:

image from base64 string

I hope it helps!


Posted

in

,

by

Comments

6 responses to “iTextSharp – Add Image from base64 string”

  1. Unknown Avatar

    It's works perfect for me!Thank you very much, this help me a lot!!!

  2. Nilesh Thakkar Avatar

    Glad it helped you 🙂

  3. Unknown Avatar

    It helped me a lot… GREAT..

  4. Unknown Avatar

    Thanks a lot 🙂

  5. Jim Green Avatar

    I have used itext's library, but it feels too complicated. To achieve the author's purpose, you can also use CnetSDK's product, which can add image to pdf from base64 string.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: