Convert and Base64 Encode Images and PDFs in C#

Base64 encoding is used to encode binary data, such as a PDF file, into an ASCII string format that is compatible with systems that can only handle text. For example, email attachments and binary uploads in HTML forms are converted and transmitted as Base64 encoded data.

The C# code snippet below shows how easy it is to save as any LEADTOOLS image format, including PDF, with Base64 encoding.

byte[] bytes;
using (var codecs = new RasterCodecs())
using (var memStream = new System.IO.MemoryStream())
	// save the image as image-based color JPEG compressed PDF file
	// see:
	codecs.Save(e.Image, memStream, RasterImageFormat.RasPdfJpeg, 24);
	bytes = memStream.ToArray();
var base64 = Convert.ToBase64String(bytes);

The C# code snippet below shows how to use the Document Converter to save any LEADTOOLS format as a text-searchable PDF, with Base64 encoding.

// Use DocumentConverter to convert image to text searchable PDF
// Then base64 encode the PDF

byte[] bytes;
using (var memStream = new System.IO.MemoryStream())
using (var ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
	// see:
	ocrEngine.Startup(null, null, null, null); 
	var leadDocument = DocumentFactory.LoadFromFile(file, new LoadDocumentOptions()); 
	var jobData = new DocumentConverterJobData 
		Document = leadDocument, 
		OutputDocumentStream = memStream, 
		// See:
		DocumentFormat = Leadtools.Document.Writer.DocumentFormat.Pdf 

	var documentWriter = new DocumentWriter(); 
	// Get the current PDF options  
	PdfDocumentOptions pdfOptions = documentWriter.GetOptions(DocumentFormat.Pdf) as PdfDocumentOptions; 
	// Set our options  
	pdfOptions.ImageOverText = true; 
	documentWriter.SetOptions(DocumentFormat.Pdf, pdfOptions); 

	// Use DocumentConverter to convert and save into a stream 
	// see:
	var docConverter = new DocumentConverter(); 
	docConverter.SetOcrEngineInstance(ocrEngine, false); 
	var job = docConverter.Jobs.CreateJob(jobData); 
	bytes = memStream.ToArray();
var base64 = Convert.ToBase64String(bytes);

Be sure to check out the LEADTOOLS documentation for comprehensive tutorials that guide you step-by-step to complete useful tasks, while shortening the learning curve.

No matter the type of PDF file — image or text — it is very easy to convert, save, and encode with Base64 encoding using LEADTOOLS.

See For Yourself – Free Evaluation

Download the LEADTOOLS SDK for free. It’s fully-functional for 60 days and comes with free chat and email support.

Stay Tuned For More Conversion Samples

Did you see our previous post, “C#, VB, and Java Code to Convert PDF to JPEG”? Stay tuned for more conversion examples to see how LEADTOOLS easily fits into any workflow converting PDF files into other document files or images and back again. Need help in the meantime? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team via email or call us at +1-704-332-5532.


Developer Advocate

    Find more about me on:
  • linkedin
  • twitter
  • youtube
This entry was posted in Document Converter and tagged , , , , . Bookmark the permalink.

Leave a Reply

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