gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:29:06 28 4
gpt4 key购买 nike

我有一个 .ods 文件,想要使用 jOpenDocument (Java) 将其导出为 PDF。

我使用以下代码:

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

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

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 作为参数。

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

提前感谢您的帮助!

最佳答案

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

关于java - jOpenDocument:尝试从 ODS 生成 PDF 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30569473/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com