A pure Java library for OASIS Open Document files manipulation

Create a PDF document from a spreadsheet

jOpenDocument can be used to create PDF files.
PDF generation requires the use of the iText library. The iText library is available for free under a multiple license: MPL and LGPL.

We recommend iText 2.1.5, the jar file is available here.

 // Load the ODS file
 final OpenDocument doc = new OpenDocument();

 // Open the PDF document
 Document document = new Document(PageSize.A4);
 File outFile = new File("invoice.pdf");

 PdfDocument pdf = new PdfDocument();


 FileOutputStream fileOutputStream = 
new FileOutputStream(outFile);
 PdfWriter writer = PdfWriter.getInstance(pdf, fileOutputStream);

 // Create a template and a Graphics2D object 
 Rectangle pageSize = document.getPageSize();
 int w = (int) (pageSize.getWidth() 0.9);
 int h = (int) (pageSize.getHeight() 0.95);
 PdfContentByte cb = writer.getDirectContent();
 PdfTemplate tp = cb.createTemplate(w, h);

 Graphics2D g2 = tp.createPrinterGraphics(w, h, null);
 // If you want to prevent copy/paste, you can use
 // g2 = tp.createGraphicsShapes(w, h, true, 0.9f);

 // Configure the renderer
 ODTRenderer renderer = new ODTRenderer(doc);
 // Scale the renderer to fit width
 renderer.setResizeFactor(renderer.getPrintWidth() / w);
 // Render

 // Add our spreadsheet in the middle of the page
 float offsetX = (pageSize.getWidth() - w2;
 float offsetY = (pageSize.getHeight() - h2;
 cb.addTemplate(tp, offsetX, offsetY);
 // Close the PDF document

The final PDF file is available here.

This nice feature can be used to build a server delivering documents on-demand.