Hello,
I'm very new to jOpenDocument - essentially looking to use Open Office
instead of iText to make PDF files.

I tried the tutorial at:
http://www.jopendocument.org/tutorial_pdf.html
--- made sure all the *.jar files are there.

The main function is running the only function in the class below
(near copy-paste of the tutorial). But I get the following error.
Any help is appreciated!

java.util.zip.ZipException: error in opening zip file
        at java.util.zip.ZipFile.open(Native Method)
        at java.util.zip.ZipFile.<init>(ZipFile.java:127)
        at java.util.zip.ZipFile.<init>(ZipFile.java:144)
        at org.jopendocument.model.OpenDocument.loadFrom(Unknown
Source)
        at org.jopendocument.model.OpenDocument.loadFrom(Unknown
Source)
        at try_odf.PDF.toPDF(PDF.java:37)
        at try_odf.Main.main(Main.java:27)
Exception in thread "main" java.lang.IllegalArgumentException:
OfficeBody cannot be null
        at org.jopendocument.model.OpenDocument.init(Unknown Source)
        at org.jopendocument.model.OpenDocument.loadFrom(Unknown
Source)
        at org.jopendocument.model.OpenDocument.loadFrom(Unknown
Source)
        at try_odf.PDF.toPDF(PDF.java:37)
        at try_odf.Main.main(Main.java:27)


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package try_odf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfDocument;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Graphics2D;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import org.jopendocument.model.OpenDocument;
import org.jopendocument.renderer.ODTRenderer;

/**
 *
 * @author rtropper
 */
public class PDF {

    public static void toPDF( String docUrl, String pdfUrl ) throws
FileNotFoundException, DocumentException {

        //re-coding for debugging
        docUrl = "test.ods";
        pdfUrl = "test.pdf";


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

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

        PdfDocument pdf = new PdfDocument();

        document.addDocListener(pdf);

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

        document.open();

        // 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);

        tp.setWidth(w);
        tp.setHeight(h);

        // Configure the renderer
        ODTRenderer renderer = new ODTRenderer(doc);
        renderer.setIgnoreMargins(true);
        renderer.setPaintMaxResolution(true);

        // Scale the renderer to fit width
        renderer.setResizeFactor(renderer.getPrintWidth() / w);
        // Render
        renderer.paintComponent(g2);
        g2.dispose();

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

Reply via email to