翻譯|使用教程|編輯:李顯亮|2021-03-31 10:13:58.233|閱讀 251 次
概述:在某些情況下,需要處理PDF文件中的數據并將其添加到數據庫中。對于這種情況,將PDF文檔轉換為 CSV 格式可能會有所幫助。在本文中,將學習如何使用C ++以編程方式對PDF和CSV文件進行相互轉換。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
PDF 文件是通過Internet交換文檔的標準格式。在某些情況下,需要處理PDF文件中的數據并將其添加到數據庫中。對于這種情況,將PDF文檔轉換為 CSV 格式可能會有所幫助。另一方面,可能希望以只讀方式與某人共享一些CSV格式的表格數據。可以通過將CSV文件轉換為PDF格式來實現。在本文中,將學習如何使用C ++以編程方式對PDF和CSV文件進行相互轉換。
要實現這些轉換,您需要C ++ API的Aspose.Cells和C ++ API的Aspose.PDF。前者是用于創建,讀取和修改Excel文件的C ++庫,而后者是用于處理PDF文件的API。我們將使用Aspose.Cells for C ++ API將CSV文件轉換為PDF格式,并使用Aspose.PDF for C ++ API將PDF文件轉換為CSV格式。
以下是將CSV文件轉換為PDF格式的步驟。
以下是使用C ++將CSV文件轉換為PDF格式的示例代碼。
// 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);
以下是將PDF文件轉換為CSV格式的步驟。
以下是使用C ++將PDF文件轉換為CSV格式的示例代碼。
// 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);
Aspose.PDF for C ++還使您能夠在轉換的CSV文件中包含特定的PDF頁面。為此,可以按照以下步驟操作。
以下是在CSV文件中包含選定PDF頁面的示例代碼。
// 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);
使用Aspose.PDF for C ++,您還可以將PDF頁面轉換為單個CSV文件。為此,您可以按照以下步驟操作。
以下是使用C ++將PDF頁面轉換為單個CSV文件的示例代碼。
// 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); }
如果你想試用Aspose的全部完整功能,可聯系在線客服獲取30天臨時授權體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn