将 Excel 工作簿转换为 PDF
将 Excel 工作簿转换为 PDF
PDF 文件广泛用于组织、政府部门和个人之间交换文件。它是一种标准文档格式,软件开发人员经常被要求找到一种方法将 Microsoft Excel 文件转换为 PDF 文档。
Aspose.Cells 支持将 Excel 文件转换为 PDF,并在转换中保持高视觉保真度。
Aspose.Cells直接在输出文件中写入API和版本号的信息。例如,在将 Document 呈现为 PDF 时,Aspose.Cells for C++ 将填充应用值为“Aspose.Cells”的字段和PDF 制片人具有值的字段,例如“Aspose.Cells v18.5.0”。
请注意,您不能指示 Aspose.Cells for C++ 更改或从输出文档中删除此信息。
直接转换
Aspose.Cells支持独立于其他软件从电子表格转换为PDF。只需使用 将 Excel 文件保存到 PDF工作簿班级'救球方法。这救球方法提供了保存格式_Pdf将本机 Excel 文件转换为 PDF 格式的枚举成员。
按照以下步骤将 Excel 电子表格直接转换为 PDF 格式:
- 实例化一个对象工作簿通过调用它的空构造函数来类。
- 如果您从头开始创建工作簿,您可以打开/加载现有模板文件或跳过此步骤。
- 使用 Aspose.Cells' API 在电子表格上执行任何工作(输入数据、应用格式、设置公式、插入图片或其他绘图对象等)。
- 电子表格代码完成后,调用工作簿班级'救球保存电子表格的方法。
文件格式应为 PDF,因此从 SaveFormat 枚举中选择相关的 PDF(预定义值)以生成最终的 PDF 文档
请看下面的示例代码,其示例 Excel 文件和输出 PDF供你参考。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
// Source directory path. | |
StringPtr srcDir = new String("..\\Data\\01_SourceDirectory\\"); | |
// Output directory path. | |
StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\"); | |
// Path of input Excel file | |
StringPtr sampleConvertExcelWorkbookToPDF = srcDir->StringAppend(new String("sampleConvertExcelWorkbookToPDF.xlsx")); | |
// Path of output Pdf file | |
StringPtr outputConvertExcelWorkbookToPDF = outDir->StringAppend(new String("outputConvertExcelWorkbookToPDF_DirectConversion.pdf")); | |
// Load the sample Excel file. | |
intrusive_ptr<Aspose::Cells::IWorkbook> workbook = Factory::CreateIWorkbook(sampleConvertExcelWorkbookToPDF); | |
// Save the Excel Document in PDF format | |
workbook->Save(outputConvertExcelWorkbookToPDF, SaveFormat_Pdf); |
高级转换
您也可以选择使用IPDF保存选项类来为转换设置不同的属性。设置不同的属性IPDF保存选项类使您可以控制输出 PDF 的打印、字体、安全和压缩设置。最重要的属性是设置合规性这使您能够将 Excel 文件保存为 PDF/A 兼容的 PDF 文件。
将工作簿保存到 PDF/A 编译文件
以下代码片段演示了如何使用IPDF保存选项将 Excel 文件保存为 PDF/A 兼容的 PDF 格式的类
请看下面的示例代码及其输出 PDF供你参考。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
// Output directory path. | |
StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\"); | |
// Path of output Pdf file. | |
StringPtr outputConvertExcelWorkbookToPDF = outDir->StringAppend(new String("outputConvertExcelWorkbookToPDF_PdfCompliance_PdfA1b.pdf")); | |
// Create an empty workbook. | |
intrusive_ptr<Aspose::Cells::IWorkbook> workbook = Factory::CreateIWorkbook(); | |
// Access first worksheet. | |
intrusive_ptr<Aspose::Cells::IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Access cell A1. | |
intrusive_ptr<Aspose::Cells::ICell> cell = worksheet->GetICells()->GetObjectByIndex(new String("A1")); | |
// Add some text in cell. | |
cell->PutValue((StringPtr)new String("Testing PDF/A")); | |
// Create pdf save options object. | |
intrusive_ptr<Aspose::Cells::IPdfSaveOptions> pdfSaveOptions = Factory::CreateIPdfSaveOptions(); | |
// Set the compliance to PDF/A-1b. | |
pdfSaveOptions->SetCompliance(Aspose::Cells::Rendering::PdfCompliance_PdfA1b); | |
// Save the Excel Document in PDF format | |
workbook->Save(outputConvertExcelWorkbookToPDF, pdfSaveOptions); |
设置PDF创建时间
随着IPDF保存选项类,可以获取或设置PDF创建时间。
请看下面的示例代码及其输出 PDF供你参考。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
// Output directory path. | |
StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\"); | |
// Path of output Pdf file. | |
StringPtr outputConvertExcelWorkbookToPDF = outDir->StringAppend(new String("outputConvertExcelWorkbookToPDF_PDFCreationTime.pdf")); | |
// Create an empty workbook. | |
intrusive_ptr<Aspose::Cells::IWorkbook> workbook = Factory::CreateIWorkbook(); | |
// Access first worksheet. | |
intrusive_ptr<Aspose::Cells::IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Access cell A1. | |
intrusive_ptr<Aspose::Cells::ICell> cell = worksheet->GetICells()->GetObjectByIndex(new String("A1")); | |
// Add some text in cell. | |
cell->PutValue((StringPtr)new String("PDF Creation Time is 25-May-2017.")); | |
// Create pdf save options object. | |
intrusive_ptr<Aspose::Cells::IPdfSaveOptions> pdfSaveOptions = Factory::CreateIPdfSaveOptions(); | |
// Set the created time for the PDF i.e. 25-May-2017 | |
pdfSaveOptions->SetCreatedTime(new Aspose::Cells::Systems::DateTime(2017, 5, 25)); | |
// Save the Excel Document in PDF format | |
workbook->Save(outputConvertExcelWorkbookToPDF, pdfSaveOptions); |