In the previous article, I introduced how to convert PDF to Excel using a free third-party library called Free Spire.PDF for Java. Actually that article is to convert each PDF page to a single Excel worksheet, which means the number of pages determines the number of worksheets. This tutorial will demonstrate how to convert multiple PDF pages to one Excel worksheet using Spire.PDF for Java.
DEPENDENCY
First of all, you’re required to download the package of Spire.PDF for Java from the link, and then add Spire.Pdf.jar as a dependency in your Java program. If you use 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.pdf</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
USING THE CODE
Spire.PDF for Java supports setting the conversion option during converting PDF to Excel, for example, multiple pages can be rendered on a single worksheet. The following are detailed steps.
l Create a PdfDocument
instance.
l Load a sample PDF document including several pages using PdfDocument.loadFromFile() method.
l Get some options when doing conversion operation using PdfDocument.getConvertOptions() method,
and then set conversion options during converting PDF to Excel using PdfConvertOptions.setPdfToXlsxOptions()
method.
l Save the document to Excel using PdfDocument.saveToFile() method.
import com.spire.pdf.*;
import com.spire.pdf.conversion.XlsxLineLayoutOptions;
public class PDFToExcel {
public static void main(String[] args) {
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load a sample PDF document
pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\Test.pdf");
//Set the PDF to XLSX conversion options: rendering multiple pages on a single worksheet
pdf.getConvertOptions().setPdfToXlsxOptions(new XlsxLineLayoutOptions(false,true,true));
//Save the document to Excel
pdf.saveToFile("output/ToOneSheet.xlsx", FileFormat.XLSX);
}
}
No comments:
Post a Comment