Java PDF Compression Guide: Optimize File Size and Performance

Handling large PDF files is a common challenge for Java developers. PDFs with high-resolution images, embedded fonts, and multimedia content can quickly become heavy, slowing down applications, increasing storage costs, and creating a poor user experience—especially on mobile devices.

Mastering PDF compression in Java is essential to reduce file size efficiently while maintaining document quality. This step-by-step guide demonstrates how to compress and optimize PDF files in Java. You’ll learn how to compress document content, optimize images, fonts, and metadata, ensuring faster file transfers, improved performance, and a smoother user experience in your Java applications.

What You Will Learn

  1. Setting Up Your Development Environment
    1. Prerequisites
    2. Adding Dependencies
  2. Reduce PDF File Size by Compressing Document Content in Java
  3. Reduce PDF File Size by Optimizing Specific Elements in Java
    1. Image Compression
    2. Font Compression or Unembedding
    3. Metadata Removal
  4. Full Java Example that Combines All PDF Compressing Techniques
  5. Best Practices for PDF Compression
  6. Conclusion
  7. FAQs

1. Setting Up Your Development Environment

Before implementing PDF compression in Java, ensure your development environment is properly configured.

1.1. Prerequisites

  • Java Development Kit (JDK): Ensure you have JDK 1.8 or later installed.
  • Build Tool: Maven or Gradle is recommended for dependency management.
  • Integrated Development Environment (IDE): IntelliJ IDEA or Eclipse is suitable.

1.2. Adding Dependencies

To programmatically compress PDF files, you need a PDF library that supports compression features. Spire.PDF for Java provides APIs for loading, reading, editing, and compressing PDF documents. You can include it via Maven or Gradle.

Maven (pom.xml):
Add the following repository and dependency to your project's pom.xml file within the <repositories> and <dependencies> tags, respectively:

<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>

Gradle (build.gradle):
For Gradle users, add the repository and dependency as follows:

repositories {
    mavenCentral()
    maven {
        url "https://repo.e-iceblue.com/nexus/content/groups/public/"
    }
}

dependencies {
    implementation 'e-iceblue:spire.pdf:11.8.0'
}

After adding the dependency, refresh your Maven or Gradle project to download the necessary JAR files.

2. Reduce PDF File Size by Compressing Document Content in Java

One of the most straightforward techniques for reducing PDF file size is to apply document content compression. This approach automatically compresses the internal content streams of the PDF, such as text and graphics data, without requiring any manual fine-tuning. It is especially useful when you want a quick and effective solution that minimizes file size while maintaining document integrity.

The following example demonstrates how to enable and apply content compression in a PDF file using Java.

import com.spire.pdf.conversion.compression.PdfCompressor;

public class CompressContent {
    public static void main(String[] args){
        // Create a compressor
        PdfCompressor compressor = new PdfCompressor("test.pdf");

        // Enable document content compression
        compressor.getOptions().setCompressContents(true);

        // Compress and save
        compressor.compressToFile("ContentCompression.pdf");
    }
}

Key Points:

  • setCompressContents(true) enables document content compression.
  • Original PDFs remain unchanged; compressed files are saved separately.

3. Reduce PDF File Size by Optimizing Specific Elements in Java

Beyond compressing content streams, developers can also optimize individual elements of the PDF, such as images, fonts, and metadata. This allows for granular control over file size optimization.

3.1. Image Compression

Images are frequently the primary reason for large files. By lowering the image quality, you can significantly minimize the size of image-heavy PDF files.

import com.spire.pdf.conversion.compression.ImageCompressionOptions;
import com.spire.pdf.conversion.compression.ImageQuality;
import com.spire.pdf.conversion.compression.PdfCompressor;

public class CompressImages {
    public static void main(String[] args){
        // Load the PDF document
        PdfCompressor compressor = new PdfCompressor("test.pdf");

        // Get image compression options
        ImageCompressionOptions imageCompression = compressor.getOptions().getImageCompressionOptions();

        // Compress images and set quality
        imageCompression.setCompressImage(true);          // Enable image compression
        imageCompression.setImageQuality(ImageQuality.Low); // Set image quality (Low, Medium, High)
        imageCompression.setResizeImages(true);           // Resize images to reduce size

        // Save the compressed PDF
        compressor.compressToFile("ImageCompression.pdf");
    }
}

Key Points:

  • setCompressImage(true) enables image compression.
  • setImageQuality(...) adjusts the output image quality; the lower the quality, the smaller the image size.
  • setResizeImages(true) enables image resizing.

3.2. Font Compression or Unembedding

When a PDF uses custom fonts, the entire font file might be embedded, even if only a few characters are used. Font compression or unembedding is a technique that reduces the size of embedded fonts by compressing them or removing them entirely from the PDF.

import com.spire.pdf.conversion.compression.PdfCompressor;
import com.spire.pdf.conversion.compression.TextCompressionOptions;

public class CompressPDFWithOptions {
    public static void main(String[] args){
        // Load the PDF document
        PdfCompressor compressor = new PdfCompressor("test.pdf");

        // Get text compression options
        TextCompressionOptions textCompression = compressor.getOptions().getTextCompressionOptions();

        // Compress fonts
        textCompression.setCompressFonts(true);

        // Optional: unembed fonts to reduce size
        // textCompression.setUnembedFonts(true);

        // Save the compressed PDF
        compressor.compressToFile("FontOptimization.pdf");
    }
}

Key Points:

  • setCompressFonts(true) compresses embedded fonts while preserving document appearance.
  • setUnembedFonts(true) removes embedded fonts entirely, which may reduce file size but could affect text rendering if the fonts are not available on the system.

3.3 Metadata Removal

PDFs often store metadata such as author details, timestamps, and editing history that aren’t needed for viewing. Removing metadata reduces file size and protects sensitive information.

import com.spire.pdf.conversion.compression.PdfCompressor;

public class CompressPDFWithOptions {
    public static void main(String[] args){
        // Load the PDF document
        PdfCompressor compressor = new PdfCompressor("test.pdf");

        // Remove metadata
        compressor.getOptions().setRemoveMetadata(true);

        // Save the compressed PDF
        compressor.compressToFile("MetadataRemoval.pdf");
    }
}

4. Full Java Example that Combines All PDF Compressing Techniques

After exploring both document content compression and element-specific optimizations (images, fonts, and metadata), let’s explore how to apply all these techniques together in one workflow.

import com.spire.pdf.conversion.compression.ImageQuality;
import com.spire.pdf.conversion.compression.OptimizationOptions;
import com.spire.pdf.conversion.compression.PdfCompressor;

public class CompressPDFWithAllTechniques {
    public static void main(String[] args){
        // Initialize compressor
        PdfCompressor compressor = new PdfCompressor("test.pdf");

        // Enable document content compression
        OptimizationOptions options = compressor.getOptions();
        options.setCompressContents(true);

        // Optimize images (downsampling and compression)
        options.getImageCompressionOptions().setCompressImage(true);
        options.getImageCompressionOptions().setImageQuality(ImageQuality.Low);
        options.getImageCompressionOptions().setResizeImages(true);

        // Optimize fonts (compression or unembedding)
        // Compress fonts
        options.getTextCompressionOptions().setCompressFonts(true);
        // Optional: unembed fonts to reduce size
        // options.getTextCompressionOptions().setUnembedFonts(true);
        
        // Remove unnecessary metadata
        options.setRemoveMetadata(true);

        // Save the compressed PDF
        compressor.compressToFile("CompressPDFWithAllTechniques.pdf");
    }
}

Reviewing the Compression Effect:

After running the code, the original sample PDF of 3.09 MB was reduced to 742 KB. The compression ratio is approximately 76%.

Java Code Example to Compress PDF

5. Best Practices for PDF Compression

When applying PDF compression in Java, it’s important to follow some practical guidelines to ensure the file size is reduced effectively without sacrificing usability or compatibility.

  • Choose methods based on content: PDF compression depends heavily on the type of content. Text-based files may only require content and font optimization, while image-heavy documents benefit more from image compression. In many cases, combining multiple techniques yields the best results.
  • Balance quality with file size: Over-compression may influence the document's readability, so it’s important to maintain a balance.
  • Test across PDF readers: Ensure compatibility with Adobe Acrobat, browser viewers, and mobile apps.

6. Conclusion

Compressing PDF in Java is not just about saving disk space—it directly impacts performance, user experience, and system efficiency. Using Libraries like Spire.PDF for Java, developers can implement fine-grained compression techniques, from compressing content, optimizing images and fonts, to cleaning up unused metadata.

By applying the right strategies, you can minimize PDF size in Java significantly without sacrificing quality. This leads to faster file transfers, lower storage costs, and smoother rendering across platforms. Mastering these compression methods ensures your Java applications remain responsive and efficient, even when handling complex, resource-heavy PDFs.

7. FAQs

Q1: Can I reduce PDF file size in Java without losing quality?

A1: Yes. Spire.PDF allows selective compression of images, fonts, and other objects while maintaining readability and layout.

Q2: Will compressed PDFs remain compatible with popular PDF readers?

A2: Yes. Compressed PDFs remain compatible with Adobe Acrobat, browser viewers, mobile apps, and other standard PDF readers.

Q3: What’s the difference between image compression and font compression?

A3: Image compression reduces the size of embedded images, while font compression reduces embedded font data or removes unused fonts. Both techniques together optimize file size effectively.

Q4: How do I choose the best compression strategy?

A4: Consider the PDF content. Use image compression for image-heavy PDFs and font compression for text-heavy PDFs. Often, combining both techniques yields the best results without affecting readability.

Q5: Can I automate PDF compression for multiple files in Java?

A5: Yes. You can write Java scripts to batch compress multiple PDFs by applying the same compression settings consistently across all files.

Merge two or multiple PDF files into a single file in Java

Merging PDFs in Java is a critical requirement for document-intensive applications, from consolidating financial reports to automating archival systems. However, developers face significant challenges in preserving formatting integrity or managing resource efficiency across diverse PDF sources. Spire.PDF for Java provides a robust and straightforward solution to streamline the PDF merging task.

This comprehensive guide explores how to combine PDFs in Java, complete with practical examples to merge multiple files, selected pages, or stream-based merging.


Setting Up the Java PDF Merge Library

Why Choose Spire.PDF for Java?

  • No External Dependencies: Pure Java implementation.
  • Rich Features: Merge, split, encrypt, and annotate PDFs.
  • Cross-Platform: Works on Windows, Linux, and macOS.

Installation

Before using Spire.PDF for Java, you need to add it to your project.

Option 1: Maven

Add the repository and dependency to 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>

Option 2: Manual JAR

Download the JAR from the E-iceblue website and add it to your project's build path.


Merge Multiple PDF Files in Java​

This example is ideal when you want to merge two or more PDF documents entirely. It’s simple, straightforward, and perfect for batch processing.

How It Works:​

  • Define File Paths: Create an array of strings containing the full paths to the source PDFs.​
  • Merge Files: The mergeFiles() method takes the array of paths, combines the PDFs, and returns a PdfDocumentBase object representing the merged file.​
  • Save the Result: The merged PDF is saved to a new file using the save() method.​

Java code to combine PDFs:

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfDocumentBase;

public class MergePdfs {
    public static void main(String[] args) {

        // Get the paths of the PDF documents to be merged 
        String[] files = new String[] {"sample-1.pdf", "sample-2.pdf", "sample-3.pdf"};

        // Merge these PDF documents
        PdfDocumentBase pdf = PdfDocument.mergeFiles(files);

        // Save the merged PDF file
        pdf.save("MergePDF.pdf", FileFormat.PDF);
    }
}

Best For:​

  • Merging entire PDFs stored locally.​
  • Simple batch operations where no page selection is needed.

Result: Combine three PDF files (a total of 10 pages) into one PDF file.

Merge multiple PDF files into a single PDF

Merging PDFs often results in large file sizes. To reduce the size, refer to: Compress PDF Files in Java.

Merge Specific Pages from Multiple PDFs in Java

Sometimes, you may only want to merge specific pages from different PDFs (e.g., pages 1-3 from File A and pages 2-5 from File B). This example gives you granular control over which pages to include from each source PDF.

How It Works:​

  • Load PDFs: Load each source PDF into a PdfDocument object and store them in an array.​
  • Create a New PDF: A blank PDF document is initialized to serve as the container for merged pages.​
  • Insert Specific Pages:​
    • insertPage(): Insert a specified page into the new PDF.​
    • insertPageRange(): Inserts a range of pages into the new PDF.
  • Save the Result: The merged PDF is saved using the saveToFile() method.​

Java code to combine selected PDF pages:

import com.spire.pdf.PdfDocument;

public class MergeSelectedPages {

    public static void main(String[] args) {

        // Get the paths of the PDF documents to be merged
        String[] files = new String[] {"sample-1.pdf", "sample-2.pdf", "sample-3.pdf"};

        // Create an array of PdfDocument
        PdfDocument[] pdfs = new PdfDocument[files.length];

        // Loop through the documents
        for (int i = 0; i < files.length; i++)
        {
            // Load a specific document
            pdfs[i] = new PdfDocument(files[i]);
        }

        // Create a new PDF document
        PdfDocument pdf = new PdfDocument();

        // Insert the selected pages from different PDFs to the new PDF
        pdf.insertPage(pdfs[0], 0);
        pdf.insertPageRange(pdfs[1], 1,3);
        pdf.insertPage(pdfs[2], 0);

        // Save the merged PDF
        pdf.saveToFile("MergePdfPages.pdf");
    }
}

Best For:​

  • Creating custom PDFs with selected pages (e.g., extracting key sections from reports).​
  • Scenarios where you need to exclude irrelevant pages from source documents.

Result: Combine selected pages from three separate PDF files into a new PDF

Combine specified pages from different PDFs into a new PDF file

Merge PDF Files by Streams in Java

In applications where PDFs are stored as streams (e.g., PDFs from network streams, in-memory data, or temporary files), Spire.PDF supports merging without saving files to disk.

How It Works:​

  • Create Input Streams: The FileInputStream objects read the raw byte data of each PDF file.​
  • Merge Streams: The mergeFiles() method accepts an array of streams, merges them, and returns a PdfDocumentBase object.​
  • Save and Clean Up: The merged PDF is saved, and all streams and documents are closed to free system resources (critical for preventing leaks).​

Java code to merge PDFs via streams:

import com.spire.pdf.*;
import java.io.*;

public class mergePdfsByStream {
    public static void main(String[] args) throws IOException {
        // Create FileInputStream objects for each PDF document file
        FileInputStream stream1 = new FileInputStream(new File("Template_1.pdf"));
        FileInputStream stream2 = new FileInputStream(new File("Template_2.pdf"));
        FileInputStream stream3 = new FileInputStream(new File("Template_3.pdf"));

        // Initialize an array of InputStream objects containing the file input streams
        InputStream[] streams = new FileInputStream[]{stream1, stream2, stream3};

        // Merge the input streams into a single PdfDocumentBase object
        PdfDocumentBase pdf = PdfDocument.mergeFiles(streams);

        // Save the merged PDF file
        pdf.save("MergePdfsByStream.pdf", FileFormat.PDF);

        // Releases system resources used by the merged document
        pdf.close();
        pdf.dispose();

        // Closes all input streams to free up resources
        stream1.close();
        stream2.close();
        stream3.close();
    }
}

Best For:​

  • Merging PDFs from non-file sources (e.g., network downloads, in-memory generation).​
  • Environments where direct file path access is restricted.

Conclusion

Spire.PDF for Java simplifies complex PDF merging tasks through its intuitive, user-friendly API. Whether you need to merge entire documents, create custom page sequences, or combine PDFs from stream sources, these examples enable efficient PDF merging in Java to address diverse document processing requirements.

To explore more features (e.g., encrypting merged PDFs, adding bookmarks), refer to the official documentation.


Frequently Asked Questions (FAQs)

Q1: Why do merged PDFs show "Evaluation Warning" watermarks?

A: The commercial version adds watermarks. Solutions:

Q2: How do I control the order of pages in the merged PDF?

A: The order of pages in the merged PDF is determined by the order of input files (or streams) and the pages you select. For example:

  • In full-document merging, files in the input array are merged in the order they appear.
  • In selective page merging, use insertPage() or insertPageRange() in the sequence you want pages to appear.

Q3: Can I merge password-protected PDFs?

A: Yes. Spire.PDF for Java supports merging encrypted PDFs, but you must provide the password when loading the file. Use the overloaded loadFromFile() method with the password parameter:

PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("sample.pdf", "userPassword"); // Decrypt with password

Q4: How to merge scanned/image-based PDFs?

A: Spire.PDF handles image-PDFs like regular PDFs, but file sizes may increase significantly.

Convert PDF to Image in Java

Converting PDF files to images is essential for various document processing tasks, including generating thumbnails, archiving, and image manipulation. These conversions allow applications to present PDF content in more accessible formats, enhancing user experience and functionality. Java libraries such as Spire.PDF for Java enable efficient conversions to formats like PNG , JPEG , GIF , BMP , TIFF , and SVG , each serving specific purposes based on their characteristics.

This guide will walk you through the conversion process using Spire.PDF for Java, providing optimized code examples for each format. Additionally, it will explain the key differences among these formats to help you select the most suitable option for your needs.

Java PDF-to-Image Conversion Library

Spire.PDF for Java is a robust, feature-rich library for PDF manipulation. It offers several advantages for image conversion:

  1. High-quality rendering that preserves document formatting and layout
  2. Batch processing capabilities for handling multiple documents
  3. Flexible output options including resolution and format control
  4. Lightweight implementation with minimal memory footprint

The library supports conversion to all major image formats while maintaining excellent text clarity and graphic fidelity, making it suitable for both simple conversions and complex document processing pipelines.

Installation

To get started, download Spire.PDF for Java from our website and add it as a dependency in your project. For Maven users, include the following 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.6.2</version>
    </dependency>
</dependencies>

Image Format Comparison

Different image formats serve distinct purposes. Below is a comparison of commonly used formats:

Format Compression Transparency Best For File Size
PNG Lossless Yes High-quality graphics, logos Medium
JPEG Lossy No Photographs, web images Small
GIF Lossless Yes (limited) Simple animations, low-color graphics Small
BMP None No Uncompressed images, Windows apps Large
TIFF Lossless Yes High-quality scans, printing Very Large
SVG Vector-based Yes (scalable) Logos, icons, web graphics Small

PNG is ideal for images requiring transparency and lossless quality, while JPEG is better for photographs due to its smaller file size. GIF supports simple animations, and BMP is rarely used due to its large size. TIFF is preferred for professional printing, and SVG is perfect for scalable vector graphics.

Convert PDF to PNG, JPEG, GIF, and BMP

Converting PDF files into various image formats like PNG, JPEG, GIF, and BMP allows developers to cater to diverse application needs. Each format serves different purposes; for example, PNG is ideal for high-quality graphics with transparency, while JPEG is better suited for photographs with smooth gradients.

By leveraging Spire.PDF's capabilities, developers can easily generate the required image formats for their projects, ensuring compatibility and optimal performance.

Basic Conversion Example

Let's start with the fundamental conversion process. The following code demonstrates how to convert each page of a PDF into individual PNG images:

import com.spire.pdf.PdfDocument;
import com.spire.pdf.graphics.PdfImageType;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ConvertPdfToImage {

    public static void main(String[] args) throws IOException {

        // Create a PdfDocument instance
        PdfDocument doc = new PdfDocument();

        // Load a PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pdf");

        // Iterate through the pages
        for (int i = 0; i < doc.getPages().getCount(); i++) {

            // Convert the current page to BufferedImage
            BufferedImage image = doc.saveAsImage(i, PdfImageType.Bitmap);

            // Save the image data as a png file
            File file = new File("output/" + String.format(("ToImage-img-%d.png"), i));
            ImageIO.write(image, "PNG", file);
        }

        // Clear up resources
        doc.close();
    }
}

Explanation:

  • The PdfDocument class loads the input PDF file.
  • saveAsImage() converts each page into a BufferedImage .
  • ImageIO.write() saves the image in PNG format.

Tip : To convert to JPEG, GIF, or BMP, simply replace " PNG " with " JPEG ", " GIF ", or " BMP " in the ImageIO.write() method.

Output:

Screenshot of input PDF document and output PNG image files.

PNG with Transparent Background

Converting a PDF page to PNG with a transparent background involves adjusting the conversion options using the setPdfToImageOptionsmethod. This adjustment provides greater flexibility in generating the images, allowing for customized output that meets specific requirements.

Here’s how to achieve this:

doc.getConvertOptions().setPdfToImageOptions(0);

for (int i = 0; i < doc.getPages().getCount(); i++) {
    BufferedImage image = doc.saveAsImage(i, PdfImageType.Bitmap);
    File file = new File("C:\\Users\\Administrator\\Desktop\\Images\\" + String.format(("ToImage-img-%d.png"), i));
    ImageIO.write(image, "PNG", file);
}

Explanation:

  • setPdfToImageOptions(0) ensures transparency is preserved.
  • The rest of the process remains the same as the basic conversion.

Custom DPI Settings

The saveAsImage method also provides an overload that allows developers to specify the DPI (dots per inch) for the output images. This feature is crucial for ensuring that the images are rendered at the desired resolution, particularly when high-quality images are required.

Here’s an example of converting a PDF to images with a specified DPI:

for (int i = 0; i < doc.getPages().getCount(); i++) {
    BufferedImage image = doc.saveAsImage(i, PdfImageType.Bitmap,300, 300);
    File file = new File("C:\\Users\\Administrator\\Desktop\\Images\\" + String.format(("ToImage-img-%d.png"), i));
    ImageIO.write(image, "PNG", file);
}

Explanation:

  • The saveAsImage() method accepts dpiX and dpiY parameters for resolution control.
  • Higher DPI values (e.g., 300) produce sharper images but increase file size.

DPI Selection Tip:

  • 72-100 DPI : Suitable for screen display
  • 150-200 DPI : Good for basic printing
  • 300+ DPI : Professional printing quality
  • 600+ DPI : High-resolution archival

Convert PDF to TIFF in Java

TIFF (Tagged Image File Format) is another popular image format, especially in the publishing and printing industries. Spire.PDF makes it easy to convert PDF pages to TIFF format using the saveToTiff method.

Here’s a simple example demonstrating how to convert a PDF to a multi-page TIFF:

import com.spire.pdf.PdfDocument;

public class ConvertPdfToTiff {

    public static void main(String[] args) {

        // Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        // Load a PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pdf");

        // Convert a page range to tiff
        doc.saveToTiff("output/PageToTiff.tiff",0,2,300,300);

        // Clear up resources
        doc.dispose();
    }
}

Explanation:

  • saveToTiff() converts a specified page range (here, pages 0 to 2).
  • The last two parameters set the DPI for the output image.

Output:

Screenshot of input PDF document and output Tiff file.

Convert PDF to SVG in Java

SVG (Scalable Vector Graphics) is a vector image format that is widely used for its scalability and compatibility with web technologies. Converting PDF to SVG can be beneficial for web applications that require responsive images.

To convert a PDF document to SVG using Spire.PDF, the following code can be implemented:

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;

public class ConvertPdfToSvg {

    public static void main(String[] args) {

        // Initialize a PdfDocument object
        PdfDocument doc = new PdfDocument();

        // Load the PDF document from the specified path
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pdf");

        // Optionally, convert to a single SVG (uncomment to enable)
        // doc.getConvertOptions().setOutputToOneSvg(true);

        // Save the document as SVG files (one SVG per page by default)
        doc.saveToFile("Output/PDFToSVG.svg", FileFormat.SVG);

        // Clear up resources 
        doc.dispose();
    }
}

Explanation:

  • saveToFile() with FileFormat.SVG exports the PDF as an SVG file.
  • Optionally, setOutputToOneSvg(true) merges all pages into a single SVG.

Output:

Screenshot of input PDF document and output SVG image files.

Conclusion

Spire.PDF for Java simplifies PDF-to-image conversion with support for popular formats like PNG, JPEG, TIFF, SVG, and more . Key features such as transparency control, custom DPI settings, and multi-page TIFF/SVG output enable tailored solutions for generating thumbnails, high-quality prints, or web-optimized graphics. The library ensures high fidelity and performance , making it ideal for batch processing or dynamic rendering. Easily integrate the provided code snippets and APIs to enhance document handling in your Java applications, whether for reporting, archiving, or interactive content.

FAQs

Q1. Can I specify the DPI when converting to TIFF or SVG?

When converting to TIFF, you can specify the DPI to ensure high-quality output. However, SVG is a vector format and does not require DPI settings, as it scales based on the display size.

Q2. Can I convert specific pages of a PDF to images?

Yes, both the saveAsImage and saveToTiff methods allow you to indicate which pages to include in the conversion.

Q3. What is the difference between lossless and lossy image formats?

Lossless formats (like PNG and TIFF) retain all image quality during compression, while lossy formats (like JPEG) reduce file size by discarding some image information, which may affect quality.

Q4. How does converting to SVG differ from raster formats?

Converting to SVG generates vector images that scale without losing quality, while raster formats like PNG and JPEG are pixel-based and can lose quality when resized.

Q5. What other file formats can Spire.PDF convert PDFs to?

Spire.PDF is a powerful Java PDF library that supports converting PDF files to multiple formats, such as:

Get a Free License

To fully experience the capabilities of Spire.PDF for Java without any evaluation limitations, you can request a free 30-day trial license.

Generate a QR code in Java using a barcode generator library

QR codes have become an integral part of modern digital interactions, enabling seamless data sharing through scannable patterns. If you’re looking to generate QR codes in Java, the Spire.Barcode for Java library offers a robust, user-friendly solution.

This comprehensive guide will walk you through every aspect of creating QR code in Java, including setup, basic QR code generation, logo integration, advanced customization, and industry best practices.


Java Barcode Generator Library

Spire.Barcode for Java is a lightweight, high-performance library designed to generate and read 1D and 2D barcodes, including QR codes, Data Matrix, Code 128, and more. It stands out for its:

  • Ease of use: Minimal code required to generate barcodes.
  • Customization options: Adjust size, color, error correction, and add logos.
  • Compatibility: Supports Java SE 6.0 and above, and integrates with popular IDEs like IntelliJ and Eclipse.
  • Output flexibility: Export QR codes to common image formats (PNG, JPG, BMP, etc.).

Spire.Barcode simplifies QR code generation with pre-built classes for configuration and rendering. Download it to manually import or add it to your project using 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.barcode</artifactId>
        <version>5.2.0</version>
    </dependency>
</dependencies>

Tip: Request a free trial license here to remove evaluation message and watermarks.

You can also use the Free Version to process barcodes:

<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.barcode.free</artifactId>
    <version>5.2.1</version>
</dependency>

How to Generate a QR Code in Java

Creating a standard QR code with Spire.Barcode for Java involves just a few steps. This example generates a QR code that encodes a URL and saves it as a PNG image.

Key Components:

  • BarcodeSettings: Configures all properties of the QR code.
  • setType(): Specifies the QR code type (other types like Code 128 are also supported).
  • setData(): Defines the content to encode (URL, text, numbers, etc.).
  • setX(): Sets the width of each "module" (the tiny squares in the QR code).
  • setQRCodeECL(): Defines error correction level.
  • generateImage(): Uses the settings to render the QR code as a BufferedImage.
  • ImageIO.write(): Saves the image to a file (PNG, JPG, or other formats).

Java code to create a QR code:

import com.spire.barcode.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class GenerateQRcode {
    public static void main(String[] args) throws IOException {

        // Create a BarcodeSettings object
        BarcodeSettings settings = new BarcodeSettings();

        // Set the barcode type to QR Code
        settings.setType(BarCodeType.QR_Code);

        // Set the QR code data 
        settings.setData("https://www.e-iceblue.com/");


        // Set to not display the encoded text (the URL)
        settings.setShowText(false);

        // Set width of the barcode module
        settings.setX(2);

        // Set the error correction level
        settings.setQRCodeECL(QRCodeECL.M);

        // Set the data mode
        settings.setQRCodeDataMode(QRCodeDataMode.Auto);

        // Create BarCodeGenerator object based on settings
        BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);

        // Generates QR code as a BufferedImage (in-memory image object)
        BufferedImage bufferedImage = barCodeGenerator.generateImage();

        // Save the generated QR code image in PNG format
        ImageIO.write(bufferedImage,"png",new File("QR_Code.png"));

    }
}

Output: A basic QR code image in PNG format. When scanned, the user will be directed to the specified URL address.

Generate a simple QR code using Java

Integrate with the Spire.PDF for Java library to add the QR code image to a PDF document.

Generate a QR Code with a Logo in Java

Adding a logo to a QR code enhances brand visibility while maintaining scannability. Spire.Barcode for Java simplifies this by allowing you to overlay an image onto the QR code via the setQRCodeLogoImage() method.​

Key Considerations:​

  • Use a small logo (10-20% of QR code size) to avoid disrupting data patterns.​
  • Ensure high contrast between the logo and QR code background.

Java code to generate a QR code with embedded logo:

import com.spire.barcode.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class QRcodeWithLogo {
    public static void main(String[] args) throws IOException {

        // Create a BarcodeSettings object
        BarcodeSettings settings = new BarcodeSettings();

        // Set the barcode type to QR Code
        settings.setType(BarCodeType.QR_Code);

        // Set the QR code data (URL, text, numbers, etc.)
        settings.setData("https://www.e-iceblue.com/");


        // Set to not display the encoded text (the URL)
        settings.setShowText(false);

        // Set width of the barcode module
        settings.setX(3);

        // Set the error correction level
        settings.setQRCodeECL(QRCodeECL.H);

        // Set the data mode
        settings.setQRCodeDataMode(QRCodeDataMode.Auto);

        // Load an image and set it as the logo image for the QR code
        BufferedImage image = ImageIO.read(new File("logo.png"));
        settings.setQRCodeLogoImage(image);

        // Create BarCodeGenerator object based on settings
        BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);

        // Generates QR code as a BufferedImage (in-memory image object)
        BufferedImage bufferedImage = barCodeGenerator.generateImage();

        // Save the generated QR code image in PNG format
        ImageIO.write(bufferedImage,"png",new File("QrCodeWithLogo.png"));

    }
}

Output: A PNG image file containing a scannable QR code with a centered logo.

Generate a QR code with logo using Java

Need to scan the QR code in Java? Refer to: How to Read Barcodes in Java Using Spire.Bacode


Customize QR Code Options

Spire.Barcode for Java lets you tailor QR codes to match your design requirements. Below are key customization options:

1. Add Display Text (Top/Bottom Labels)

Add descriptive text above or below the QR code and set its formatting (font, color, alignment).

// Enable top and bottom text
settings.setShowTopText(true);
settings.isShowBottomText(true);

// Set text content
settings.setTopText("Scan to Visit Our Site");
settings.setBottomText("Example Corp © 2025");

// Customize text appearance
settings.setTopTextFont("Arial", 14.0f);
settings.setTopTextColor(Color.BLUE);
settings.setTopTextMargin(1.0f);
settings.setBottomTextFont("Calibri", 12.0f, FontStyle.Italic);
settings.setBottomTextColor(Color.GRAY);

// Align text (Near, Center, Far)
settings.setTopTextAligment(StringAlignment.Center);
settings.setBottomTextAlignment(StringAlignment.Far);

Add top and bottom text to a QR code

2. Customize Borders & Quiet Zones

The "quiet zone" is a blank (usually white) area surrounding the QR code (and its border). It’s critical for scanners to recognize where the QR code starts and ends.

Spire.Barcode for Java allows you to set both the visible border and quiet zone:

// Enable border
settings.hasBorder(true);

// Set border properties
settings.setBorderColor(Color.BLUE); // Border color
settings.setBorderWidth(1); // Border thickness
settings.setBorderDashStyle(3); // Dashed border (options: Solid, Dash, Dot, etc.)

// Set border margins
// These margins create the "quiet zone" around the QR code
settings.setLeftMargin(8);
settings.setRightMargin(8);
settings.setTopMargin(8);
settings.setBottomMargin(8);

Set border and quiet zone for a QR code

3. Change Colors

Modify the QR code’s foreground (data) and background colors to match your brand:

// Set foreground color (black by default)
settings.setForeColor(Color.GRAY);
// Set background color (white by default)
settings.setBackColor(new Color(250, 243, 202));

Set foreground and background colors for a QR code


Best Practices for QR Code Generation

Follow these guidelines for professional QR implementations:

  1. Error Correction Level: Choose wisely:
    • L (Low): 7% data recovery
    • M (Medium): 15% recovery
    • Q (Quartile): 25% recovery
    • H (High): 30% recovery (best for logos)
  2. Optimal Size: Minimum 22 cm (0.8x0.8 in) for print, 200200 px for digital
  3. Contrast: Ensure high contrast between code and background
  4. Test Scannability: Always verify your QR codes with multiple devices/ scanner apps

Conclusion

Generating QR codes in Java is straightforward with libraries like Spire.Barcode. Whether you need a basic QR code encoding text/URL, a branded code with a logo, or a fully customized design, this library provides the tools to achieve it efficiently. By following the examples and best practices outlined in this guide, you’ll be able to integrate professional-grade QR codes into your Java applications, enhancing user experience and enabling seamless data sharing.

C# Replace Text with Regular Expression

When working with Word templates, it's common to replace specific placeholders with real content—like names, dates, or even company logos. Manually updating each instance may take lots of effort and time. Fortunately, you can automate this process using code. In this tutorial, we'll show you how to use C# regex replace to find and replace text in Word documents. You'll learn how to apply regular expressions across the entire document, target specific paragraphs, and even replace matched text with images.

Before We Start: Install Word Library

To make this task easier, we recommend using Spire.Doc for .NET — a powerful Word library designed to automate common document operations such as reading, editing, and converting Word files. With Spire.Doc, performing a C# regex replace in Word documents can be done in just a few lines of code.

You can install the library via NuGet with the following command:

PM> Install-Package Spire.Doc

Alternatively, you can download the Spire.Doc package and install it with custom settings. A free version is also available, ideal for small projects or evaluation purposes.

Use Regex to Replace Text in an Entire Word Document

Let's start with the most common scenario — replacing text throughout the entire Word document. You can use regular expressions to match all patterns like #name or #tel, then replace them with actual values such as a person's name or phone number.

With the help of the Document.Replace() method provided by Spire.Doc, this task becomes incredibly simple. Here's a step-by-step explanation along with sample code to show how to replace text using regex in C#.

Code example - replacing #name with "May" in the entire Word document using C#:

using Spire.Doc;
using System.Text.RegularExpressions;
namespace FindText
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create an object of Document class and load a Word document
            Document doc = new Document();
            doc.LoadFromFile("/input/replace template.docx");
            // Set the regular expression
            Regex regex = new Regex(@"#\w+\b");
            // Replace all matches with "May"
            doc.Replace(regex, "May");
            // Save the document
            doc.SaveToFile("/output/replacealloccurences.docx", FileFormat.Docx2013);
        }
    }
}

Here is a comparison preview of the results before and after using regex to replace text in C#: C# Regex Replace Text within an Entire Word Document

Steps Explained:

  • Create a Document instance and load a Word document from files.
  • Set the regular expression.
  • Replace all matches in the document with Documet.Replace() method.
  • Save the updated document.

Apply Regex Replace to a Specific Paragraph in C#

Replacing text across the entire document may seem straightforward, but how can we achieve the same result within a specific paragraph? When you need more precise control, such as performing regex replace only within a paragraph range, you can retrieve the paragraph text and reset its content manually. In code, this means using regular expressions to locate matches within the target paragraph, remove the original text, and insert the replacement.

Let’s first look at a real-world code example — we’ll break down the detailed steps right after.

Code example - replacing #name with "Lily" in the first paragraph using regular expression:

using Spire.Doc;
using Spire.Doc.Documents;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
        // Create a Document object and load a template
        Document doc = new Document();
        doc.LoadFromFile("/input/replace template.docx");

        Regex regex = new Regex(@"#\w+\b");

        // Get the first paragraph
        Paragraph para = doc.Sections[0].Paragraphs[0];
        string originalText = para.Text;

        if (regex.IsMatch(originalText))
        {
            string newText = regex.Replace(originalText, "Lily");

            // Clear the content and append the new text
            para.ChildObjects.Clear(); 
            para.AppendText(newText);  
        }

        // Save the updated document
        doc.SaveToFile("/output/replacefirstpara.docx", FileFormat.Docx2013);
    }
}

Here is a before-and-after comparison of using regular expressions in C# to replace text in the first paragraph: Replace with Regex Using C# in a Specific Paragraph

Detailed steps explained:

  • Create a Document object and load the Word file.
  • Access the target paragraph using Sections[].Paragraphs[] property.
  • Get the original text from the paragraph.
  • Use a regular expression to find and replace matching text.
  • Clear the original content and append the updated text to the paragraph.
  • Save the modified document.

Replace Regex Matches with Images in Word Using C#

Now that we’ve covered how to replace text in the entire document or within a single paragraph, let’s move on to something more advanced—replacing text with images. This is often used in practical scenarios like generating reports or company brochures. For instance, you might want to replace a placeholder such as [logo] with your company’s actual logo.

With the help of C# regex replace and Spire.Doc’s image insertion APIs, this task can also be automated. Let’s take a look at a real code example first, then we’ll explain how it works.

Code example – replacing [avatar] with an image:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Text.RegularExpressions;

namespace RegexReplaceWithImage
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Document object and load a Word file from disk
            Document document = new Document();
            document.LoadFromFile(@"\input\replace template.docx");

            // Define a regular expression to match placeholders
            Regex regex = new Regex(@"\[\w+\]", RegexOptions.IgnoreCase);

            // Find all matches in the document that match the pattern
            TextSelection[] selections = document.FindAllPattern(regex);

            // Loop through each matched placeholder
            foreach (TextSelection selection in selections)
            {
                // Create a picture object and load the image from disk
                DocPicture pic = new DocPicture(document);
                pic.LoadImage(@"\avatar-1.png");

                // Get the matched text as a single text range
                TextRange textRange = selection.GetAsOneRange();

                // Get the index of the text range in its parent paragraph
                int index = textRange.OwnerParagraph.ChildObjects.IndexOf(textRange);

                // Insert the image at the matched position
                textRange.OwnerParagraph.ChildObjects.Insert(index, pic);

                // Remove the original placeholder text
                textRange.OwnerParagraph.ChildObjects.Remove(textRange);
            }

            // Save the modified document to disk
            document.SaveToFile(@"\output\ReplaceTextWithImage.docx", FileFormat.Docx2016);
            document.Close();
        }
    }
}

Here is a before-and-after comparison of replacing text with an image using regex in C#: Regex Replace Text with Image Using C#

Detailed steps explained:

  • Create an object of Document class and read a Word document.
  • Define a regular expression to match target content.
  • Find all occurrences using Document.FindAllPattern() method.
  • Loop through the collection of matches.
  • Create a DocPicture instance and load an image.
  • Get the matched text as a text range and retrieve its index in the parent paragraph.
  • Insert the image at the position and remove the original text.
  • Save the Word document as a new file.

Replace Text with Regex in VB.NET

If you're working with VB.NET instead of C#, you can achieve the same result using regular expressions to search and replace text throughout a Word document. The approach is similar—load the file, apply a regex pattern, and update the content. Here's a simple example to get you started.

Imports Spire.Doc
Imports System.Text.RegularExpressions

Module Module1
	Sub Main()
		Dim doc As New Document()
		doc.LoadFromFile("/input/replace template.docx")

		Dim regex As New Regex("#\w+\b")
		doc.Replace(regex, "Lena")

		doc.SaveToFile("/output/regexreplace.docx", FileFormat.Docx2013)
	End Sub
End Module

Above is the VB.NET code for replacing text throughout an entire Word document using regular expressions.

If you need to perform other tasks, such as replacing text in specific paragraphs or replacing text with images, you can easily convert the C# code examples into VB.NET using the C# Code ⇆ VB.NET Code Converter — a handy tool developed by the Spire.Doc team.

The Conclusion

With the help of regular expressions, replacing text or even inserting images in Word documents using C# becomes a smooth and efficient process. Whether you're working on templates, reports, or personalized documents, this approach saves time and reduces manual work. Ready to try it out? Download Spire.Doc and get started with a free 30-day trial license—perfect for evaluation and small projects.

With the help of Spire.Doc for .NET, developers can easily set bullet style for the existing word documents via invoke p.ListFormat.ApplyBulletStyle() method to format. This article will show you how to convert HTML list into word and set the bullet style for the word list in C#.

Firstly, please view the effective screenshot for the result word document:

How to set word bullet style by appending the HTML code in C#

Here comes to the steps:

Step 1: Create a new document and add a section to the document.

Document document = new Document();
Section section=document.AddSection();

Step 2: Add word list to the paragraph by appending HTML.

Paragraph paragraph = section.AddParagraph();
paragraph.AppendHTML("<ol><li>Version 1</li><li>Version 2</li><li>Version 3</li></ol>");

Step 3: Set the bullet style for the paragraph.

foreach (Paragraph p in section.Paragraphs)
{
    p.ApplyStyle(BuiltinStyle.Heading2);

    if (p.ListFormat.CurrentListLevel != null)
    {
        p.ListFormat.ApplyBulletStyle();
    }
}

Step 4: Save the document to file

document.SaveToFile("result.docx",FileFormat.Docx);

Full codes:

// Create a new Word document object
Document document = new Document();

// Add a section to the document
Section section = document.AddSection();

// Add a paragraph to the section
Paragraph paragraph = section.AddParagraph();

// Append HTML content to the paragraph 
paragraph.AppendHTML("
  1. Version 1
  2. Version 2
  3. Version 3
"); // Loop through all paragraphs in the section foreach (Paragraph p in section.Paragraphs) { // Apply "Heading 2" built-in style to the paragraph p.ApplyStyle(BuiltinStyle.Heading2); // Check if the paragraph has listformat if (p.ListFormat.CurrentListLevel != null) { // Apply bullet style p.ListFormat.ApplyBulletStyle(); } } // Save the document to a file in DOCX format document.SaveToFile("result.docx", FileFormat.Docx); // Dispose document.Dispose();

How to Merge Cells in Excel Using C# and VB.NET

Working with large Excel files often means merging rows, columns, or specific cell ranges to keep the data clean and easy to read. You can merge cells in Excel using C# or VB.NET with just a few lines of code and even center the content both horizontally and vertically for a professional look. Whether it’s combining entire rows, joining columns, or formatting a selected range, these examples show how to automate the process and improve your workflow.

If you often work with Excel files in .NET projects, handling tasks like merging cells, formatting data, or generating reports, you’ll want a tool that makes the process fast and reliable. Spire.XLS is a lightweight library that lets you create, read, and modify Excel files directly in C# or VB.NET—without relying on Microsoft Office. You can add it to your project in seconds via NuGet:

PM> Install-Package Spire.XLS

From there, you’re ready to start automating your Excel workflow.

Merge Rows or Columns in Excel using C#

Merging rows or columns in Excel is a common way to organize data and create professional-looking spreadsheets. For example, you can merge the first row across multiple columns to form a clear, centered table header. Using C# code, this process can be automated easily, saving time and ensuring consistency across large Excel files.

Here's the code example of merging the first row and centering it in Excel using C#:

using Spire.Xls;
namespace MergeCells
{

    class Program
    {

        static void Main(string[] args)
        {
            // Create a Workbook instance and load an Excel file
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("\\Population.xlsx");

            // Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            // Merge the first row
            sheet.Rows[0].Merge();
            // Merge the first column
            //sheet.Columns[0].Merge();

            // Set the alignment to center horizontally and vertically
            sheet.Rows[0].Style.HorizontalAlignment = HorizontalAlignType.Center;
            sheet.Rows[0].Style.VerticalAlignment = VerticalAlignType.Center;

            // Save the Excel file
            workbook.SaveToFile("\\Mergerowsandcenter.xls");

        }
    }
}

Here's the preview of merging the first row in Excel:

Merge the First Row in an Excel file using C#

Code steps explained:

  • Create a Workbook object and read an Excel file.
  • Get a worksheet.
  • Merge a certain row using Worksheet.Rows[].Merge() method.
  • Set the alignment to center horizontally and vertically by adjusting the Style.HorizontalAlignment and Style.VerticalAlignment properties.
  • Save the modified Excel file.

The C# code above for merging rows in an Excel file can also be used to merge columns, which is indicated with comments in the code.

After merging cells to create a clean table header, you might sometimes need to undo the operation. You can easily unmerge cells in Excel in C# or VB.NET, restoring the original cell layout without affecting your data.

Merge a Specific Cell Range in Excel

Merging a specific cell range in Excel using C# is useful when you want to highlight a section of data, such as a summary row or a group of related columns. The process is very similar to merging entire rows or columns, and you can use the same Merge() method—applied to a CellRange object via Worksheet.Range[].Merge() method. By specifying the exact range to merge, you can create a clear, organized layout that improves readability and automates the operation across multiple sheets or files without manually adjusting each cell.

The code example below demonstrates how to merge the cell range "B6:E6" in an Excel worksheet using C#:

using Spire.Xls;
namespace MergeCells
{

    class Program
    {

        static void Main(string[] args)
        {
            // Create a Workbook instance and load an Excel file
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("\\Population.xlsx");

            // Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            // Merge the particular cell range in Excel 
            sheet.Range["B6:E6"].Merge();

            // Save the Excel file
            workbook.SaveToFile("\\Mergecellrange.xls");

        }
    }
}

Here's the preview of merging a cell range in an Excel file using C#:

Merge a Specified Cell Range in Excel with C#

Merge and Center Cells in Excel using VB.NET

Just like in C#, you can merge and center cells in Excel using VB.NET to create clean table headers or highlight key data sections. The steps are very similar, and the same Merge() method applies to rows, columns, or specific cell ranges. If you want, you can even convert C# examples to VB.NET using our handy C# ↔ VB.NET code converter, making it easy to adapt existing code for your project.

Here's a code example of merging the first row and centering it in Excel using VB.NET:

Imports Spire.Xls

Namespace MergeCells

    Friend Class Program

        Private Shared Sub Main(args As String())
            ' Create a Workbook instance and load an Excel file
            Dim workbook As Workbook = New Workbook()
            workbook.LoadFromFile("E:\Administrator\Python1\input\Population.xlsx")

            ' Get the first worksheet
            Dim sheet As Worksheet = workbook.Worksheets(0)

            ' Merge the first row
            sheet.Rows(0).Merge()
            ' Merge the first column
            'sheet.Columns[0].Merge();

            ' Set the alignment to center horizontally and vertically
            sheet.Rows(0).Style.HorizontalAlignment = HorizontalAlignType.Center
            sheet.Rows(0).Style.VerticalAlignment = VerticalAlignType.Center

            ' Save the Excel file
            workbook.SaveToFile("\Mergerowsandcenter.xls")

        End Sub
    End Class
End Namespace

The Conclusion

By following these examples, you can merge cells in Excel using C#, whether it’s entire rows, columns, or specific ranges. With the help of Spire.XLS, you can merge cells in Excel files automatically without hassle and create clear, professional-looking spreadsheets. Start streamlining your workflow today and get a 30-day free license to try all features without restrictions.

C# Excel to DataTable conversion using Spire.XLS

Working with Excel data is a common requirement in .NET development. Developers often need to convert Excel files into a DataTable, which provides a structured way to manipulate data, perform LINQ queries, bind to UI controls, or import into a database.

Although this task is common, the implementation details are not always straightforward. Converting Excel data into a DataTable in C# can be achieved in several ways, but traditional approaches often depend on OLEDB drivers or involve lengthy OpenXML programming, both of which add unnecessary complexity and external dependencies. In this guide, we’ll use Spire.XLS for .NET to simplify the process of importing Excel data into a DataTable with C# code. The library provides built-in methods to load Excel files, export worksheets or specific ranges, and work with the data directly in a DataTable.

Quick Navigation


What is a DataTable and Why Use It?

A DataTable in C# is a memory-resident representation of structured data. It allows developers to:

  • Store Excel data in tabular form.
  • Perform filtering, sorting, and LINQ queries.
  • Bind to UI components like DataGridView.
  • Bulk insert into relational databases.

Compared with DataSet, a DataTable is lightweight and especially well-suited for working with a single worksheet or range of data.


Preparing the Environment

Before starting, install Spire.XLS for .NET in your project. The easiest way is through NuGet:

Install-Package Spire.XLS

Then, import the required namespaces in your C# code:

using Spire.Xls;
using System.Data;

Converting Excel to DataTable in C#

The following sections demonstrate how to load an Excel file and convert its contents into a DataTable using Spire.XLS. Unlike older methods that depend on OLEDB, this library works without Microsoft Office installation, making it reliable for both desktop and server-side applications.

Load an Excel File

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");

This creates a Workbook object from the specified Excel file, which you can then use to access individual worksheets.

Convert a Worksheet to DataTable

Worksheet sheet = workbook.Worksheets[0];
DataTable dataTable = sheet.ExportDataTable(sheet.AllocatedRange, true);

Here, the entire first worksheet is exported to a DataTable.

  • sheet.AllocatedRange retrieves the used range of the worksheet.
  • The true parameter means the first row will be treated as column headers.

This approach is useful when you want to import all data from a sheet directly into a DataTable, for example when binding to a DataGridView or performing bulk inserts into a database.

Convert a Specific Range to DataTable

DataTable partialTable = sheet.ExportDataTable(sheet.Range["A1:C10"], true);

This snippet converts only the range A1:C10 into a DataTable. It’s a practical choice when dealing with large Excel files, where only part of the sheet is needed for processing, or when extracting a specific report section.

Complete Example: Excel Worksheet to DataTable with C#

The following example demonstrates the entire workflow of loading an Excel file and converting it into a DataTable. After running this code, you can process the table further, such as querying, filtering, or inserting into a database.

using Spire.Xls;
using System.Data;

class Program
{
    static void Main()
    {
        // Load Excel file
        Workbook workbook = new Workbook();
        workbook.LoadFromFile("Sample.xlsx");

        // Convert first worksheet to DataTable
        Worksheet sheet = workbook.Worksheets[0];
        DataTable dataTable = sheet.ExportDataTable(sheet.AllocatedRange, true);

        // Print row count
        Console.WriteLine("Rows imported: " + dataTable.Rows.Count);

        // Print column names
        foreach (DataColumn col in dataTable.Columns)
        {
            Console.Write(col.ColumnName + "\t");
        }
        Console.WriteLine();

        // Print all rows
        foreach (DataRow row in dataTable.Rows)
        {
            foreach (var item in row.ItemArray)
            {
                Console.Write(item + "\t");
            }
            Console.WriteLine();
        }
    }
}

This code loads the first worksheet from an Excel file, exports its content into a DataTable, and prints the table to the console. The following screenshot shows an example of the output:

C# Excel to DataTable Example Output

For scenarios where you need to read Excel data cell by cell, see our guide on reading Excel files in C#.


Bonus: Exporting DataTable Back to Excel in C#

In some cases, you may also need to write modified data from a DataTable back to Excel. This can be done easily:

Workbook wb = new Workbook();
Worksheet ws = wb.Worksheets[0];

// Assume dataTable is already populated
ws.InsertDataTable(dataTable, true, 1, 1);

wb.SaveToFile("Output.xlsx", ExcelVersion.Version2016);

This code inserts the DataTable starting at cell A1 and saves the file. It demonstrates the reverse workflow, ensuring smooth two-way data exchange between Excel and C#. For more details, you can check How to Export DataTable into Excel in C#.


Handling Large Excel Files and Performance Tips

When working with large Excel files, performance optimization is key. Here are a few best practices:

  • Read only the required worksheets instead of loading all.
  • Export specific ranges rather than entire sheets if possible.
  • Use stream-based methods (LoadFromStream) to avoid file locks.
  • Minimize memory usage by processing rows iteratively when handling very large DataTables.

Spire.XLS supports .xls, .xlsx, and .csv formats consistently, making it suitable across different scenarios.


Best Practices

When converting Excel to DataTable, keep in mind:

  • Data type handling: Excel stores values as text by default. Convert them to int, decimal, or DateTime as needed.
  • Null or empty cells: Always check for missing values to prevent runtime errors.
  • Database integration: Before bulk inserting into SQL Server or another database, validate and sanitize the DataTable.

Conclusion

Converting Excel data into a DataTable is a common but critical task in .NET development. With Spire.XLS for .NET, you can achieve this efficiently without relying on OLEDB drivers or complex OpenXML parsing.

Whether you need to convert an entire worksheet, extract a specific range, or write data back to Excel, the process remains straightforward and reliable. Mastering this workflow ensures smoother integration between Excel data and your C# applications.

If you want to unlock the full feature set of Spire.XLS, you can apply for a free temporary license. For smaller projects, you can also use Free Spire.XLS for .NET.


FAQ

Q: How do I import data from Excel to DataTable in C#?

Use Worksheet.ExportDataTable() to directly convert worksheet data into a DataTable.

Q: Can I read Excel into DataTable without OLEDB?

Yes. Spire.XLS does not require OLEDB or Microsoft Office installation.

Q: How to read only a specific worksheet or range into a DataTable?

Pass the target Worksheet or Range to ExportDataTable().

Q: Can I export a DataTable back to Excel in C#?

Yes. Use Worksheet.InsertDataTable() and then save the file with SaveToFile().

Page 12 of 12