Converting to PDF Files

Skip to end of metadata
Go to start of metadata
PDF files are widely used for exchanging 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.

Converting Excel to PDF

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

Advantages of Direct Conversion

Converting directly from spreadsheet to PDF, instead of using a third-party tool or external API, has several advantages.

  • Direct conversion does not require temporary files because the whole process can be done in memory.
  • No XML file is needed so large files can easily be converted.
  • The conversion speed is much faster.

Conversion Steps

Please follow the steps below to perform this conversion:

  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 your desired 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.

The above steps are implemented in the code snippet that follows.
In the example, we use a template Excel file (with rich formatting) to test the conversion. The screenshot shows the input file so that you can compare it with the output.


Figure: The template Excel file.

Example Code

[C#]
//Instantiate the Workbook object
//Open an Excel file
Workbook workbook = new Workbook("f:\\test\\Book1.xls");


//Save the document in PDF format
workbook.Save("f:\\test\\outBook1.pdf", SaveFormat.Pdf);
 


[VB.NET]
'Instantiate the Workbook object
'Open an Excel file
Dim workbook As Workbook = New Workbook("f:\test\Book1.xls")


'Save the document in PDF format
workbook.Save("f:\test\outBook1.pdf", SaveFormat.Pdf)

After executing the code, a PDF file is generated.


Figure: The output PDF file.

PDF/A Conversion

With the release of version v5.1.0, Aspose.Cells provides support for PDF/A compliance.

Example Code

[C#]
//Instantiate new workbook
Workbook workbook = new Workbook();

//Insert a value into the A1 cell in the first worksheet
workbook.Worksheets[0].Cells[0, 0].PutValue("Testing PDF/A");

//Defind PdfSaveOptions
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();

//Set the compliance type
pdfSaveOptions.Compliance = PdfCompliance.PdfA1b;

//Save the file
workbook.Save("e:\\test\\pdf_a_compliance.pdf" , pdfSaveOptions);
 


[VB.NET]
'Instantiate new workbook
Dim workbook As New Workbook()

'Insert a value into the A1 cell in the first worksheet
workbook.Worksheets(0).Cells(0, 0).PutValue("Testing PDF/A")

'Defind PdfSaveOptions
Dim pdfSaveOptions As New PdfSaveOptions()

'Set the compliance type
pdfSaveOptions.Compliance = PdfCompliance.PdfA1b

'Save the file
workbook.Save("e:\test\pdf_a_compliance.pdf", pdfSaveOptions)
 

Conversion Attributes

We work to enhance the conversion features with each new release. Aspose.Cell's Excel to PDF conversion still has a couple of limitations. Some spreadsheet formatting might be lost when converting to PDF format. Also, some drawing objects are not yet supported.

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   Partially Only solid background pattern is supported, other patterns are not supported
Border Color Yes  
Border Line style Partially Double-line style is not supported
Border Line width Yes  
Cell Data   Yes  
Comments   No  
Conditional Formatting   Yes  
Document Properties   No  
Drawing Objects   Partially Supported Objects: TextBox, Line, Rectangle, Oval, GroupBox, Button, CheckBox, RadioButton, ListBox, ComboBox, Label
Font Size Yes  
Font Color Yes  
Font Style Yes  
Font Underline Partially Only single underline is supported
Font Effects Partially Only strike through effect is supported
Images   Yes  
Hyperlink   Yes  
Charts   Partially Check Supported Charts List
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  

Additional Resources

You may find the Convert an XLS File to PDF Format article useful for further reference.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.