Convert Excel to PDF/TIFF in Java

NateBennett
2 min readNov 25, 2020

--

In my previous article, I’ve introduced how to convert Excel to Image by using Free Spire.XLS for Java. Today, I will share how to convert Excel to PDF/TIFF in Java Application.

Installation
Method 1: Download the Free Spire.XLS for Java and unzip it. Then add the Spire.Xls.jar file to your project as dependency.

Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>

【Example 1】Convert Excel to PDF

import com.spire.xls.*;

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

//Load the Excel document
Workbook workbook = new Workbook();
workbook.loadFromFile("file1.xlsx");

//Fit to page
workbook.getConverterSetting().setSheetFitToPage(true);

//Convert the whole Excel workbook to PDF
workbook.saveToFile("ExcelToPDF.pdf",FileFormat.PDF);

//Convert the first Excel worksheet to PDF
//Worksheet worksheet = workbook.getWorksheets().get(0);
//worksheet.saveToPdf("ToPDF2.pdf");
}
}

Output:

【Example 2】Convert Excel to TIFF

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

public class ExcelToTIFF {
public static void main(String[] args) {
//Load the Excel document
Workbook workbook = new Workbook();
workbook.loadFromFile("file1.xlsx");

//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);

//Save the first worksheet to TIFF
sheet.saveToTiff("SheetToTiff.tiff");

//Save a particular cell range in the first worksheet to TIFF
//sheet.saveToTiff("CellRangeToTiff.tiff",1,1,5,2);
}
}

Output:

--

--

NateBennett

Sharing code to help developers deal with office files