0

I am using the following codes to print and save the data in pdf format but it doesn't worked. So, what is the code to print the information of jframe in netbeans in pdf format.

PrinterJob job = PrinterJob.getPrinterJob();

job.setJobName("jPanel2");

job.setPrintable (new Printable()
{
    public int print(Graphics pg, PageFormat pf, int pageNum)
    {
        if (pageNum > 0)
        {
            return Printable.NO_SUCH_PAGE;
        }
        Graphics2D g2 = (Graphics2D) pg;
        g2.translate(pf.getImageableX(), pf.getImageableY());
        jPanel2.paint(g2);
        return Printable.PAGE_EXISTS;
    }
});

boolean ok = job.printDialog();

if (ok)
{
    try
    {
        job.print();
    }
    catch (PrinterException ex)
    {
    }
}
Jérémie Bertrand
  • 3,025
  • 3
  • 44
  • 53
  • Don't use `jPanel2.paint(g2);`, use `jPanel2.print(g2);` instead – MadProgrammer Jan 22 '15 at 00:39
  • possible duplicate of [Printing a JFrame and its components](http://stackoverflow.com/questions/12764634/printing-a-jframe-and-its-components) – MadProgrammer Jan 22 '15 at 00:41
  • For [example](http://stackoverflow.com/questions/12764634/printing-a-jframe-and-its-components/12765916#12765916), [example](http://stackoverflow.com/questions/22058738/print-jlabels-icon-in-a-printer-using-a-button/22059079#22059079), [example](http://stackoverflow.com/questions/22241711/setting-print-size-of-a-jlabel-and-put-a-jradiobutton-on-the-print/22242658#22242658), – MadProgrammer Jan 22 '15 at 00:43

0 Answers0