Converting to PDF Files

Skip to end of metadata
Go to start of metadata
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

Direct Conversion

With the release of the version v2.3.0, Aspose.Cells for Java supports direct conversion of Excel files to Pdf format. You can directly save an excel file to pdf format using the s ave method of the Workbook class providing the FileFormatType.PDF interface member that converts the native excel files to pdf format. The Direct Conversion ( approach ) is most efficient approach that you should use now with latest versions. It will not loose your data / formatting in most cases in the generated PDF file . This approach does not require Aspose.Pdf product (by any means) as Aspose.Cells for Java can alone convert Excel spreadsheets to PDF.

You may also use PdfSaveOptions to specify the Security options. Brows and check "PdfSecurityOptions" API and set your desired options accordingly before converting to PDF.

Example:
[Java]
//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);
 
Second Approach

For older users (the users using older versions or the product), to convert Excel files to PDF documents, they might require Aspose.Pdf for Java. Aspose.Cells for Java can work with Aspose.Pdf for Java to convert a spreadsheet to a Pdf document using an intermediate step. 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 s ave method of the Workbook class to save your spreadsheet work. But, the file format should be in XML and select Aspose_Pdf (a pre-defined value) from the FileFormatType interface. Selecting ASPOSE_PDF value from the FileFormatType interface will direct the s ave method to generate spreadsheet in the XML form compatible to Aspose.Pdf Schema so that Aspose.Pdf for Java 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 package.
  • Call the b indXML method of the Pdf object and pass the name of the newly generated XML file (containing the spreadsheet content).
  • Call s ave method of the Pdf class to generate the PDF document.

The above steps are implemented below in an example.

Example:
[Java]
import java.io.*;
import com.aspose.cells.*;
//Note: Import the following package/classes from 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 are always on the process to enhance the conversion with every new release of the product. There are some limitations of the Excel to PDF conversion. Some format settings specified in spreadsheet might be lost when converting to PDF format. Also, a few drawing objects might be supported only.

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. 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 partially supported for the conversion.

Document Element Attribute Net Supported Notes
Alignment   Yes  
Rotation   Partially Only supports 90 and -90.
Background settings   Partially Only solid background pattern is supported, other patterns are not supported.
Border Color Yes  
Border Line style Partially DOUBLE, DASH_DOT_DOTTED, DASH_DOTTED line styles are not supported.
Border Line width Partially Thin and Thick lines are supported. 
Cell Data   Yes  
Comments   No  
Conditional Formatting   No  
Document Properties   No  
Drawing Objects   Partially TextBox, Rectangle, Oval and Arc shapes are supported.
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  
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 No  
Page Setup Scaling Yes  
Row Height/Column Width   Yes  
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.