Articles in this section
Category / Section

How to load PDF document from Base64 string using C# and VB.NET

5 mins read

The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can load and save the PDF document using the Base64 string using C# and VB.NET.

Steps to load and save the PDF document programmatically:

  1. Create a new C# console application project.

Create a new C# console application project

  1. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from Nuget.org.

NuGet package reference

  1. Include the following namespace in Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
using System.Drawing;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Parsing
Imports System.Drawing

 

  1. Use the following code sample to load and save the PDF document using the Base64 string.

C#

//Read the base64 text from the text file
StreamReader reader = new StreamReader(@"../../Data/Base64_String.txt");
string base64String = reader.ReadToEnd();
reader.Close();
 
//Get bytes from the base64 string 
byte[] byteArray = System.Convert.FromBase64String(base64String);
 
//Load PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray);
 
//Get the first page from the document
PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage;
 
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
 
//Use the system installed font
PdfFont font = new PdfStandardFont(PdfFontFamily.Courier, 20);
 
//Draw the text
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
 
//Save the PDF document to the memory stream
MemoryStream stream = new MemoryStream();
loadedDocument.Save(stream);
 
//Close the document
loadedDocument.Close(true);
 
//Convert the saved PDF document to Base64 string
string base64 = Convert.ToBase64String(stream.ToArray());
 
//Write the base64 string to a text file
File.WriteAllText("Test.txt", base64);

 

VB.NET

'Read the base64 text from the text file 
Dim reader As StreamReader = New StreamReader("../../Data/Base64_String.txt")
Dim base64String As String = reader.ReadToEnd()
reader.Close()
 
'Get bytes from base64 string 
Dim byteArray As Byte() = System.Convert.FromBase64String(base64String)
 
'Load PDF document 
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument(byteArray)
 
'Get the first page from the document 
Dim page As PdfLoadedPage = TryCast(loadedDocument.Pages(0), PdfLoadedPage)
 
'Create PDF graphics for the page
Dim graphics As PdfGraphics = page.Graphics
 
'Use the system installed font 
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Courier, 20)
 
'Draw the text 
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, New PointF(0, 0))
 
'Save the PDF document to the memory stream
Dim stream As MemoryStream = New MemoryStream()
loadedDocument.Save(stream)
 
'Close the document 
loadedDocument.Close(True)
 
'Convert the saved PDF document to Base64 string 
Dim base64 As String = Convert.ToBase64String(stream.ToArray())
 
'Write the base64 string to a text file
File.WriteAllText("Test.txt", base64)

 

A complete working sample can be download from PdfSample.zip.

 

Take a moment to peruse the documentation. You can find the options like opening an existing PDF document from the stream, byte array, opening an encrypted and corrupted document.

 

Refer to this link to explore a rich set of Syncfusion Essential® PDF features.

 

Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or the NuGet feed, include a license key in your product. Refer to this link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied