DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

Java Convert PDF to Excel and Excel to PDF

In our daily lives, we use PDFs and Excels in a variety of ways. We know the data in PDFs can't be edited easily, whereas the data in an Excel spreadsheet can be freely edited. Occasionally, we don't want others to change the data; at other times, we need to edit the data in the files. In this article, I am going to show you how to convert PDF document to Excel and Excel to PDF by using Spire.Office for Java from the following four parts.

  • Convert each PDF page to a single Excel worksheet
  • Convert a Multi-Page PDF to One Excel Worksheet
  • Convert a Whole Excel File to PDF
  • Convert a Specific Worksheet to PDF

Install Spire.Office.jar

Spire.Office for Java supports to operate Word, Excel, PowerPoint and PDF files from Java. If you’re creating a Maven project, you can easily add the jar in your application using the following configurations. For non-Maven projects, download the jar file from this link and add it as a dependency in your application.

<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.office</artifactId>
        <version>4.12.2</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Convert each PDF page to a single Excel worksheet

The details steps to convert each PDF page to a single Excel worksheet.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.loadFromFile() method.
  • Save the PDF file to Excel using PdfDocument.saveToFile() method.
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;

public class PdftoExcel {
    public static void main(String[] args) throws Exception {

        //create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        //Load a sample PDF file
        pdf.loadFromFile("Sample.pdf");

        //Save to Excel
        pdf.saveToFile("out/ToExcel.xlsx", FileFormat.XLSX);

        }
    }
Enter fullscreen mode Exit fullscreen mode

pdf to excel

Convert a Multi-Page PDF to One Excel Worksheet

The details steps to convert a PDF file containing 2 pages to one Excel worksheet.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.loadFromFile() method.
  • Set the PDF to XLSX conversion options to render multiple PDF pages on a single worksheet using PdfDocument.getConvertOptions().setPdfToXlsxOptions() method.
  • Save the PDF file to Excel using PdfDocument.saveToFile() method.
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.conversion.XlsxLineLayoutOptions;


public class PdftoExcel {
    public static void main(String[] args) throws Exception {

        //create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();
        //Load a sample PDF file
        pdf.loadFromFile("Sample.pdf");

        //Set the PDF to XLSX conversion options: rendering multiple pages on a single worksheet
        pdf.getConvertOptions().setPdfToXlsxOptions(new XlsxLineLayoutOptions(false,true,true));

        //Save to Excel
        pdf.saveToFile("out/ToOneSheet.xlsx", FileFormat.XLSX);

        }
    }
Enter fullscreen mode Exit fullscreen mode

Convrt the PDF to one Excel worksheet

Convert a Whole Excel File to PDF

The details steps to convert an Excel Workbook with two worksheets to a PDF.

  • Create a Workbook object.
  • Load a sample Excel document using Workbook.loadFromFile() method.
  • Set the Excel to PDF conversion options through the methods under the ConverterSetting object, which is returned by Workbook.getConverterSetting() method.
  • Convert the whole Excel document to PDF using Workbook.saveToFile() method.
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;

public class ExceltoPDF {
    public static void main(String[] args) throws Exception {

        //Create a Workbook instance and load an Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx");

        //Set worksheets to fit to page when converting
        workbook.getConverterSetting().setSheetFitToPage(true);

        //Save the resulting document to a specified path
        workbook.saveToFile("out/ExcelToPdf.pdf", FileFormat.PDF);
        }
    }
Enter fullscreen mode Exit fullscreen mode

Excel to PDF

Convert a Specific Worksheet to PDF

The details steps to convert a specific Excel worksheet to a PDF.

  • Create a Workbook object and load a sample Excel document using Workbook.loadFromFile() method.
  • Set the Excel to PDF conversion options through the methods under the ConverterSetting object, which is returned by Workbook.getConverterSetting() method.
  • Get a specific worksheet using Workbook.getWorksheets().get() method.
  • Convert the worksheet to PDF using Worksheet.saveToPdf() method.
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class ExceltoPDF {
    public static void main(String[] args) throws Exception {

        //Create a Workbook instance and load an Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx");

        //Set worksheets to fit to page when converting
        workbook.getConverterSetting().setSheetFitToPage(true);

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

        //Save the resulting document to a specified path
        worksheet.saveToPdf("out/WorksheetToPdf.pdf");

        }
    }
Enter fullscreen mode Exit fullscreen mode

Convert Excel worksheet to PDF

Discussion (0)