java - Printing multiple JTables as one job -- Book object only prints 1st table -


for number of reasons, i'm trying combine output of multiple jtables single print job. after tearing hair out trying build pdfs, , combing java api, settled on book class. printing code looks this.

try {            printerjob printer = printerjob.getprinterjob();      //set 1/2 " margins , orientation     pageformat pf = printer.defaultpage();     pf.setorientation(pageformat.landscape);     paper paper = new paper();     double margin = 36; // half inch     paper.setimageablearea(margin, margin, paper.getwidth() - margin * 2, paper.getheight() - margin * 2);     pf.setpaper(paper);      book printjob = new book();      // note next line: getalltables() returns arraylist of jtables     (jtable t : getalltables() )           printjob.append(t.getprintable(printmode.fit_width, null, null), pf,2);      printer.setpageable(printjob);      system.out.println(printjob.getnumberofpages());      if (printer.printdialog())         printer.print();     } catch (printerexception e) {         e.printstacktrace(); } 

primary problem: output "first" table printing. i've made sure loop iterates correctly, , debugging statements have shown every table being added book. changing order of tables has no effect. changing print mode printmode.normal, appear result in pieces of other tables being printed. however, run slew horizontal pagination problems, table width exceeds page width (and still doesn't explain why printmode.fit_width isn't working)

a secondary question: how can detect correct number of pages in each printable? of tables 2 pages long, moment, i'm adding 2 pages each time append. read "somewhere" using book.unknown_number_of_pages page number fix problem, leads indexoutofbounds exception in api's code. i've considered calling print myself until no_page_exists, i'd need graphics object proper page dimensions (and have no idea how that).

lastly: if book approach hopeless, how else can combine output of multiple jtables (ie. multiple printables) single job? looked exporting table pdf, jtable's built-in pagination nice, i'd rather not have myself. last resort give , use itext's built in table function construct copy of table.

edit 3: per comment below, got working generating printable, determining # of pages, , generating new one. modified durendal's wrapper spare hassle of iterating on each page. code wrapper is

    class printablewrapper implements printable     {         private printable delegate;         private int offset;          public printablewrapper(printable delegate, int offset) {             this.offset = offset;             this.delegate = delegate;         }          @override         public int print(graphics graphics, pageformat pageformat, int pageindex) throws printerexception {             return delegate.print(graphics, pageformat, pageindex-offset);         }     }    

i put durendal's code determining number of pages in own function

    public int getnumberofpages(printable delegate, pageformat pageformat) throws printerexception      {         graphics g = new bufferedimage(1, 1, bufferedimage.type_int_rgb).creategraphics();         int numpages = 0;         while (true) {             int result = delegate.print(g, pageformat, numpages);             if (result == printable.page_exists) {                 ++numpages;             } else {                 break;             }         }          return numpages;     } 

after create book object, printing code looks this

            int totalpages = 0;             (dragndroptable t : getalltables() )             {                 int pages = getnumberofpages(t.getprintable(printmode.fit_width, null, null), pf);                 printable p = t.getprintable(printmode.fit_width, null, null);                 printjob.append(new printablewrapper(p,totalpages), pf, pages);                 totalpages += pages;             }             printer.setpageable(printjob);              if (printer.printdialog())                 printer.print(); 

and works charm!

edit 2: (you can skip this) tried durendal's answer. while i'm printing enough pages, multipage printables printing last page multiple times (once every page in printable). same problem discussed in 1st edit (below), , have no idea why happening, , debugging statements saying it's printing of pages correctly, last page of multipage printable printed in place of each page. code attached. insight appreciated (and repayed virtual cookies)

try {            printerjob printer = printerjob.getprinterjob();      //set 1/2 " margins , orientation     pageformat pf = printer.defaultpage();     pf.setorientation(pageformat.landscape);     paper paper = new paper();     double margin = 36; // half inch     paper.setimageablearea(margin, margin, paper.getwidth() - margin * 2, paper.getheight() - margin * 2);     pf.setpaper(paper);      book printjob = new book();      // note next line: getalltables() returns arraylist of jtables     (jtable t : getalltables() )       {         printable p = t.getprintable(printmode.fit_width, null, null);         int pages = getnumberofpages(p, pf);          (int i=0; < pages; i++)             printjob.append(new pagewrapper(p,i), pf);     }      printer.setpageable(printjob);      system.out.println(printjob.getnumberofpages());      if (printer.printdialog())         printer.print();     } catch (printerexception e) {         e.printstacktrace(); }  public int getnumberofpages(pageformat pageformat) throws printerexception  {     graphics g = new bufferedimage(1, 1, bufferedimage.type_int_rgb).creategraphics();     int numpages = 0;     while (true) {         int result = delegate.print(g, pageformat, numpages);         if (result == printable.page_exists)              ++numpages;         else             break;     }      return numpages; } 

i'm using unmodified pagewrapper durendal gave below.

edit 1: (you can skip this) dovetailing off of durendal's answer, tried make wrapper spares chore of iterating on pages ourselves. unfortunately, doesn't seem work correctly on multipage printables, printing same page multiple times in document. post it, because may work, , it's more convenient use.

class printablewrapper implements printable     {         private printable delegate;         private int offset;          public printablewrapper(printable delegate, int offset) {             this.offset = offset;             this.delegate = delegate;         }          @override         public int print(graphics graphics, pageformat pageformat, int pageindex) throws printerexception {             return delegate.print(graphics, pageformat, pageindex-offset);         }           public int getnumberofpages(pageformat pageformat) throws printerexception          {             graphics g = new bufferedimage(1, 1, bufferedimage.type_int_rgb).creategraphics();             int numpages = 0;             while (true) {                 int result = delegate.print(g, pageformat, numpages);                 if (result == printable.page_exists)                      ++numpages;                 else                     break;             }              return numpages;         }     } 

my printing code looks (after set page format)

book printjob = new book(); int totalpages = 0;  (dragndroptable t : getalltables() ) {     printable p = t.getprintable(printmode.fit_width, null, null);     printablewrapper pw = new printablewrapper(p, totalpages);     totalpages += pw.getnumberofpages(pf);     printjob.append(pw, pf,pw.getnumberofpages(pf)); }  printer.setpageable(printjob);  if (printer.printdialog()) {     printer.print();  } 

i had same problem recently. book class useless, because if add printables it, when book printed pass pageindex book printable @ pageindex.

that in cases not want.

create simple printable wrapper can remember pageindex used printable delegate , add book:

class pagewrapper implements printable {     private printable delegate;     private int localpageindex;      public pagewrapper(printable delegate, int pageindex) {         this.localpageindex = pageindex;         this.delegate = delegate;     }      @override     public int print(graphics graphics, pageformat pageformat, int pageindex) throws printerexception {         return delegate.print(graphics, pageformat, localpageindex);     } } 

now need iterate trough each page of each printable/pageable , add wrapper instance (that knows pageindex delegate) book. solves problem book passes wrong pageindex printables added (its easier pageables printables).

you can detect number of pages in printable printing ;) yes, i'm serious:

graphics g = new bufferedimage(1, 1, bufferedimage.type_int_rgb).creategraphics(); int numpages = 0; while (true) {     int result = printable.print(g, pageformat, numpages);     if (result == printable.page_exists) {         ++numpages;     } else {         break;     } } 

its important obtain pageformat instance actual printerjob want print to, because number of pages depends on page format (paper size).


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -