How To Convert PDF To Base64 In Java


Quickly and Easily Convert & Edit Your PDF's Online Free!

Or Drag and Drop Documents Here to Upload

Choose Functionality

Click On The Conversion Option You Need

Edit Your Documents

Quickly and Easily Edit & Convert Documents

Download Your Documents

Save Your Document And Download!

How To Convert PDF To Base64 In Java

Overview of Converting PDF to Base64 in Java

Base64 encoding is a method to encode binary data into an ASCII string format by converting it into a radix-64 representation. This is particularly useful when you need to encode binary files such as PDF documents for embedding in JSON, XML, or HTML data. In Java, converting a PDF file to a Base64 string can be accomplished using the Base64 Encoder which is part of the Java API from Java 8 onwards.

The benefits of converting a PDF to Base64 include:

  • Easy integration of the PDF data into text-based formats.
  • Safe transmission over systems that might not handle binary data correctly.
  • Elimination of issues related to character encoding during the transport.

Before you start, ensure you have the necessary environment setup which includes:

  • Java Development Kit (JDK) installed on your system.
  • An Integrated Development Environment (IDE) like Eclipse, IntelliJ IDEA or a simple text editor.
  • A PDF file that you wish to convert to Base64.


  • Java Development Kit (JDK) 8 or higher installed on your machine.
  • A PDF file that needs to be encoded.

Steps to Convert a PDF to Base64 in Java

Step 1: Read the PDF File as a Byte Array

Begin by reading the PDF file into a byte array. This is done using standard Java IO classes.

try (FileInputStream fileInputStream = new FileInputStream("path/to/your/file.pdf")) {
    byte[] pdfByteArray = fileInputStream.readAllBytes();
    // Continue with the conversion process

Step 2: Encode the Byte Array to Base64

With the byte array obtained, use Java’s Base64 encoder to convert the bytes into a Base64 string.

String base64EncodedPDF = Base64.getEncoder().encodeToString(pdfByteArray);
// The string 'base64EncodedPDF' now contains your encoded content

Step 3: Handle Exceptions Appropriately

Make sure to handle any exceptions that might occur during the file reading and encoding process.

catch (FileNotFoundException e) {
    // Handle the case where the file could not be found
} catch (IOException e) {
    // Handle other IO exceptions

Step 4: Use the Encoded String as Needed

Once you have the Base64 encoded string, you can use it within your application as needed, such as embedding it in JSON or XML data.

// Example usage in JSON object
String json = "{"document"":"""" + base64EncodedPDF + """"}"";

Latest Posts, News & Resources


Convert PDF to Word

Converting a static PDF into a dynamic Word document can significantly streamline your workflow.

Convert PDF to JPG

Converting a multi-page PDF into individual JPG images can significantly enhance your digital experience.

Convert PDF to PNG

Converting a multi-page PDF into PNG images can significantly enhance your presentation.

Convert PDF to Text

Converting PDFs to text enables researchers, and businesses to extract valuable insights from the content.

Convert PDF to DOCX

Converting a static PDF into a dynamic DOCX document can significantly streamline your workflow.

Convert Word to PDF

Converting a multi-page WORD document into to PDF can significantly enhance the audience of your document.

Convert JPG to PDF

Merging JPG images into a consolidated PDF document can elevate your presentation and organization skills.

Convert Tiff to PDF

Converting TIFF images into a single PDF document can profoundly enhance your content.

Convert PNG to PDF

Converting individual PNG images into a singular PDF document can redefine your content delivery.

Convert Power Point to PDF

Transition from presentations to documents seamlessly. Perfect for business, educators or any user!

Convert Excel to PDF

Converting Excel spreadsheets into PDF's can elevate your data and communication efforts.

Convert DOCX to PDF

Converting a DOCX document to PDF can significantly expand your ability to share the document online.

Split PDF

Tackle large PDFs effortlessly. Whether for academic, professional, or personal use, easily segment PDFs into sections or pages.

Edit PDF

Transform your PDFs effortlessly. Perfect for students making corrections, professionals updating reports, & more.

Compress PDF

Reduce PDF sizes without compromising quality. Perfect for students, business professionals, emailing, etc.

Sign PDF

Add a professional touch to your PDFs. Perfect for business contracts, official agreements, or any document requiring validation.

Rotate PDF

Correct and customize your PDFs' orientation in moments. Perfect for professionals ensuring document consistency.

Watermark PDF

Add a unique touch or safeguard sensitive documents. Perfect for businesses branding reports, copyrighting images, & more.

Merge PDF

Consolidate multiple PDFs with ease. Ideal for students compiling research, professionals creating comprehensive reports.