6 Scenarios Of Converting Excel To PDF Using Java

Excel files are widely used to record and process data. But when it comes to document distribution, people often consider converting them to PDF. Since PDF files can be easily accessed on almost all devices even if the viewers don’t have a special application such as Adobe Reader installed, moreover, they will always look exactly the same no matter what software or operating systems are used to view them. This article will explain how to convert Excel to PDF using Java, it consists of the following conversion scenarios:

  • Convert an Excel Workbook to PDF
  • Convert a Worksheet to PDF
  • Convert a Worksheet Range to PDF
  • Convert Excel to PDF/A
  • Convert Excel to Encrypted PDF
  • Convert Excel to PDF with Specific Image Quality

Add Dependencies

This article uses Spire.XLS for Java API to achieve Excel to PDF conversion. Before coding, you need to add needed dependencies for including the API into your Java project. There are two ways to do that.

Method 1: If you are using maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<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>5.4.3</version>
    </dependency>
</dependencies>

Method 2: If you are not using maven, you can download the JAR file from this link, extract the zip file and then import the Spire.Xls.jar file under the lib folder into your project as a dependency.

Convert an Excel Workbook to PDF in Java 

Spire.XLS for Java provides a Workbook class that represents an Excel workbook. This class exposes a loadFromFile(filePath) method to load an Excel workbook, and a saveToFile(filePath, FileFormat) method to save an Excel workbook to other file formats, including PDF, CSV, HTML, ODS, Open XML and more.

The following example explains how to convert an Excel workbook to PDF in Java:

import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;

public class ConvertWorkbookToPdf {
    public static void main(String []args) {
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load an Excel file
        workbook.loadFromFile("Test.xlsx");

        //Fit all worksheets on one page (optional)
        workbook.getConverterSetting().setSheetFitToPage(true);

        //Save the workbook to PDF
        workbook.saveToFile("WorkbookToPdf.pdf", FileFormat.PDF);
    }
}

The input Excel:

Convert an Excel Workbook to PDF in Java

The converted PDF:

Convert an Excel Workbook to PDF in Java

Convert a Worksheet to PDF in Java

If you just want to convert a particular worksheet instead of the whole workbook to PDF, you can access the worksheet by its index (zero-based) using Workbook.getWorksheets().get(sheetIndex) method, and then use Worksheet.saveToPdf() method to save the worksheet to PDF.

The following code example explains how to save the second worksheet of an Excel workbook to PDF in Java:

import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class ConvertWorksheetToPdf {
    public static void main(String []args) {
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load an Excel file
        workbook.loadFromFile("Test.xlsx");

        //Get the second worksheet
        Worksheet worksheet = workbook.getWorksheets().get(1);
        //Fit the worksheet on one page (optional)
        worksheet.getPageSetup().isFitToPage(true);

        //Save the worksheet to PDF
        worksheet.saveToPdf("WorksheetToPdf.pdf");
    }
}

The converted PDF:

Convert a Worksheet to PDF in Java

Convert a Worksheet Range to PDF in Java

To convert a specific worksheet range to PDF, you can use the Worksheet.getPageSetup().setPrintArea() method and pass the worksheet range in the form of String as an input parameter, then call Worksheet.saveToPdf() method to save the worksheet to PDF.

The following example explains how to convert a worksheet range to PDF in Java:

import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class ConvertWorksheetRangeToPdf {
    public static void main(String []args) {
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load an Excel file
        workbook.loadFromFile("Test.xlsx");

        //Get the first worksheet
        Worksheet worksheet = workbook.getWorksheets().get(0);
        //Set the desired cell range as print area
        worksheet.getPageSetup().setPrintArea("B3:C8");

        //Save the worksheet to PDF
        worksheet.saveToPdf("CellRangeToPdf.pdf");
    }
}

The converted PDF:

Convert a Worksheet Range to PDF in Java

Convert Excel to PDF/A in Java

PDF/A is an ISO-standardized version of PDF designed for long-term archiving and preservation. To convert an Excel file to PDF/A, you can use the workbook.getConverterSetting().setPdfConformanceLevel() method and pass a PdfConformanceLevel enumeration value as an input parameter, then call Workbook.saveToFile(filePath, FileFormat.PDF) method to save the workbook to PDF.

The PdfConformanceLevel enumeration has the following members:

  • PDF_A_1_A
  • PDF_A_1_B
  • PDF_A_2_A
  • PDF_A_2_B
  • PDF_A_2_U
  • PDF_A_3_A
  • PDF_A_3_B
  • PDF_A_3_U
  • Pdf_X_1_A_2001
  • None

The following example explains how to convert an Excel file to a PDF/A-1a compliant document in Java:

import com.spire.pdf.PdfConformanceLevel;
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;

public class ConvertExcelToPdfA {
    public static void main(String []args) {
        //Create a Workbook instance
        Workbook workbook = new Workbook();

        //Load an Excel file
        workbook.loadFromFile("Test.xlsx");

        //Fit all worksheets on one page (optional)
        workbook.getConverterSetting().setSheetFitToPage(true);
        //Set the conformance level of the PDF as PdfConformanceLevel.Pdf_A_1_A
        workbook.getConverterSetting().setPdfConformanceLevel(PdfConformanceLevel.Pdf_A_1_A);

        //Save the workbook to PDF
        workbook.saveToFile("ExcelToPdfA.pdf", FileFormat.PDF);
    }
}

The converted PDFA/1-a document:

Convert Excel to PDF/A in Java

Convert Excel to Encrypted PDF in Java

Password encryption is one of the most powerful ways to protect your confidential and sensitive data from unauthorized access. You can password protect the PDF by using Workbook.getConverterSetting().getPdfSecurity().encrypt(openPassword, permissionPassword, PdfPermissionsFlags, PdfEncryptionKeySize) method.

The following example explains how to convert an Excel workbook to encrypted PDF in Java:

import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;

public class ConvertExcelToEncryptedPdf {
    public static void main(String []args) {
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load an Excel file
        workbook.loadFromFile("Test.xlsx");

        //Fit all worksheets on one page (optional)
        workbook.getConverterSetting().setSheetFitToPage(true);
        //Encrypt the PDF with open password and permission password
        workbook.getConverterSetting().getPdfSecurity().encrypt("123456", "123456", PdfPermissionsFlags.Fill_Fields, PdfEncryptionKeySize.Key_256_Bit);

        //Save the workbook to PDF
        workbook.saveToFile("ExcelToEncryptedPdf.pdf", FileFormat.PDF);
    }
}

When opening the converted PDF, the following dialog box appears:

Convert Excel to Encrypted PDF in Java

Convert Excel to PDF with Specific Image Quality in Java

You can set the image quality you want to get in the converted PDF file using workbook.getConverterSetting().setJPEGQuality(value) method. The value can be set from 0 to 100.

The following example explains how to set image quality when converting Excel to PDF in Java:

import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;

public class ConvertExcelToPdfWithJpegQuality {
    public static void main(String []args) {
                //Create a Workbook instance
                Workbook workbook = new Workbook();

                //Load an Excel file
                workbook.loadFromFile("Test.xlsx");

                //Fit all worksheets on one page (optional)
                workbook.getConverterSetting().setSheetFitToPage(true);
                //Set Jpeg quality (0-100)
                workbook.getConverterSetting().setJPEGQuality(50);

                //Save the workbook to PDF
                workbook.saveToFile("ExcelToPdfWithImageQuality.pdf", FileFormat.PDF);
    }
}


Similar Articles