首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jOpenDocument:尝试从ODS生成PDF时出现问题

jOpenDocument:尝试从ODS生成PDF时出现问题
EN

Stack Overflow用户
提问于 2015-06-01 17:00:49
回答 1查看 817关注 0票数 0

我有一个.ods文件,并希望使用jOpenDocument (Java)将其导出为.ods。

我使用以下代码:

代码语言:javascript
复制
                         // load the ODS file
        final OpenDocument filledCertificate = new OpenDocument();
        filledCertificate.loadFrom("filledCertificate.ods");

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

        PdfDocument pdf = new PdfDocument();

        document.addDocListener(pdf);
        System.out.println("test");
        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);
        System.out.println("test 2");
        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);
        System.out.println("test 3");

        // Configure the renderer
        ODTRenderer renderer = new ODTRenderer(filledCertificate);

        System.out.println("test 3.1");
        renderer.setIgnoreMargins(true);
        System.out.println("test 3.2");
        renderer.setPaintMaxResolution(true);
        System.out.println("test 4");                      
        // 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);
        System.out.println("test 5");
        // Close the PDF document
        document.close();

运行时,控制台显示如下:

代码语言:javascript
复制
StylePageLayoutProperties: Assuming Page Format A4: Width:  21.0cm
StylePageLayoutProperties: Assuming Page Format A4: Height: 29.7cm
StylePageLayoutProperties: Assuming Page Format A4: Width:  21.0cm
StylePageLayoutProperties: Assuming Page Format A4: Height: 29.7cm
test
test 2
test 3
PageHeigth:297000
[StyleStyle: name:co1 family:table-column cellProps:null                              masterTableName:null, StyleStyle: name:ro1 family:table-row cellProps:null masterTableName:null, StyleStyle: name:ta1 family:table cellProps:null masterTableName:Default, StyleStyle: name:Default family:null cellProps:null masterTableName:null]
Key:co1
Key:ro1
Key:ta1
Key:Default
java.lang.IllegalArgumentException: Unable to find Style name:null type:table-cell
at org.jopendocument.model.office.OfficeAutomaticStyles.getStyle(Unknown Source)
at org.jopendocument.model.office.OfficeAutomaticStyles.getCellStyle(Unknown Source)
at org.jopendocument.model.table.TableTable.getCellStyle(Unknown Source)
at org.jopendocument.model.table.TableTableCell.computeStyle(Unknown Source)
at org.jopendocument.model.table.TableTableCell.setRowAndColumn(Unknown Source)
at org.jopendocument.model.table.TableTableRow.computeAllCells(Unknown Source)
at org.jopendocument.model.table.TableTableRow.getCellsInRange(Unknown Source)
at org.jopendocument.renderer.ODTRenderer.getPageWidthInPixel(Unknown Source)
at org.jopendocument.renderer.ODTRenderer.updateSize(Unknown Source)
at org.jopendocument.renderer.ODTRenderer.setResizeFactor(Unknown Source)
at org.jopendocument.renderer.ODTRenderer.setDocument(Unknown Source)
at org.jopendocument.renderer.ODTRenderer.<init>(Unknown Source)
at jOpenDocument.createDocument.main(createDocument.java:82)

我真的不知道我可以在这里修改哪种风格?实例化只需要OpenDocument作为参数。

异常处理已经包含在内(不是代码段的一部分)。

提前感谢您的帮助!

EN

回答 1

Stack Overflow用户

发布于 2015-06-03 15:47:26

我可以自己解决这个问题:我只是使用了一个旧版本的jOpenDocument (1.3)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30569473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档