Learn how to Convert PDF to Base64 in C# using Aspose.PDF for .NET
Created
October 17, 2025 04:09
-
-
Save aspose-com-gists/0c49f1a4095c6e49ec7fa94429430414 to your computer and use it in GitHub Desktop.
Convert PDF to Base64 in C# using Aspose.PDF for .NET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Aspose.Pdf; | |
using Aspose.Pdf.Text; | |
// 1) Load the PDF | |
using var doc = new Document("sample_pdf.pdf"); | |
// Optionally perform edits here, e.g., optimize, redact, secure, etc. | |
// 2) Save to memory | |
using var ms = new MemoryStream(); | |
doc.Save(ms); | |
var bytes = ms.ToArray(); | |
// 3) Convert to Base64 | |
var base64 = Convert.ToBase64String(bytes); | |
Console.WriteLine(base64.Substring(0, Math.Min(base64.Length, 120)) + "..."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using Aspose.Pdf; | |
// Open the source PDF from a file path | |
using var src = new Document("sample_pdf.pdf"); | |
// Create a new empty PDF that will contain only the selected page(s) | |
using var sub = new Document(); | |
// Copy the requested page into the new document | |
sub.Pages.Add(src.Pages[1]); | |
// Save the one-page PDF to memory instead of disk | |
using var ms = new MemoryStream(); | |
sub.Save(ms); | |
// Convert the in-memory PDF bytes to a Base64 string | |
var base64 = Convert.ToBase64String(ms.ToArray()); | |
// Print a short preview of the Base64 text to the console | |
Console.WriteLine(base64.Substring(0, Math.Min(base64.Length, 120)) + "..."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using Aspose.Pdf; | |
// 1) Create a new PDF document in memory | |
var pdf = new Document(); | |
// 2) Add a page and some sample content | |
var page = pdf.Pages.Add(); | |
var header = new TextFragment("Hello from Aspose.PDF"); | |
header.TextState.FontSize = 18; | |
header.TextState.FontStyle = FontStyles.Bold; | |
page.Paragraphs.Add(header); | |
var body = new TextFragment("This PDF was generated in memory and then converted to Base64."); | |
body.TextState.FontSize = 12; | |
page.Paragraphs.Add(body); | |
// 3) Save to a MemoryStream | |
using var ms = new MemoryStream(); | |
pdf.Save(ms); | |
byte[] bytes = ms.ToArray(); | |
// 4) Convert to Base64 string | |
string base64 = Convert.ToBase64String(bytes); | |
// 5) Optionally prepare a data URI for browsers or embed in JSON | |
string dataUri = $"data:application/pdf;base64,{base64}"; | |
// Output for demo purposes | |
Console.WriteLine("Base64 length: " + base64.Length); | |
Console.WriteLine("Data URI sample (truncated): " + dataUri.Substring(0, Math.Min(dataUri.Length, 80)) + "..."); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using Aspose.Pdf; | |
// Read Base64 text from a file and trim extra whitespace or newlines | |
using System; | |
using System.IO; | |
using Aspose.Pdf; | |
// Read the text file | |
string base64 = File.ReadAllText("sample-base64.txt").Trim(); | |
// Optional: handle data URI input like "data:application/pdf;base64,AAAA..." | |
// Detect and strip the prefix so only the raw Base64 remains | |
const string prefix = "data:application/pdf;base64,"; | |
if (base64.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) | |
base64 = base64.Substring(prefix.Length); | |
// Decode the Base64 string into a byte array | |
byte[] pdfBytes = Convert.FromBase64String(base64); | |
// Validate by loading the bytes into Aspose.PDF; this throws if bytes are not a valid PDF | |
using var doc = new Document(new MemoryStream(pdfBytes)); | |
// Persist the recovered PDF to disk | |
doc.Save("output.pdf"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment