Click here to Skip to main content
15,796,333 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.
Posted

1 solution

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

C#
// 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.
document.Add(image);
 
Share this answer
 

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