| PDF documents are widely used as a standard format of exchanging documents between organizations, government sectors and individuals. Software developers are often asked to device a way to easily convert Microsoft Excel files into PDF documents. Aspose.Cells supports this features. This article shows how. |
Converting Excel to PDF
Microsoft Excel to PDF conversion was introduced with Aspose.Cells for Java 2.3.0. From that release, Aspose.Cells can convert spreadsheets to PDF directly, without the need for any other product. To convert spreadsheets with older versions of Aspose.Cells, use Aspose.Pdf for the conversion.
Aspose.Cell's converts spreadsheets to PDF with a high degree of accuracy and fidelity. However, there are a few limitations, listed at the end of this article.
Direct Conversion: Aspose.Cells 2.3.0 and upwards
From version 2.3.0, Aspose.Cells for Java supports direct conversion of Microsoft Excel files to PDF format. You can save an Excel file directly to PDF format using the Workbook class' Save method, and providing the FileFormatType.PDF interface member. Direct conversion like this is the most efficient conversion method. It will not lose data or formatting but keep the output PDF looking like the input Excel file.
To specify security options when saving to PDF, use PdfSaveOptions.
Programming Example
//Instantiate a new workbook with Excel file path Workbook workbook = new Workbook("F:\\FileTemp\\Book1.xls"); //Save the document in PDF format workbook.save("F:\\FileTemp\\MyPdfFile.pdf", FileFormatType.PDF);
PDF/A Conversion
Aspose.Cells for Java also provides support for PDF/A compliance.
Programming Example
//Open an existing Excel file Workbook workbook = new Workbook("e:\\test2\\Book1.xlsx"); //Define PdfSaveOptions PdfSaveOptions saveOptions = new PdfSaveOptions(); //Set the compliance type saveOptiions.setCompliance(PdfCompliance.PDF_A_1_B); //Save the PDF file workbook.save("e:\\test2\\out.pdf", saveOptions);
Conversion with Aspose.Pdf: Aspose.Cells Prior to 2.3.0
Users with Aspose.Cells versions prior to version 2.3.0, need to use a component like Aspose.Pdf for Java to convert spreadsheets to PDF files. Aspose.Cells and Aspose.Pdf work together to convert a spreadsheet to PDF via an intermediate step.
To convert spreadsheets to PDF with Aspose.Cells and Aspose.Pdf:
- Instantiate an object of the Workbook class by calling its empty constructor.
- Do your desired work on the spreadsheet using the Aspose.Cells API.
- Call the Workbook class' Save method to save the spreadsheet:
- Set the file format to XML.
- Select Aspose_Pdf (a pre-defined value) from the FileFormatType interface. This directs the Save method to generate a spreadsheet in the XML form compatible with the Aspose.Pdf Schema so that Aspose.Pdf for Java can then generate a PDF document.
- When the XML file has been created, create an object of the Pdf class in the aspose.pdf package.
- Call the Pdf class' bindXML method and pass the name of the output XML file.
- Call the Pdf class' Save method to generate the PDF document.
The above steps are implemented below in an example.
Programming Example
import java.io.*; import com.aspose.cells.*; //Note: Import the following package/classes from the Aspose.Pdf for Java library. import aspose.pdf.*; //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("F:\\FileTemp\\MySpreadsheet.xml", FileFormatType.ASPOSE_PDF); //Bind the XML file (containing spreadsheet data) with the Pdf object Pdf pdf = new Pdf(); pdf.bindXML("F:\\FileTemp\\MySpreadsheet.xml", null); //Create the PDF document by calling its save method pdf.save("F:\\FileTemp\\MySpreadsheet.pdf");
Conversion Attributes
We work hard to improve conversion and other aspects of Aspose.Cells with every release. The Excel to PDF conversion has a few limitations. Some format settings specified in a spreadsheet might be lost, and not all drawing objects are supported.
The table below 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. It can also identify those features that may not be supported or are partially supported for the conversion.
| Document Element | Attribute | Net Supported | Notes |
|---|---|---|---|
| Alignment | Yes | ||
| Rotation | Partially | Only supports 90 and -90. | |
| Background settings | Yes | ||
| Border | Color | Yes | |
| Border | Line style | Yes | |
| Border | Line width | Yes | |
| Cell Data | Yes | ||
| Comments | No | ||
| Conditional Formatting | Yes | ||
| Document Properties | Yes | ||
| Drawing Objects | Yes | ||
| 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 | Yes | ||
| 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 |

