Java (481)
Java Convert Byte Array to PDF: Load & Create with Spire.PDF
2025-08-21 06:31:19 Written by zaki zou
In modern Java applications, PDF data is not always stored as files on disk. Instead, it may be transmitted over a network, returned by a REST API, or stored as a byte array in a database. In such cases, you’ll often need to convert a byte array back into a PDF file or even generate a new PDF from plain text bytes.
This tutorial will walk you through both scenarios using Spire.PDF for Java, a powerful library for working with PDF documents.
Table of Contents:
- Getting Started with Spire.PDF for Java
- Understanding PDF Bytes vs. Text Bytes
- Loading PDF from Byte Array
- Creating PDF from Text Bytes
- Common Pitfalls to Avoid
- Frequently Asked Questions
- Conclusion
Getting Started with Spire.PDF for Java
Spire.PDF is a powerful and feature-rich API that allows Java developers to create, read, edit, convert, and print PDF documents without any dependencies on Adobe Acrobat.
Key Features:
- Create PDFs with text, images, tables, and shapes.
- Edit existing PDFs and extract text and images.
- Convert PDFs to formats like HTML, Word, Excel, and images.
- Encrypt PDFs with password protection.
- Add watermarks, annotations, and digital signatures.
To get started, download Spire.PDF for Java from our website and add the JAR files to your project's build path. If you’re using Maven, include the following dependency in your pom.xml.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>11.12.16</version>
</dependency>
</dependencies>
Once set up, you can now proceed to convert byte arrays to PDFs and perform other PDF-related operations.
Understanding PDF Bytes vs. Text Bytes
Before coding, it’s important to distinguish between two very different kinds of byte arrays :
- PDF File Bytes : These represent the actual binary structure of a valid PDF document. They always start with %PDF-1.x and contain objects, cross-reference tables, and streams. Such byte arrays can be loaded directly into a PdfDocument.
- Text Bytes : These are simply ASCII or UTF-8 encodings of characters. For example,
byte[] bytes = {84, 104, 105, 115};
System.out.println(new String(bytes)); // Output: "This"
Such arrays are not valid PDFs, but you can create a new PDF and write the text into it.
Loading PDF from Byte Array in Java
Suppose you want to download a PDF from a URL and work with it in memory as a byte array. With Spire.PDF for Java, you can easily load and save it back as a PDF document.
import com.spire.pdf.PdfDocument;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class LoadPdfFromByteArray throws Exception{
public static void main(String[] args) {
// The PDF URL
String fileUrl = "https://www.e-iceblue.com/resource/sample.pdf";
// Download PDF into a byte array
byte[] pdfBytes = downloadPdfAsBytes(fileUrl);
// Create a PdfDocument object
PdfDocument doc = new PdfDocument();
// Load PDF from byte array
doc.loadFromStream(new ByteArrayInputStream(pdfBytes));
// Save the document locally
doc.saveToFile("downloaded.pdf");
doc.close();
}
// Helper method: download file as byte[]
private static byte[] downloadPdfAsBytes(String fileUrl) throws Exception {
URL url = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
InputStream inputStream = conn.getInputStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] data = new byte[4096];
int nRead;
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
inputStream.close();
conn.disconnect();
return buffer.toByteArray();
}
}
How this works
- Make an HTTP request to fetch the PDF file.
- Convert the InputStream into a byte array using ByteArrayOutputStream .
- Pass the byte array into Spire.PDF via loadFromStream .
- Save or manipulate the document as needed.
Output:

Creating PDF from Text Bytes in Java
If you only have plain text bytes (e.g., This document is created from text bytes.), you can decode them into a string and then draw the text onto a new PDF document.
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
public class TextFromBytesToPdf {
public static void main(String[] args) {
// Your text bytes
byte[] byteArray = {
84, 104, 105, 115, 32,
100, 111, 99, 117, 109, 101, 110, 116, 32,
105, 115, 32,
99, 114, 101, 97, 116, 101, 100, 32,
102, 114, 111, 109, 32,
116, 101, 120, 116, 32,
98, 121, 116, 101, 115, 46
};
String text = new String(byteArray);
// Create a PDF document
PdfDocument doc = new PdfDocument();
// Configure the page settings
doc.getPageSettings().setSize(PdfPageSize.A4);
doc.getPageSettings().setMargins(40f);
// Add a page
PdfPageBase page = doc.getPages().add();
// Draw the string onto PDF
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 20f);
PdfSolidBrush brush = new PdfSolidBrush(new PdfRGBColor(Color.black));
page.getCanvas().drawString(text, font, brush, 20, 40);
// Save the document to a PDF file
doc.saveToFile("TextBytes.pdf");
doc.close();
}
}
This will produce a new PDF named TextBytes.pdf (shown below) containing the sentence represented by your byte array.

You might be interested in: How to Generate PDF Documents in Java
Common Pitfalls to Avoid
When converting byte arrays to PDFs, watch out for these issues:
- Confusing plain text with PDF bytes
Not every byte array is a valid PDF. Unless the array starts with %PDF-1.x and contains the proper structure, you can’t load it directly with PdfDocument.loadFromStream .
- Incorrect encoding
If your text bytes are in UTF-16, ISO-8859-1, or another encoding, you need to specify the charset when creating a string:
String text = new String(byteArray, StandardCharsets.UTF_8);
- Large byte arrays
When dealing with large PDFs, consider streaming instead of holding everything in memory to avoid OutOfMemoryError .
- Forgetting to close documents
Always call doc.close() to release resources after saving or processing a PDF.
Frequently Asked Questions
Q1. Can I store a PDF as a byte array in a database?
Yes. You can store a PDF as a BLOB in a relational database. Later, you can retrieve it, load it into a PdfDocument , and save or manipulate it.
Q2. How do I check if a byte array is a valid PDF?
Check if the array begins with the %PDF- header. You can do:
String header = new String(Arrays.copyOfRange(bytes, 0, 5));
if (header.startsWith("%PDF-")) {
// valid PDF
}
Q3. Can Spire.PDF load a PDF directly from an InputStream?
Yes. Instead of converting to a byte array, you can pass the InputStream directly to loadFromStream() .
Q4. Can I convert a PdfDocument back into a byte array?
You can save the document into a ByteArrayOutputStream instead of a file:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
doc.saveToStream(baos);
byte[] pdfBytes = baos.toByteArray();
Q5. What if my byte array contains images instead of text or PDF?
In that case, you’ll need to create a new PDF and insert the image using Spire.PDF’s drawing APIs.
Conclusion
In this article, we explored how to efficiently convert byte arrays to PDF documents using Spire.PDF for Java. Whether you're loading existing PDF files from byte arrays retrieved via APIs or creating new PDFs from plain text bytes, Spire.PDF provides a robust solution to meet your needs.
We covered essential concepts, including the distinction between PDF file bytes and text bytes, and highlighted common pitfalls to avoid during the conversion process. With the right understanding and tools, you can seamlessly integrate PDF functionalities into your Java applications, enhancing your ability to manage and manipulate document data.
For further exploration, consider experimenting with additional features of Spire.PDF, such as editing, encrypting, and converting PDFs to other formats. The possibilities are extensive, and mastering these techniques will undoubtedly improve your development skills and project outcomes.

Optical Character Recognition (OCR) has become an indispensable technology in modern software development, enabling computers to convert different types of documents, such as images or scanned documents, into editable and searchable text. In the Java ecosystem, integrating OCR capabilities enables powerful document processing, data extraction, and accessibility features. This guide explores how to implement OCR in Java using the Spire.OCR for Java library, leveraging its advanced features to extract text from images with or without positional coordinates.
Table of Contents:
- Why Perform OCR in Java?
- Environment Setup & Configuration
- Extract Text from an Image in Java
- Extract Text with Coordinates from an Image in Java
- Advanced OCR Techniques
- FAQs (Supported Languages and Image Formats)
- Conclusion & Free License
Why Perform OCR in Java?
OCR technology transforms images into machine-readable text. Java developers leverage OCR for:
- Automating invoice/receipt processing
- Digitizing printed records and forms
- Enabling text search in scanned documents
- Extracting structured data with spatial coordinates
Spire.OCR for Java stands out with its:
- Advanced OCR algorithms ensure accurate text recognition.
- Support for multiple image formats and languages.
- Batch processing of multiple images, improving efficiency.
- Ease of integration with Java applications
Environment Setup & Configuration
Step 1: Add Spire.OCR to Your Project
Add the following to your pom.xml:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.ocr</artifactId>
<version>2.1.1</version>
</dependency>
Alternatively, you can download the Spire.OCR for Java library, and then add it to your Java project.
Step 2: Download the OCR Model Files
Spire.OCR for Java relies on pre-trained models to extract image text. Download the OCR model files for your OS:
After downloading, unzip the files to a directory (e.g., F:\win-x64)
Extract Text from an Image in Java
The following is a basic text extraction example, i.e., reading text from an image and saving it to .txt files.
Use Cases:
- Archiving printed materials (e.g., books, newspapers) as digital text.
- Converting images (e.g., screenshots, memes, signs) into shareable/editable text.
Java code to get text from an image:
import com.spire.ocr.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class ocrJava {
public static void main(String[] args) throws Exception {
// Create an instance of the OcrScanner class
OcrScanner scanner = new OcrScanner();
// Create an instance of the ConfigureOptions class
ConfigureOptions configureOptions = new ConfigureOptions();
// Set the path to the OCR model
configureOptions.setModelPath("F:\\win-x64");
// Set the language for text recognition
configureOptions.setLanguage("English");
// Apply the configuration options to the scanner
scanner.ConfigureDependencies(configureOptions);
// Extract text from an image
scanner.scan("sample.png");
String text = scanner.getText().toString();
// Save the extracted text to a text file
try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
writer.write(text);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Key Steps:
-
Initialize Scanner: OcrScanner handles OCR operations
-
Configure Settings:
- setModelPath(): Location of OCR model files
- setLanguage(): Supports multiple languages (Chinese, Spanish, etc.)
-
Process Image: scan() performs OCR on the image file
-
Export Text: getText() retrieves results as a string
Output:

Extract Text with Coordinates from an Image in Java
In some applications, knowing the position of text within the image is crucial, for example, when processing structured documents like invoices or forms. The Spire.OCR library supports this through its block-based text extraction feature.
Use Cases:
- Automated invoice processing (locate amounts, dates)
- Form data extraction (identify fields by position)
Java code to extract text with coordinates:
import com.spire.ocr.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class ExtractWithCoordinates {
public static void main(String[] args) throws Exception {
// Create an instance of the OcrScanner class
OcrScanner scanner = new OcrScanner();
// Create an instance of the ConfigureOptions class
ConfigureOptions configureOptions = new ConfigureOptions();
// Set the path to the OCR model
configureOptions.setModelPath("F:\\win-x64");
// Set the language for text recognition
configureOptions.setLanguage("English");
// Apply the configuration options to the scanner
scanner.ConfigureDependencies(configureOptions);
// Extract text from an image
scanner.scan("invoice.png");
IOCRText text = scanner.getText();
// Create a list to store information
List<String> results = new ArrayList<>();
// Iterate through each detected text block
for (IOCRTextBlock block : text.getBlocks()) {
// Add the extracted text and coordinates to the list
results.add("Block Text: " + block.getText());
results.add("Coordinates: " + block.getBox());
results.add("---------");
}
// Save to a text file
try {
Files.write(Paths.get("ExtractWithCoordinates.txt"), results);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Key Features:
-
Text & Coordinate Extraction:
-
Iterates over text blocks detected in the image.
-
getText(): Returns an IOCRText object containing recognized text.
-
getBox(): Returns bounding box coordinates [x, y, width, height]
-
-
Efficient File Writing:
-
Uses Java NIO for efficient file operations.
-
Outputs human-readable results for easy parsing.
-
Output:

Advanced OCR Techniques
1. Enable Auto-Rotation of Images
For accurate processing of skewed or rotated images, enable the SetAutoRotate() method to rotate the image to the correct upright position automatically:
ConfigureOptions configureOptions = new ConfigureOptions();
configureOptions.SetAutoRotate(true);
2. Preserve the Original Layout
If you need to preserve the original visual layout in the image (e.g., table, multi-column layout), initialize the VisualTextAligner class to enhance the formatting of the extracted text.
// Align the recognized text (for better formatting)
VisualTextAligner visualTextAligner = new VisualTextAligner(scanner.getText());
String scannedText = visualTextAligner.toString();
FAQs (Supported Languages and Image Formats)
Q1: What image formats does Spire.OCR for Java support?
A: Spire.OCR for Java supports all common formats:
- PNG
- JPEG/JPG
- BMP
- TIFF
- GIF
Q2: What languages does Spire.OCR for Java support?
A: Multiple languages are supported:
- English (default)
- Chinese (Simplified and Traditional)
- Japanese
- Korean
- German
- French
Q3: How to improve OCR accuracy?
A: To boost accuracy:
- Use high-quality images (300+ DPI, well-lit, low noise).
- Preprocess images (adjust contrast, remove artifacts) before scanning.
- Specify the correct language(s) for the text in the image.
Q4: Can Spire.OCR for Java extract text from scanned PDFs?
A: This task requires the Spire.PDF for Java integration to convert PDFs to images or extract images from scanned PDFs first, and then use the above Java examples to get text from the images.
Conclusion & Free License
This guide provides a comprehensive roadmap for mastering Spire.OCR for Java, equipping developers with the knowledge to seamlessly integrate powerful OCR capabilities into their Java applications. From step-by-step installation to implementing basic and advanced text extraction, every critical aspect of recognizing text from images has been covered. Whether you're new to OCR or experienced, you now have the tools to convert images to text simply and effectively.
Request a 30-day free trial license here to enjoy unlimited OCR processing in Java.

Converting CSV files to Excel is a common task for Java developers working on data reporting, analytics pipelines, or file transformation tools. While manual CSV parsing is possible, it often leads to bloated code and limited formatting. Using a dedicated Excel library like Spire.XLS for Java simplifies the process and allows full control over layout, styles, templates, and data consolidation.
In this tutorial, we’ll walk through various use cases to convert CSV to Excel using Java — including basic import/export, formatting, injecting CSV into templates, and merging multiple CSVs into a single Excel file.
Quick Navigation
- Set Up Spire.XLS in Your Java Project
- Convert a CSV File to Excel Using Java
- Format Excel Output Using Java
- Merge Multiple CSV Files into One Excel File
- Tips & Troubleshooting
- Frequently Asked Questions
Set Up Spire.XLS in Your Java Project
Before converting CSV to Excel, you’ll need to add Spire.XLS for Java to your project. It supports both .xls and .xlsx formats and provides a clean API for working with Excel files without relying on Microsoft Office.
Install via Maven
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls</artifactId>
<version>15.12.15</version>
</dependency>
</dependencies>
Add JAR Manually
Download Spire.XLS for Java and add the JAR to your classpath manually. For smaller projects, you can also use the Free Spire.XLS for Java.
Convert a CSV File to Excel Using Java
The simplest use case is to convert a single .csv file into .xlsx or .xls format in Java. Spire.XLS makes this process easy using just two methods: loadFromFile() to read the CSV, and saveToFile() to export it as Excel.
import com.spire.xls.*;
public class CsvToXlsx {
public static void main(String[] args) {
Workbook workbook = new Workbook();
workbook.loadFromFile("data.csv", ",");
workbook.saveToFile("output.xlsx", ExcelVersion.Version2013);
}
}
To generate .xls format instead, use ExcelVersion.Version97to2003.
Below is the output Excel file generated after converting the CSV:

You can also specify a custom delimiter or choose the row/column to begin inserting data — useful if your sheet has titles or a fixed layout.
workbook.loadFromFile("data_semicolon.csv", ";", 3, 2);
Format Excel Output Using Java
When you're exporting CSV for reporting or customer-facing documents, it's often necessary to apply styles for better readability and presentation. Spire.XLS allows you to set cell fonts, colors, and number formats using the CellStyle class, automatically adjust column widths to fit content, and more.
Example: Apply Styling and Auto-Fit Columns
import com.spire.xls.*;
public class CsvToXlsx {
public static void main(String[] args) {
Workbook workbook = new Workbook();
workbook.loadFromFile("data.csv", ",");
Worksheet sheet = workbook.getWorksheets().get(0);
// Format header row
CellStyle headerStyle = workbook.getStyles().addStyle("Header");
headerStyle.getFont().isBold(true);
headerStyle.setKnownColor(ExcelColors.LightYellow);
for (int col = 1; col <= sheet.getLastColumn(); col++) {
sheet.getCellRange(1, col).setStyle(headerStyle);
}
// Format numeric column
CellStyle numStyle = workbook.getStyles().addStyle("Numbers");
numStyle.setNumberFormat("#,##0.00");
sheet.getCellRange("B2:B100").setStyle(numStyle);
// Auto-fit all columns
for (int i = 1; i <= sheet.getLastRow(); i++) {
sheet.autoFitColumn(i);
}
workbook.saveToFile("formatted_output.xlsx", ExcelVersion.Version2013);
}
}
Here’s what the styled Excel output looks like with formatted headers and numeric columns:

Need to use a pre-designed Excel template? You can load an existing .xlsx file and insert your data using methods like insertArray(). Just note that formatting won’t automatically apply — use CellStyle to style your data programmatically.
Merge Multiple CSV Files into One Excel File
When handling batch processing or multi-source datasets, it’s common to combine multiple CSV files into a single Excel workbook. Spire.XLS lets you:
- Merge each CSV into a separate worksheet, or
- Append all CSV content into a single worksheet
Option 1: Separate Worksheets per CSV
import com.spire.xls.*;
import java.io.File;
public class CsvToXlsx {
public static void main(String[] args) {
// Get the CSV file names
File[] csvFiles = new File("CSVs/").listFiles((dir, name) -> name.endsWith(".csv"));
// Create a workbook and clear all worksheets
Workbook workbook = new Workbook();
workbook.getWorksheets().clear();
for (File csv : csvFiles) {
// Load the CSV file
Workbook temp = new Workbook();
temp.loadFromFile(csv.getAbsolutePath(), ",");
// Append the CSV file to the workbook as a worksheet
workbook.getWorksheets().addCopy(temp.getWorksheets().get(0));
}
// Save the workbook
workbook.saveToFile("merged.xlsx", ExcelVersion.Version2016);
}
}
Each CSV file is placed into its own worksheet in the final Excel file:

Option 2: All Data in a Single Worksheet
import com.spire.xls.*;
import java.io.File;
public class CsvToXlsx {
public static void main(String[] args) {
// Get the CSV file names
File[] csvFiles = new File("CSVs/").listFiles((dir, name) -> name.endsWith(".csv"));
// Create a workbook
Workbook workbook = new Workbook();
// Clear default sheets and add a new one
workbook.getWorksheets().clear();
Worksheet sheet = workbook.getWorksheets().add("Sample");
int startRow = 1;
boolean isFirstFile = true;
for (File csv : csvFiles) {
// Load the CSV data
Workbook temp = new Workbook();
temp.loadFromFile(csv.getAbsolutePath(), ",");
Worksheet tempSheet = temp.getWorksheets().get(0);
// Check if it's the first file
int startReadRow = isFirstFile ? 1 : 2;
isFirstFile = false;
// Copy the CSV data to the sheet
for (int r = startReadRow; r <= tempSheet.getLastRow(); r++) {
for (int c = 1; c <= tempSheet.getLastColumn(); c++) {
sheet.getCellRange(startRow, c).setValue(tempSheet.getCellRange(r, c).getText());
}
startRow++;
}
}
// Save the merged workbook
workbook.saveToFile("merged_single_sheet.xlsx", ExcelVersion.Version2016);
}
}
Below is the final Excel sheet with all CSV data merged into a single worksheet:

Related Article: How to Merge Excel Files Using Java
Tips & Troubleshooting
Problems with your output? Try these fixes:
-
Text garbled in Excel → Make sure your CSV is UTF-8 encoded.
-
Wrong column alignment? → Check if delimiters are mismatched.
-
Large CSV files? → Split files or use multiple sheets for better memory handling.
-
Appending files with different structures? → Normalize column headers beforehand.
Conclusion
Whether you're handling a simple CSV file or building a more advanced reporting workflow, Spire.XLS for Java offers a powerful and flexible solution for converting CSV to Excel through Java code. It allows you to convert CSV files to XLSX or XLS with just a few lines of code, apply professional formatting to ensure readability, inject data into pre-designed templates for consistent branding, and even merge multiple CSVs into a single, well-organized workbook. By automating these processes, you can minimize manual effort and generate clean, professional Excel files more efficiently.
You can apply for a free temporary license to experience the full capabilities without limitations.
Frequently Asked Questions
How do I convert CSV to XLSX in Java?
Use Workbook.loadFromFile("file.csv", ",") and then saveToFile("output.xlsx", ExcelVersion.Version2016).
Can I format the Excel output?
Yes. Use CellStyle to control fonts, colors, alignment, and number formats.
Is it possible to use Excel templates for CSV data?
Absolutely. Load a .xlsx template and inject CSV using setText() or insertDataTable().
How can I merge several CSV files into one Excel file?
Use either multiple worksheets or merge everything into one sheet row by row.