Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert CSV to PDF and PDF to CSV format using C++
Convert CSV to PDF and PDF to CSV format using C++
// Source directory path.
StringPtr srcDir = new String("SourceDirectory\\");
// Output directory path.
StringPtr outDir = new String("OutputDirectory\\");
// Create CSV LoadOptions object
intrusive_ptr<ILoadOptions> loadOptions = Factory::CreateILoadOptions(LoadFormat_CSV);
// Load the input Excel file
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("Sample1.csv")), loadOptions);
// Save as PDF file
workbook->Save(outDir->StringAppend(new String("Sample1_out.pdf")), SaveFormat_Pdf);
// Load PDF file
auto pdfDocument = MakeObject<Document>(u"SourceDirectory\\Sample2_csv.pdf");
// Initialize ExcelSaveOptions class object
auto options = MakeObject<ExcelSaveOptions>();
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<Document>();
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);
// Load PDF file
auto pdfDocument = MakeObject<Document>(u"SourceDirectory\\Sample2_csv.pdf");
// Initialize ExcelSaveOptions class object
auto options = MakeObject<ExcelSaveOptions>();
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<Document>();
// 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);
}
// Load PDF file
auto pdfDocument = MakeObject<Document>(u"SourceDirectory\\Sample2_csv.pdf");
// Initialize ExcelSaveOptions class object
auto options = MakeObject<ExcelSaveOptions>();
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment