Convert Excel to PDF with C++

Converting Excel Workbook to PDF

PDF files are widely used to exchange documents between organizations, government sectors, and individuals. It is a standard document format, and software developers are often asked to find a way to convert Microsoft Excel files into PDF documents.

Aspose.Cells supports converting Excel files to PDF and maintains high visual fidelity in the conversion.

Direct Conversion

Aspose.Cells for C++ supports conversion from spreadsheets to PDF independently of other software. Simply save an Excel file to PDF using the Workbook class' Save method. The Save method provides the SaveFormat.Pdf enumeration member that converts the native Excel files to PDF format.

Follow the below steps to directly convert the Excel spreadsheets to PDF format:

  1. Instantiate an object of the Workbook class by calling its empty constructor.
  2. You may open/load an existing template file or skip this step if you are creating the workbook from scratch.
  3. Do any work (input data, apply formatting, set formulas, insert pictures, or other drawing objects, and so on) on the spreadsheet using Aspose.Cells' APIs.
  4. When the spreadsheet code is complete, call the Workbook class' Save method to save the spreadsheet.

The file format should be PDF, so select Pdf (a pre-defined value) from the SaveFormat enumeration to generate the final PDF document.

#include <iostream>
#include "Aspose.Cells.h"

using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Output directory path
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    // Path of input Excel file
    U16String inputFilePath = srcDir + u"Book1.xls";

    // Path of output PDF file
    U16String outputFilePath = outDir + u"output.pdf";

    // Create workbook
    Workbook workbook(inputFilePath);

    // Save the document in PDF format
    workbook.Save(outputFilePath, SaveFormat::Pdf);

    std::cout << "Document saved successfully in PDF format!" << std::endl;

    Aspose::Cells::Cleanup();
}

Advanced Conversion

You may also opt to use the PdfSaveOptions class to set different attributes for the conversion. Setting different properties of the PdfSaveOptions class gives you control over the print, font, security, and compression settings for the output PDF.

The most important property is GetCompliance(), which enables you to set the PDF standards compliance level. Currently, you can save to PDF 1.4, PDF 1.5, PDF 1.6, PDF 1.7, PDF/A-1a, PDF/A-1b, PDF/A-2a, PDF/A-2b, PDF/A-2u, PDF/A-3a, PDF/A-2ab, and PDF/A-3u formats. Note that with the PDF/A format, an output file size is larger than a regular PDF file size.

Saving Workbook to PDF/A Complied Files

The below-provided code snippet demonstrates how to use the PdfSaveOptions class to save Excel files to PDF/A compliant PDF format.

#include <iostream>
#include "Aspose.Cells.h"

using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Output directory path
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    // Instantiate new workbook
    Workbook workbook;

    // Insert a value into the A1 cell in the first worksheet
    workbook.GetWorksheets().Get(0).GetCells().Get(0, 0).PutValue(U16String(u"Testing PDF/A"));

    // Define PdfSaveOptions
    PdfSaveOptions pdfSaveOptions;

    // Set the compliance type
    pdfSaveOptions.SetCompliance(PdfCompliance::PdfA1b);

    // Save the file
    workbook.Save(outDir + u"output.pdf", pdfSaveOptions);

    std::cout << "PDF file created successfully with PDF/A-1b compliance!" << std::endl;

    Aspose::Cells::Cleanup();
}

Set the PDF Creation Time

With the PdfSaveOptions class, you can get or set the PDF creation time. The following code demonstrates the use of the PdfSaveOptions.GetCreatedTime() property to set the creation time of the PDF file.

#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Path of input excel file
    U16String inputPath = srcDir + u"Book1.xlsx";

    // Load excel file containing charts
    Workbook workbook(inputPath);

    // Create an instance of PdfSaveOptions
    PdfSaveOptions options;
	options.SetCreatedTime(Date{ 2025,01,01 });

    // Save the workbook to PDF format while passing the object of PdfSaveOptions
    workbook.Save(srcDir + u"output.pdf", options);

    std::cout << "Workbook saved to PDF successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

Set ContentCopyForAccessibility option

With the PdfSaveOptions class, you can get or set the PDF GetAccessibilityExtractContent() option to control the content access in the converted PDF.

#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Output directory path
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    // Path of input excel file
    U16String inputPath = srcDir + u"BookWithSomeData.xlsx";

    // Load excel file containing some data
    Workbook workbook(inputPath);

    // Create an instance of PdfSaveOptions
    PdfSaveOptions pdfSaveOpt;

    // Create an instance of PdfSecurityOptions
    PdfSecurityOptions securityOptions;

    // Set AccessibilityExtractContent to false
    securityOptions.SetAccessibilityExtractContent(false);

    // Set the security option in the PdfSaveOptions
    pdfSaveOpt.SetSecurityOptions(securityOptions);

    // Save the workbook to PDF format while passing the object of PdfSaveOptions
    workbook.Save(outDir + u"outFile.pdf", pdfSaveOpt);

    std::cout << "Workbook saved to PDF format successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

Export Custom properties to PDF

With the PdfSaveOptions class, you can export the custom properties in the source workbook to the PDF. PdfCustomPropertiesExport enumerator is provided for specifying the way by which properties are exported. These properties can be observed in Adobe Acrobat Reader by clicking on File and then properties option as shown in the following image. Template file “sourceWithCustProps.xlsx” can be downloaded here for testing, and the output PDF file “outSourceWithCustProps” is available here for analysis.

todo:image_alt_text

#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Rendering;

int main()
{
    Aspose::Cells::Startup();

    // Load excel file containing custom properties
    U16String inputFilePath(u"sourceWithCustProps.xlsx");
    Workbook workbook(inputFilePath);

    // Create an instance of PdfSaveOptions
    PdfSaveOptions pdfSaveOptions;

    // Set CustomPropertiesExport property to PdfCustomPropertiesExport::Standard
    pdfSaveOptions.SetCustomPropertiesExport(PdfCustomPropertiesExport::Standard);

    // Save the workbook to PDF format while passing the object of PdfSaveOptions
    U16String outputFilePath(u"outSourceWithCustProps.pdf");
    workbook.Save(outputFilePath, pdfSaveOptions);

    Aspose::Cells::Cleanup();
}

Conversion Attributes

We work to enhance the conversion features with each new release. Aspose.Cells' Excel to PDF conversion still has a couple of limitations. MapChart is not supported when converting to PDF format. Also, some drawing objects are not supported well.

The table that follows lists all features that are fully or partially supported when exporting to PDF using Aspose.Cells. This table is not final and does not cover all the spreadsheet attributes, but it does identify those features that are not supported or partially supported for conversion to PDF.

Document Element Attribute Supported Notes
Alignment Yes
Background settings Yes
Border Color Yes
Border Line style Yes
Border Line width Yes
Cell Data Yes
Comments Yes
Conditional Formatting Yes
Document Properties Yes
Drawing Objects Partially Shadow and 3-D effects for drawing objects are not supported well; WordArt and SmartArt are partially supported.
Font Size Yes
Font Color Yes
Font Style Yes
Font Underline Yes
Font Effects Yes
Images Yes
Hyperlink Yes
Charts Partially MapChart is not supported.
Merged Cells Yes
Page Break Yes
Page Setup Header/Footer Yes
Page Setup Margins Yes
Page Setup Page Orientation Yes
Page Setup Page Size Yes
Page Setup Print Area Yes
Page Setup Print Titles Yes
Page Setup Scaling Yes
Row Height/Column Width Yes
RTL (Right to Left) Language Yes

Advance topics