PDF files are a standard format for exchanging documents over the internet. There might be situations where you need to process the data in the PDF file and add that to a database. For such scenarios, converting the PDF document to CSV format might prove to be helpful. On the other hand, you might have some tabular data in CSV format that you want to share in a read-only manner with someone. You can achieve this by converting the CSV files to PDF format. In this article, you will learn how to interconvert PDF and CSV files programmatically using C++.
- C++ API to Convert CSV to PDF and PDF to CSV Format
- Convert CSV Files to PDF Format
- Converting a PDF File to CSV Format
- Convert Selected PDF Pages to a CSV File
- Converting PDF Pages to Individual CSV Files
- Get a Free License
C++ API to Convert CSV to PDF and PDF to CSV Format
To achieve these conversions, you will need Aspose.Cells for C++ and Aspose.PDF for C++ APIs. The former is a C++ library for creating, reading, and modifying Excel files, whereas the latter is an API for working with PDF files. We will use Aspose.Cells for C++ API to convert CSV files to PDF format and Aspose.PDF for C++ API to convert PDF files to CSV format. You can either install the APIs through NuGet or download them directly from the Downloads section.
PM> Install-Package Aspose.Cells.Cpp
PM> Install-Package Aspose.PDF.Cpp
Convert CSV Files to PDF Format
The following are the steps to convert CSV files to PDF format.
- Create an instance of the ILoadOptions class.
- Load the CSV file by creating an object of the IWorkbook class using the ILoadOptions instance created earlier.
- Save the file in PDF format using the IWorkbook->Save (intrusive_ptr<Aspose::Cells::Systems::String> fileName, Aspose::Cells::SaveFormat saveFormat) method.
The following is the sample code to convert CSV files to PDF format using C++.
Converting a PDF File to CSV Format
The following are the steps to convert a PDF file to CSV format.
- Load the PDF file using the Document class.
- Create an instance of the ExcelSaveOptions class.
- Set the format to CSV using ExcelSaveOptions->set_Format (ExcelSaveOptions::ExcelFormat value) method.
- Save the CSV file using the Document->Save (System::String outputFileName, System::SharedPtr<SaveOptions> options) method.
The following is the sample code to convert a PDF file to CSV format using C++.
Convert Selected PDF Pages to a CSV File
Aspose.PDF for C++ also provides you the ability to include specific PDF pages in the converted CSV file. To achieve this, you can follow the following steps.
- Load the PDF file using the Document class.
- Create an instance of the ExcelSaveOptions class.
- Set the format to CSV using ExcelSaveOptions->set_Format (ExcelSaveOptions::ExcelFormat value) method.
- Create a new object of the Document class to represent the CSV file.
- Loop through the pages of the PDF file using the Document->get_Pages() method.
- Within the loop, specify the condition for selecting the desired pages.
- Add the pages to the newly created Document object using the Document->get_Pages()->Add (System::SharedPtr<Page> const & entity) method.
- Save the CSV file using the Document->Save (System::String outputFileName, System::SharedPtr<SaveOptions> options) method outside the loop.
The following is the sample code to include selected PDF pages in the CSV file.
Converting PDF Pages to Individual CSV Files
With Aspose.PDF for C++, you can also convert PDF pages to individual CSV files. To achieve this, you can follow the following steps.
- Load the PDF file using the Document class.
- Create an instance of the ExcelSaveOptions class.
- Set the format to CSV using ExcelSaveOptions->set_Format (ExcelSaveOptions::ExcelFormat value) method.
- Loop through the pages of the PDF file using the Document->get_Pages() method.
- Within the loop, create a new object of the Document class to represent the CSV file.
- Add the pages to the newly created Document object using the Document->get_Pages()->Add (System::SharedPtr<Page> const & entity) method.
- Save the CSV file using the Document->Save (System::String outputFileName, System::SharedPtr<SaveOptions> options) method.
The following is the sample code to convert PDF pages to individual CSV files using C++.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to convert CSV files to PDF and PDF files to CSV format. Furthermore, you have seen how to add specific pages to the generated CSV file or convert PDF pages to individual CSV files. Aspose.Cells for C++ and Aspose.PDF for C++ are powerful APIs for working with Excel and PDF files, respectively. You can explore the APIs in detail by visiting their official documentation. In case of any questions, please feel free to reach us on our free support forum.