Convert PDF to Excel using C++

·

2 min read

If you need to edit the data in a PDF table, you can convert it into an Excel document for further processing. This article will share how to convert PDF to Excel in C++ using Spire.PDF for C++ library.

  • Convert PDF to Excel

  • Convert a Multi-Page PDF to One Excel Worksheet

Install the Library

Spire.PDF for C++ is a professional PDF API applied to creating, writing, editing, handling and reading PDF files without any external dependencies within C++ application. To install it, you can refer to the below tutorial.

Integrate Spire.PDF for C++ in a C++ Application

Convert PDF to Excel in C++

Spire.PDF for C++ provides the PdfDocument->SaveToFile(LPCWSTR_S filename, FileFormat::XLSX) method to convert each PDF page to a single Excel worksheet. The complete sample code is shown below.

#include "Spire.Pdf.o.h"

using namespace Spire::Pdf;
using namespace std;

int main() {
    //Specify input and output file paths
    wstring inputFile = L"Data\\Sample.pdf";
    wstring outputFile = L"Output\\ToXLSX.xlsx";

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

    //Load a pdf document
    doc->LoadFromFile(inputFile.c_str());

    //Save to XLSX
    doc->SaveToFile(outputFile.c_str(), FileFormat::XLSX);
}

Convert a Multi-Page PDF to One Excel Worksheet in C++

With Spire.PDF for C++, you are also allowed to set the PDF to XLSX conversion options to render multiple PDF pages on a single worksheet using PdfDocument->GetConvertOptions()->SetPdfToXlsxOptions() method. The complete sample code is shown below.

#include "Spire.Pdf.o.h"

using namespace Spire::Pdf;
using namespace std;

int main() {
    //Specify input and output file paths
    wstring inputFile = L"Data\\Sample.pdf";
    wstring outputFile = L"Output\\ToOneSheet.xlsx";

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

    //Load a pdf document
    doc->LoadFromFile(inputFile.c_str());

    //Set the PDF to XLSX conversion options to render multiple pages on a single worksheet
    doc->GetConvertOptions()->SetPdfToXlsxOptions(new XlsxLineLayoutOptions(false, true, true));    

    //Save the pdf file to excel
    doc->SaveToFile(outputFile.c_str(), FileFormat::XLSX);
    doc->Close();
    delete doc;
}