我有一个 .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 作为参数。
已经包含异常处理(不是代码片段的一部分)。
提前感谢您的帮助!