Save Byte Array to PDF C# (Code Example)

PDF documents are one of the most popular formats for distributing documents over the internet. A few major advantages of PDFs are that they can be viewed on any device and contain information in a convenient format that is easy to read. Additionally, PDF documents can have many different forms of content including text, images, charts, and graphs. This is why PDFs are so popular among professionals and businesses for document distribution, especially for technical or long-form content.

A byte is a fundamental data type of information in computing and telecommunications. It represents a single character of text or a numerical value with a maximum of 255. The term octet is also used to refer to an 8-bit byte. The byte can represent 256 values, ranging from 0 to 255. This range includes the standard ASCII codes for most English letters, numbers, and symbols, as well as printable symbols like those on a typical keyboard. In text convention (basic output), the range is usually extended to include the space character and many more graphical symbols. This is referred to as the computer's character set or code page (the 8-bit form). In this article, we will see how we can convert the byte array to a PDF file using the IronPDF .NET library.

Topics Covered in Tutorial

We will cover the following topics in this tutorial:

  • IronPDF
  • Steps to Create PDF Files Programmatically
  • Step 1: Create a C# Project
  • Step 2: Install the IronPDF Library
    • Method 1: NuGet Package Manager Console
    • Method 2: Using the DLL File
  • Step 3: Add the IronPDF Namespace
  • Step 4: Convert Byte Array to PDF File
    • Output
  • Conclusion

IronPDF: C# PDF Library

IronPDF is a .NET library that makes reading, manipulating, and writing PDF documents easy. You can use it in your .NET projects for desktop and web applications. IronPDF is available for free for development purposes. This library allows you to convert HTML to PDF, extract text from a PDF, and more. IronPDF is a lightweight .NET PDF library designed specifically with web developers in mind. It is written entirely in C#. IronPDF contains an extensive set of functions that can be used to work with PDF files in a programming environment. Functions are created to improve the workflow and provide access to every aspect of PDF. These include creating, opening, deleting, adding pages, and extracting text from PDF documents. The IronPDF library has many useful features:

  • Reading and writing PDFs using the latest standards.
  • Printing to any Windows printer.
  • Extracting pages from PDF files.
  • Converting HTML content to PDF files with CSS styling applied to it.
  • Adding watermarks or text overlays on the pages of the document.

Let's start with how we can use the IronPDF library to convert the byte array to a PDF file.

Steps to Convert Byte Array to PDF:

We will start with a C# project in Visual Studio. Select any C# application template according to your project needs. For this quick tutorial, I will use the Console Application Visual Studio template. You can modify the already existing C++ project file.

Step 1: Create a C# Project

Use the latest version of Visual Studio for creating the C# project. Follow the provided steps to create a C# project.

  • Open the Visual Studio 2022 application on your machine.
  • Create a C# console application or modify an already existing C# application.
  • Give an appropriate name according to your project.
  • Select the .NET Framework 6.0, the latest framework that comes with long support. You can choose any other framework according to requirements but the latest version is strongly recommended.

By following the above steps, a C# project will be created. Now it's time to install the IronPDF library.

Step 2: Install the IronPDF Library

IronPDF is easy to install too. You get a fast and efficient installation process, cutting down development time and removing the confusion that usually accompanies it.

Method 1: NuGet Package Manager Console

Installing the IronPDF Library with Package Manager Console is very easy. Once you have the console open, type the following command, hit enter and it will be done!

Install-Package IronPDF

Method 2: Using a DLL file

To facilitate a more seamless installation process for this library and to give you a good understanding of how to use it, we recommend you download the DLL file from here. Installation instructions can be found at this link.

Step 3: Add the IronPDF Namespace

Although IronPDF is essential for our project, we need to import its namespace into every code file to use it. To do that, add the following line of code at the top of each file:

using IronPDF;
using IronPDF;
Imports IronPDF
VB   C#

Step 4: Convert Byte Array to PDF Document

As IronPDF is ready to use, now we can start converting the byte array to a PDF file. Write the following code into your program file:

using IronPdf;
using System.Text;

// Converting an array of bytes into a PDF file.

byte[] byteArray = { 84, 104, 105, 115, 32, 105, 115, 32, 98, 121, 116, 101, 32, 97, 114, 114, 97, 121};

// Storing encoded bytes into ASCII literals in a string variable
string data = Encoding.ASCII.GetString(byteArray);
string HTML = $"<h1>{data}</h1>";

var Renderer = new IronPdf.ChromePdfRenderer();
using var PDF = Renderer.RenderHtmlAsPdf(HTML);
PDF.SaveAs("bytearray.pdf");
using IronPdf;
using System.Text;

// Converting an array of bytes into a PDF file.

byte[] byteArray = { 84, 104, 105, 115, 32, 105, 115, 32, 98, 121, 116, 101, 32, 97, 114, 114, 97, 121};

// Storing encoded bytes into ASCII literals in a string variable
string data = Encoding.ASCII.GetString(byteArray);
string HTML = $"<h1>{data}</h1>";

var Renderer = new IronPdf.ChromePdfRenderer();
using var PDF = Renderer.RenderHtmlAsPdf(HTML);
PDF.SaveAs("bytearray.pdf");
Imports IronPdf
Imports System.Text

' Converting an array of bytes into a PDF file.

Private byteArray() As Byte = { 84, 104, 105, 115, 32, 105, 115, 32, 98, 121, 116, 101, 32, 97, 114, 114, 97, 121}

' Storing encoded bytes into ASCII literals in a string variable
Private data As String = Encoding.ASCII.GetString(byteArray)
Private HTML As String = $"<h1>{data}</h1>"

Private Renderer = New IronPdf.ChromePdfRenderer()
Private PDF = Renderer.RenderHtmlAsPdf(HTML)
PDF.SaveAs("bytearray.pdf")
VB   C#

The above code is converting the byte array to PDF. At first, we must have a byte array with content. After that, we'll convert the byte array to string using ASCII.GetString function. By using this method, we'll be able to extract text from a byte array. Now we will use this data to convert PDF file. After extracting the text, we create an HTML string using the extracted data. Once we create an HTML String, we initiate Chrome PDF Renderer. Finally, we call the RenderHtmlAsPdf function to convert PDF files and create a new document.

Now we can save the resultant PDF file doc in our system by using the SaveAs function. We use the source image file with the loaded image stream Bitmap to convert to the byte array. This step will enable us to use the features of the IronPDF library.

Output

This is the generated PDF file contents.

Conclusion

In this article, we have seen how we can easily extract text from PDF files using IronPDF .NET PDF library. This was accomplished with only writing a few lines of code and with a high percentage of accuracy. Additionally, IronPDF also has many useful features such as HTML to PDF conversion, PDF formatting tools, and numerous more core PDF features which are imperative for PDF editing. IronPDF also eliminates the dependency on Adobe Acrobat.

IronPDF is free for development purposes and also offers a 30-day trial for production testing. IronPDF provides a variety of pricing plans which you can get according to your needs. The price of IronPDF is relatively significantly less than its competitors. With pricing variating from individual to large companies, it proves to be a compelling purchase with impressive performance.

In addition, Iron Software offers a suite of five Iron Software packages for the price of just two. Get more details from this link.