
In the world of data and document processing, Excel sheets play a crucial role in organizing and presenting information. Often, you might need to convert Excel spreadsheets into other formats for various purposes, such as sharing reports, archiving data, or ensuring cross-platform compatibility. One common requirement is converting Excel sheets to PDF format. In this blog post, we’ll explore how to convert Excel files to PDF in C++.
- C++ Excel to PDF Converter API
- Convert Excel Files to PDF in C++
- Convert Excel to PDF with Compliance Level
- Set PDF Creation Date in Excel to PDF Conversion
- Get Free License
C++ Excel to PDF Converter API
Aspose.Cells for C++ is designed to implement Excel automation features within C++ applications. Using the API, you can create new Excel files from scratch as well as manipulate the existing ones. The API’s built-in spreadsheet converter lets you convert Excel files to other formats with high fidelity. You can either download the API package or install it from NuGet.
Install-Package Aspose.Cells.Cpp
Convert an Excel XLS to PDF in C++ {#Convert-Excel-Files-to-PDF-in-C++}
The following are the steps to convert Excel files to PDF using Aspose.Cells.
- Load the Excel file using intrusive_ptrAspose::Cells::IWorkbook pointer.
- Save the Excel file as a PDF using the Save(StringPtr, SaveFormat_Pdf) method of IWorkbook class.
The following code sample shows how to convert Excel files to PDF using 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); |
Convert Excel to PDF with Compliance Level
Aspose.Cells for C++ also allows you to set the compliance level of the converted PDF file such as PDF/A. For this, the API provides an additional class named as IPdfSaveOptions that allows you to customize the Excel to PDF conversion with different options. The following are the steps to convert an Excel file to PDF with PDF/A compliance.
- Load the Excel file using intrusive_ptrAspose::Cells::IWorkbook pointer.
- Use intrusive_ptrAspose::Cells::IPdfSaveOptions pointer to create PDF save options.
- Set compliance level using IPdfSaveOptions->SetCompliance(Aspose::Cells::Rendering::PdfCompliance) method.
- Save the Excel file as PDF using Save(StringPtr, IPdfSaveOptions) method of IWorkbook class.
The following code sample shows how to set compliance level in Excel to PDF conversion.
// 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); |
Set Creation Date in Excel to PDF Conversion
You can also set the creation date and time for the converted PDF file. For this, you can simply use IPdfSaveOptions->SetCreatedTime(new Aspose::Cells::Systems::DateTime) method. The following are the steps to perform this operation.
- Load the Excel file using intrusive_ptrAspose::Cells::IWorkbook pointer.
- Use intrusive_ptrAspose::Cells::IPdfSaveOptions pointer to create PDF save options.
- Set date and time using IPdfSaveOptions->SetCreatedTime(new Aspose::Cells::Systems::DateTime(2017, 5, 25)) method.
- Save the Excel file as PDF using Save(StringPtr, IPdfSaveOptions) method of IWorkbook class.
The following code sample shows how to set the creation date and time in Excel to PDF conversion.
// 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); |
C++ Excel Library - Get a Free License
You can get a free temporary license in order to try the API without evaluation limitations.
Conclusion
Converting Excel sheets to PDF in C++ is made easy and efficient with Aspose.Cells for C++. This library provides a seamless way to work with Excel files programmatically and offers extensive features for customization. By following the steps and code examples in this blog post, you can easily convert your Excel files to PDF format, making data sharing and archiving a breeze in your C++ applications. You can explore more about the API using documentation.