Developers often need to share or store PDF content in text-based formats such as JSON, XML, or HTML attributes. Base64 encoding is also used to transmit files through APIs or save them safely in databases. It provides a reliable way to represent binary PDF data as plain text while preserving every byte for accurate reconstruction later. In this guide, you will learn how to convert a PDF to Base64 in C# and decode it back to a PDF file.
Aspose.PDF for .NET — A Powerful PDF to Base64 Converter in C#
In this article, we use Aspose.PDF for .NET, a powerful library for creating, reading, editing, and converting PDF files in C#. It provides complete control over PDF content and structure without needing Adobe Acrobat or any external tools. With Aspose.PDF, you can easily load existing PDF documents, modify them, apply security settings, and export them to various formats, including Base64-encoded text.
Please download Aspose.PDF for .NET from the releases. You may also install it from NuGet using the following command:
PM> Install-Package Aspose.PDF
Convert PDF to Base64 in C#
You can convert a PDF file to Base64 directly from memory when you need to process the file before encoding. This method lets you open the PDF using the Aspose.Pdf.Document
class, make any changes, and then save it to a MemoryStream
. Finally, you can convert the stream into a Base64 string that can be easily transmitted through APIs or stored in a database.
Follow the steps below to convert a PDF file to a base64 string in C#:
- Load the PDF using the
Document
class. - Save the document to a
MemoryStream
. - Convert the byte array to a Base64 string with the
Convert.ToBase64String()
method. - Use or return the Base64 string.
The following code example shows how to convert PDF to a Base64 string.
Convert Specific Pages of PDF to Base64 in C#
You can convert only part of a PDF document to Base64 when you need to send a smaller portion of the document, such as a single page or a few selected pages. This approach helps reduce the payload size when transmitting data through APIs or other text-based channels.
Follow the steps below to convert a specific page of a PDF to a Base64 string:
- Load the PDF file using the
Document
class. - Create a new
Document
class object to hold extracted pages. - Copy the required pages into the new document.
- Save to
MemoryStream
and encode to Base64.
Create a New PDF Document and Convert to Base64
You can build a fresh PDF in memory and convert it to Base64 without touching the disk. This is useful when you generate documents on the fly and need to send them through an API, embed them in JSON or HTML, or store them in a database. In this method, you create an Aspose.Pdf.Document
, add content, save it to a MemoryStream
, and then turn the bytes into a Base64 string.
Follow the steps below to create a new PDF document and convert it to Base64:
- Create an empty
Document
. - Add a page and place some text with
TextFragment
. - Save the document to a
MemoryStream
. - Convert the stream bytes to a Base64 string.
Convert Base64 to PDF in C#
You can convert a Base64 string back to a PDF file when your application receives encoded data from a client or an API. This is helpful when you need to restore the original document, save it to disk, or load it into the Aspose.Pdf.Document
class for further processing, such as editing, securing, or extracting content.
Follow the steps below to convert a Base64 string into a PDF document:
- Load the encoded string (e.g., from a file or API).
- Convert the string into raw PDF bytes.
- Create a PDF document using the
Document
class object with the decoded bytes. - Save the PDF document.
The following code example shows how to convert a Base64 string back to a PDF document using C#:
Get a Free License
You can explore all the features of Aspose.PDF for .NET without any restrictions by applying for a free temporary license. Visit the temporary license page to get yours and experience the full power of the API.
Convert PDF to Base64 Online for Free
If you need to convert a PDF file to Base64 instantly without writing any code, try this free online PDF to Base64 converter. This browser-based tool lets you upload a PDF and get its Base64-encoded text in seconds. No installation, registration, or additional libraries are required. Everything runs securely in your web browser.

PDF File to Base64 String: Additional Resources
To learn more about working with PDF files and Base64 encoding using Aspose.PDF for .NET, explore the following helpful guides and tools:
- Learn about supported file formats, advanced PDF manipulation features, and detailed API usage examples using Aspose.PDF for .NET Documentation.
- Review the complete class library, methods, and properties for developers working with PDF creation, conversion, and encoding using Aspose.PDF API Reference.
- Try browser-based PDF converters, mergers, compressors, and viewers using Aspose PDF Apps.
- Explore practical examples, how-to articles, and in-depth programming guides on the Aspose.PDF Blog.
PDF to Base64: FAQs
1. What is Base64 encoding, and why is it used with PDF files?
Base64 encoding converts binary data, such as a PDF file, into plain text. It is commonly used to transmit files through text-based systems like JSON, XML, or APIs without data corruption.
2. Can I convert only a specific page of a PDF to Base64?
Yes. Aspose.PDF for .NET lets you extract any page or a range of pages and convert only that portion to Base64 to reduce file size and transmission time.
3. How do I decode a Base64 string back to a PDF file in C#?
You can use the Convert.FromBase64String()
method to get the byte array and then load it into the Aspose.Pdf.Document
class or save it directly to disk as a .pdf
file.
4. Is there a size limit when converting large PDFs to Base64?
There is no fixed limit from Aspose.PDF, but large files require sufficient memory. Using streams instead of loading the entire file into memory helps improve performance.
5. Can I use this method to send a PDF through an API or web service?
Yes. Base64 strings are ideal for embedding PDF data into JSON or XML payloads when transferring files through APIs or web applications.
6. Does Base64 encoding affect PDF quality or content?
No. Base64 encoding only changes how the data is represented. The decoded file will be identical to the original PDF with no loss in quality or information.
7. How do I convert multiple PDF files to Base64 in one go?
You can loop through each file, load it using Aspose.Pdf.Document
, and convert each one to Base64 individually. This works well for batch processing.
8. Can I generate a new PDF and directly convert it to Base64 without saving it to disk?
Yes. You can create a new Document
in memory, add content, save it to a MemoryStream
, and then encode it to Base64, no need to create a physical file.
Conclusion
In this article, you learned how to convert a PDF file to a Base64 string and decode it back to PDF in C# using Aspose.PDF for .NET. This library offers a reliable and feature-rich API for working with PDF documents, making Base64 encoding and decoding simple to implement in any .NET application.
If you have any questions or need assistance, please visit our free support forum, our team is always available to help.