Introduction
PDF documents are widely being used as a standard format of exchanging documents between organizations, government sectors and common users etc. So, it may also be required by the developers to convert their Excel files into PDF documents. Realizing this possible requirement, Aspose.Cells has added the support of converting Excel files to the PDF documents to facilitate its users. In this topic, we will see that how can this conversion be done.
Converting Excel to PDF
To convert Excel files to PDF documents, developers also need Aspose.Pdf (another excellent component by Aspose). Aspose.Cells can work with Aspose.Pdf to convert a spreadsheet to a Pdf document. Both Aspose.Cells and Aspose.Pdf development teams are working hard to enhance the Excel to PDF conversion and support as many features of Microsoft Excel spreadsheets as possible. Please follow the steps below to perform this conversion:
- Instantiate an object of Workbook class by calling its empty constructor
- Do your desired work on the spreadsheet using the API of Aspose.Cells
- Once you finish working on spreadsheet, call Save method of the Workbook class to save your spreadsheet work. But, the file format should be in XML and select AsposePdf (a pre-defined value) from the FileFormatType enumeration. Selecting AsposePdf value from the FileFormatType enumeration will direct the Save method to generate spreadsheet in the XML form compatible to Aspose.Pdf Schema so that Aspose.Pdf can generate PDF document according to the spreadsheet
- After the XML file (containing the spreadsheet content) is created, you can create an object of Pdf class in Aspose.Pdf namespace
- Call the BindXML method of the Pdf object and pass the name of the newly generated XML file (containing the spreadsheet content). BindXML also takes another parameter to accept the name of the XSL file if XSLT is used. But, this is not required by us. So, we can simply pass a null here
- Call Save method of the Pdf class to generate the PDF document
The above steps are implemented below in an example.
Example:
[C#]
//Instantiate the Workbook object
Workbook workbook = new Workbook();
//Do something here to work on your spreadsheet
//Save the document in Aspose.Pdf.Xml format
workbook.Save("MySpreadsheet.xml", FileFormatType.AsposePdf);
//Read the file in Aspose.Pdf.Xml format into Aspose.Pdf
Aspose.Pdf.Pdf pdf1 = new Aspose.Pdf.Pdf();
//Bind the XML file (containing spreadsheet data) with the Pdf object
pdf1.BindXML("MySpreadsheet.xml", null);
//Create the PDF document by calling its Save method
pdf1.Save("MySpreadsheet.pdf");
[VB.NET]
'Instantiate the Workbook object
Dim workbook As Workbook = New Workbook()
'Do something here to work on your spreadsheet
'Save the document in Aspose.Pdf.Xml format
workbook.Save("MySpreadsheet.xml", FileFormatType.AsposePdf)
'Read the file in Aspose.Pdf.Xml format into Aspose.Pdf
Dim pdf1 As Aspose.Pdf.Pdf = New Aspose.Pdf.Pdf()
'Bind the XML file (containing spreadsheet data) with the Pdf object
pdf1.BindXML("MySpreadsheet.xml", Nothing)
'Create the PDF document by calling its Save method
pdf1.Save("MySpreadsheet.pdf")
[JAVA]
//Instantiate the Workbook object
Workbook workbook = new Workbook();
//Do something here to work on your spreadsheet
//Save the document in Aspose.Pdf.Xml format
workbook.save("MySpreadsheet.xml", FileFormatType.ASPOSE_PDF);
//Bind the XML file (containing spreadsheet data) with the Pdf object
Pdf pdf = Pdf.bindXML(new FileInputStream("F:\\FileTemp\\dest.xml"));
//Create the PDF document by calling its Save method
pdf.save(new FileOutputStream("MySpreadsheet.pdf"));
Conversion Limitations
There are some limitations of the Excel to PDF conversion. Some formatting settings specified in spreadsheets might be lost when converting to PDF format. Also a few drawing objects might be missing.
We have provided a table given below that lists all those features, which are supported fully or partially when exporting to PDF using Aspose.Cells and Aspose.Pdf. This table also identifies those features that are not supported in this conversion.
|
Document Element
|
Attribute
|
Net 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
|
Only Aspose.Pdf line style types are supported
|
|
Border
|
Line width
|
Yes
|
|
|
Cell Data
|
|
Yes
|
|
|
Comments
|
|
No
|
|
|
Conditional Formatting
|
|
No
|
|
|
Document Properties
|
|
No
|
|
|
Drawing Objects
|
|
No
|
|
|
Font
|
Size
|
Yes
|
|
|
Font
|
Color
|
Yes
|
|
|
Font
|
Style
|
Yes
|
|
|
Font
|
Underline
|
Partially
|
Only single underline is supported
|
|
Font
|
Effect
|
Partially
|
Only strike through effect is supported
|
|
Images
|
|
Yes
|
|
|
Hyperlink
|
|
Yes
|
|
|
Charts
|
|
Partially
|
Check Supported Charts List
|
|
Merged Cells
|
|
Partially
|
|