PDF 文件是通过Internet交换文档的标准格式。在某些情况下,需要处理PDF文件中的数据并将其添加到数据库中。对于这种情况,将PDF文档转换为 CSV 格式可能会有所帮助。另一方面,可能希望以只读方式与某人共享一些CSV格式的表格数据。可以通过将CSV文件转换为PDF格式来实现。在本文中,将学习如何使用C ++以编程方式对PDF和CSV文件进行相互转换。
- 将CSV文件转换为PDF格式
- 将PDF文件转换为CSV格式
- 将选定的PDF页面转换为CSV文件
- 将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格式
以下是将CSV文件转换为PDF格式的步骤。
- 创建ILoadOptions 类的实例 。
- 通过 使用先前 创建的ILoadOptions实例创建IWorkbook类 的对象来加载CSV文件 。
- 使用IWorkbook-> Save(intrusive_ptrfileName,Aspose :: Cells :: SaveFormat saveFormat) 方法将文件保存为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格式
以下是将PDF文件转换为CSV格式的步骤。
- 使用Document 类加载PDF文件 。
- 创建一个ExcelSaveOptions类的实例。
- 使用ExcelSaveOptions-> set_Format(ExcelSaveOptions :: ExcelFormat值)方法将格式设置为CSV 。
- 使用“文档”->“保存”(System :: String outputFileName,System :: SharedPtr选项)方法保存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);
将选定的PDF页面转换为CSV文件
Aspose.PDF for C ++还使您能够在转换的CSV文件中包含特定的PDF页面。为此,可以按照以下步骤操作。
- 使用Document 类加载PDF文件 。
- 创建一个ExcelSaveOptions类的实例。
- 使用ExcelSaveOptions-> set_Format(ExcelSaveOptions :: ExcelFormat值)方法将格式设置为CSV 。
- 创建Document 类的新对象以表示CSV文件。
- 使用Document-> get_Pages() 方法循环浏览PDF文件的页面 。
- 在循环中,指定选择所需页面的条件。
- 使用Document-> get_Pages()-> Add(系统:: SharedPtr常量和实体)方法将页面添加到新创建的Document对象。
- 在循环外使用“文档”->“保存”(System :: String outputFileName,System :: SharedPtr选项)方法保存CSV文件。
以下是在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);
将PDF页面转换为单个CSV文件
使用Aspose.PDF for C ++,您还可以将PDF页面转换为单个CSV文件。为此,您可以按照以下步骤操作。
- 使用Document 类加载PDF文件 。
- 创建一个ExcelSaveOptions类的实例。
- 使用ExcelSaveOptions-> set_Format(ExcelSaveOptions :: ExcelFormat值)方法将格式设置为CSV 。
- 使用Document-> get_Pages() 方法循环浏览PDF文件的页面 。
- 在循环中,创建Document 类的新对象以表示CSV文件。
- 使用Document-> get_Pages()-> Add(系统:: SharedPtr常量和实体)方法将页面添加到新创建的Document对象。
- 使用“文档”->“保存”(System :: String outputFileName,System :: SharedPtr选项)方法保存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技术交流群(761297826),我们很高兴为您提供查询和咨询。