Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active March 27, 2021 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/c6dbbba95ae31ff9c526b37ca170b929 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/c6dbbba95ae31ff9c526b37ca170b929 to your computer and use it in GitHub Desktop.
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