0

Instead of generating a Doc you could print your graphics of a JFrame/JPanel directly.

This code should work:

          PrinterJob pj = PrinterJob.getPrinterJob();
          pj.setJobName("name");
            PageFormat format = pj.getPageFormat(null);
            pj.setPrintable (new Printable() {
            @Override
            public int print(Graphics pg, PageFormat pf, int pageNum) throws PrinterException {
                if (pageNum > 0){
                      return Printable.NO_SUCH_PAGE;
                      }

                      Graphics2D g2 = (Graphics2D) pg;

                      this.paint(g2);
                      return Printable.PAGE_EXISTS;
            }

          }, format);
          if (pj.printDialog() == false)
          return;
          pj.print();
          } catch (PrinterException ex) {
                // handle exception
          }
        }
jkdev
  • 11,360
  • 15
  • 54
  • 77
  • Don't use `paint`, use `print`. Also, make sure the window is actually ready to be printed – MadProgrammer Mar 01 '19 at 06:30
  • how am i supposed to make sure that the window is ready to be printed – Aks Chunara Mar 01 '19 at 06:44
  • Can you send me the link to the question similar to mine – Aks Chunara Mar 01 '19 at 06:45
  • Printing a "frame" is the same process as printing a "component". In fact, if you took the time read through all three, you will find one is an exact duplicate – MadProgrammer Mar 01 '19 at 06:46
  • @AksChunara Now that this question has been marked as a duplicate, the links to similar questions are near the top of the page, just after "This question already has an answer here:" – jkdev Mar 01 '19 at 08:19

0 Answers0