java - jOpenDocument: problems trying to generate PDF from ODS in -
i have .ods file , want export pdf using jopendocument (java).
i use following code:
// load ods file final opendocument filledcertificate = new opendocument(); filledcertificate.loadfrom("filledcertificate.ods"); // open 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 template , 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 want prevent copy/paste, can use // g2 = tp.creategraphicsshapes(w, h, true, 0.9f); tp.setwidth(w); tp.setheight(h); system.out.println("test 3"); // configure 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 renderer fit width renderer.setresizefactor(renderer.getprintwidth() / w); // render renderer.paintcomponent(g2); g2.dispose(); // add our spreadsheet in middle of page float offsetx = (pagesize.getwidth() - w) / 2; float offsety = (pagesize.getheight() - h) / 2; cb.addtemplate(tp, offsetx, offsety); system.out.println("test 5"); // close pdf document document.close();
when running, console shows following:
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 find style name:null type:table-cell @ org.jopendocument.model.office.officeautomaticstyles.getstyle(unknown source) @ org.jopendocument.model.office.officeautomaticstyles.getcellstyle(unknown source) @ org.jopendocument.model.table.tabletable.getcellstyle(unknown source) @ org.jopendocument.model.table.tabletablecell.computestyle(unknown source) @ org.jopendocument.model.table.tabletablecell.setrowandcolumn(unknown source) @ org.jopendocument.model.table.tabletablerow.computeallcells(unknown source) @ org.jopendocument.model.table.tabletablerow.getcellsinrange(unknown source) @ org.jopendocument.renderer.odtrenderer.getpagewidthinpixel(unknown source) @ org.jopendocument.renderer.odtrenderer.updatesize(unknown source) @ org.jopendocument.renderer.odtrenderer.setresizefactor(unknown source) @ org.jopendocument.renderer.odtrenderer.setdocument(unknown source) @ org.jopendocument.renderer.odtrenderer.<init>(unknown source) @ jopendocument.createdocument.main(createdocument.java:82)
i have no idea style can modify here? instantiation requires opendocument parameter.
exception handling included (not part of snippet).
thanks in advance!
i solve myself: used older version of jopendocument (1.3).
Comments
Post a Comment