PDF files are the standard format for exchanging documents over the Internet. In some cases, you need to process the data in the PDF file and add it to the database. In this case, converting PDF documents to CSV format may be helpful. On the other hand, you might want to share some table data in CSV format with someone in read-only mode. This can be done by converting CSV files to PDF format. In this article, you’ll learn how to programmatically convert PDF and CSV files to and from each other using C ++.

  • Convert the CSV file to PDF format
  • Convert PDF files to CSV format
  • Convert the selected PDF page to a CSV file
  • Convert PDF pages to a single CSV file

To implement these transformations, you need ASposed.Cells for the C ++ API and asposed.PDF for the C ++ API. The former is a C ++ library for creating, reading, and modifying Excel files, while the latter is an API for processing PDF files. We will use asposite.Cells for C ++ API to convert CSV files to PDF format and asposite.pdf for C ++ API to convert PDF files to CSV format.

Convert the CSV file to PDF format

Here are the steps to convert a CSV file 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 you created earlier.
  • Use the IWorkbook-> Save (intrusive_ptrfileName, Aspose :: Cells :: SaveFormat SaveFormat) method to Save the file to PDF format.

Here is sample code to convert a CSV file to PDF format using C ++.

// Source directory path.
StringPtr srcDir = new String("SourceDirectory\\");

// Output directory path.
StringPtr outDir = new String("OutputDirectory\\");

// Create CSV LoadOptions object
intrusive_ptrloadOptions = Factory::CreateILoadOptions(LoadFormat_CSV);

// Load the input Excel file
intrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Sample1.csv")), loadOptions);

// Save as PDF file
workbook->Save(outDir->StringAppend(new String("Sample1_out.pdf")), SaveFormat_Pdf);
Copy the code

Convert PDF files to CSV format

Here are the steps to convert a PDF file to CSV format.

  • Load PDF files using the Document class.
  • Create an instance of the ExcelSaveOptions class.
  • Use the ExcelSaveOptions-> set_Format (ExcelSaveOptions :: ExcelFormat value) method to set the format to CSV.
  • Use document -> Save (System :: String outputFileName, System :: SharedPtr option) to save the CSV file.

Here is sample code to convert a PDF file to CSV format using C ++.

// Load PDF file
auto pdfDocument = MakeObject(u"SourceDirectory\\Sample2_csv.pdf");

// Initialize ExcelSaveOptions class object
auto options = MakeObject();
options->ConversionEngine = ExcelSaveOptions::ConversionEngines::NewEngine;

// Set save format as CSV
options->set_Format(ExcelSaveOptions::ExcelFormat::CSV);

// Save as CSV file
pdfDocument->Save(u"OutputDirectory\\Sample2_csv_out.csv", options);
Copy the code

Convert the selected PDF page to a CSV file

Aspose.PDF for C ++ also enables you to include specific PDF pages in converted CSV files. To do this, follow these steps.

  • Load PDF files using the Document class.
  • Create an instance of the ExcelSaveOptions class.
  • Use the ExcelSaveOptions-> set_Format (ExcelSaveOptions :: ExcelFormat value) method to set the format to CSV.
  • Create a new object of the Document class to represent the CSV file.
  • Use the Document-> get_Pages () method to loop through the pages of the PDF file.
  • In the loop, specify the criteria for selecting the desired page.
  • Use the Document-> get_Pages () -> Add (System: : SharedPtr constants and entities) method to Add the page to the newly created Document object.
  • Save the CSV file outside the loop using the Document -> Save (System :: String outputFileName, System :: SharedPtr option) method.

The following is sample code for including selected PDF pages in a CSV file.

// Load PDF file
auto pdfDocument = MakeObject(u"SourceDirectory\\Sample2_csv.pdf");

// Initialize ExcelSaveOptions class object
auto options = MakeObject();
options->ConversionEngine = ExcelSaveOptions::ConversionEngines::NewEngine;

// Set save format as CSV
options->set_Format(ExcelSaveOptions::ExcelFormat::CSV);

// Create an instance of the Document class to represent the CSV file.
auto newPdfDocument = MakeObject();

for (int i = 1; i <= pdfDocument->get_Pages()->get_Count(); i++)
{
	// Get first 2 pages
	if (i <= 2) { // Add the page to the new Document instance newPdfDocument->get_Pages()->Add(pdfDocument->get_Pages()->idx_get(i));
	}
}

// Save as CSV file
newPdfDocument->Save(u"OutputDirectory\\Sample2_csv_out.csv", options);
Copy the code

Convert PDF pages to a single CSV file

PDF for C ++, you can also convert PDF pages into a single CSV file. To do this, you can follow these steps.

  • Load PDF files using the Document class.
  • Create an instance of the ExcelSaveOptions class.
  • Use the ExcelSaveOptions-> set_Format (ExcelSaveOptions :: ExcelFormat value) method to set the format to CSV.
  • Use the Document-> get_Pages () method to loop through the pages of the PDF file.
  • In the loop, create a new object of the Document class to represent the CSV file.
  • Use the Document-> get_Pages () -> Add (System: : SharedPtr constants and entities) method to Add the page to the newly created Document object.
  • Use document -> Save (System :: String outputFileName, System :: SharedPtr option) to save the CSV file.

Here is sample code to convert a PDF page to a single CSV file using C ++.

// Load PDF file
auto pdfDocument = MakeObject(u"SourceDirectory\\Sample2_csv.pdf");

// Initialize ExcelSaveOptions class object
auto options = MakeObject();
options->ConversionEngine = ExcelSaveOptions::ConversionEngines::NewEngine;

// Set save format as CSV
options->set_Format(ExcelSaveOptions::ExcelFormat::CSV);

for (int i = 1; i <= pdfDocument->get_Pages()->get_Count(); i++)
{
	// Create an instance of the Document class to represent the CSV file.
	auto newPdfDocument = MakeObject();

	// Add the page to the new Document instance
	newPdfDocument->get_Pages()->Add(pdfDocument->get_Pages()->idx_get(i));

	// Save as CSV file
	newPdfDocument->Save(u"OutputDirectory\\Sample2_csv_out_" + System::Convert::ToString(i) + u".csv", options);
}
Copy the code

If you have any questions or requirements, please feel free to join the Aspose Technology Exchange Group (761297826), we are happy to provide you with inquiries and consultation.