Click here to Skip to main content
15,850,359 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using C# Razor with the iText7 plugin. I need to add images (stored as Base64 strings in SQL) to a PDF document.

I got as far as creating a byte array from the Base64 string, and creating an iText Element to use for placing the rawimage object on the PDF document. (I tried using CreateBMP but that generates an error saying "not implemented yet")

When I run the code there are no errors, and the PDF file is created in my local folder.

But when I open the PDF the image is not there, or if it is there it is not visible. (the text lines I added on the first page are all there, using list.add)

When I scroll down in the PDF to the second page where the image is supposed to be, I get a generic error in Adobe saying there is an error on the page, contact the PDF creator.

Any thoughts? I can post the code I have if that will help.

What I have tried:

tried dozens of examples from google search results, I have the code pared down to the minimum code that runs without errors and creates the PDF file.
Gary SC59 7-Dec-23 19:20pm    
UPDATE: I was approaching it the wrong way. The Base64 string I have merely needs to be decoded back to a file, not an image or a pdf. I stripped away the header "data:application/pdf;base64," and then I was able to convert the remaining base64 string to a file on the hard drive using System.IO.File. I was making it more difficult than it needed to be.

1 solution

To be able to insert an image into pdf document, you'll need to convert Base64 string into image:

// convert Base64 string to byte array.
byte[] imageBytes = Convert.FromBase64String(base64Image);
// create image from byte array.
Image image = new Image(imageBytes);
// add image to the document.
Share this answer
Gary SC59 7-Dec-23 19:17pm    
Thank You for replying.
Yes, I have tried that. In order to get the byte[] array object to accept the string I have to remove the "data:application/pdf,Base64," header from the string (which comes that way from an SQL table), otherwise I get an error about non-Base64 characters in the string. But then I never get an image to show up in the pdf body. I opened the pdf in Acrobat and it did show me a big square that looked like a sea of QR codes all pasted together where the image was supposed to be, nothing recognizable as an image. So it's putting something there, but browsers and FoxIt apparently will not render it. I was surprised to see Acrobat show it to me at all.

It is apparently putting something there, but it is not recognized as an image.

I am trying to use iText7, since itextsharp is deprecated and no longer recommended.

using System directives do not have any methods for working with pdf as far as I can tell.
Gary SC59 7-Dec-23 19:22pm    
I am accepting your solution because your comment pointed me in the right direction, even if the specifics were a bit different. Thanks for the input.
Maciej Los 8-Dec-23 2:54am    
You're very welcome and thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900