DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

David has posted 95 posts at DZone. View Full User Profile

Covert Microsoft Excel Files to PDF or PDF/A in Android Applications

05.28.2014
| 4598 views |
  • submit to reddit
//Instantiate a new workbook with Excel file path
String sdCardPath = Environment.getExternalStorageDirectory().getPath()+ File.separator;
Workbook workbook = new Workbook(sdCardPath + "Book1.xls");
//Save the document in PDF format
workbook.save(sdCardPath + "MyPdfFile.pdf", FileFormatType.PDF);

//PDF/A Conversion

Aspose.Cells for Java also provides support for PDF/A compliance.
//Open an existing Excel file
String sdCardPath = Environment.getExternalStorageDirectory().getPath()+ File.separator;
Workbook workbook = new Workbook(sdCardPath + "Book1.xls");
//Define PdfSaveOptions
PdfSaveOptions saveOptions = new PdfSaveOptions();
//Set the compliance type
saveOptiions.setCompliance(PdfCompliance.PDF_A_1_B);
//Save the PDF file
workbook.save(sdCardPath + "out.pdf", saveOptions);

//Set Creation Time for Output PDF

//When using the PdfSaveOptions class, you can get or set the PDF creation time.

File sdDir = Environment.getExternalStorageDirectory();
String sdPath = sdDir.getCanonicalPath();
//Load an exiting workbook or create from scratch
Workbook book = new Workbook(sdPath + "/book2.xlsx");
//Create an instance of PdfSaveOptions and pass SaveFormat to the construnctor
PdfSaveOptions options = new PdfSaveOptions(SaveFormat.PDF);
//Set the CreatedTime for the PdfSaveOptions as per requirement
options.setCreatedTime(DateTime.getNow());
//Save the workbook to PDF format while passing the object of PdfSaveOptions
book.save(sdPath + "/output.pdf", SaveFormat.PDF);
This technical tip show how developers can Covert Microsoft Excel Files to PDF or PDF/A inside their Android applications using Aspose.Cell for Android .  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 for Android can convert spreadsheets to PDF directly (including PDF/A), without any other product. Aspose.Cell converts spreadsheets to PDF with a high degree of accuracy and fidelity. Save an Excel file directly to PDF using the Workbook class' Save method, and provide the FileFormatType.PDF interface member. Direct conversion like this is the most efficient conversion method. It does not lose data or formatting but keeps the output PDF looking like the input Excel file. To specify security options when saving to PDF, use PdfSaveOptions.