java - Printing JPanel into PDF with itext without displaying it -
i have number of sheets contains numbers of jpanel. jpanels parents jtextpane, once panel visible can print pdf itext, if page not itext doesnt print panel. @ time of printing ducument created , contents added jtextpane , size of panel known too. hier print code
public static void printcomponent(final jcomponent component, final rectangle bounds) throws documentexception, saxexception, ioexception, parserconfigurationexception { if (pdfcanvas == null) { return; } final rectangle alloc = new rectangle(component.getbounds().width, component.getbounds().height); pdfcanvas.savestate(); pdfcanvas.setleading(0); pdfcanvas.setcharacterspacing(0); final pdftemplate canvas = pdfcanvas.createtemplate(alloc.width, alloc.height); final fontmapper arialuni = new fontmapper() { @override public basefont awttopdf(final java.awt.font font) { //return basefont.... } @override public java.awt.font pdftoawt(final basefont font, final int size) { return null; } }; final graphics2d g2d = canvas.creategraphics(alloc.width, alloc.height, arialuni, true, 1.0f); g2d.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); g2d.setrenderinghint(renderinghints.key_text_antialiasing, renderinghints.value_text_antialias_on); g2d.setrenderinghint(renderinghints.key_rendering, renderinghints.value_render_quality); g2d.scale(printscalefactor, printscalefactor); component.printall(g2d); g2d.dispose(); component.revalidate(); final point2d root = gettransformedprinterpoint(new point(bounds.x, bounds.y), true); pdfcanvas.addtemplate(canvas, (float) root.getx(), (float) root.gety() - alloc.height); pdfcanvas.restorestate(); }
this code works expected when panel once visible.
thanks
Comments
Post a Comment