PrinterJob

Résolu
didoux95 Messages postés 845 Date d'inscription mardi 25 avril 2006 Statut Membre Dernière intervention 1 août 2017 - 4 sept. 2007 à 22:55
didoux95 Messages postés 845 Date d'inscription mardi 25 avril 2006 Statut Membre Dernière intervention 1 août 2017 - 5 sept. 2007 à 14:57
Bonjour à tous.
Comment est ce que l'on peut faire pour changer de page avec la class PrinterJob. J'ai calculer le nombre de pages nécessaire, j'arrive à écrir sur la premiere mais pas sur les suivantes (j'ai fait une methode qui me calcule la pos du texte et tout et tout; donc pour ca, pas de problème).

merci.

2 réponses

Utilisateur anonyme
5 sept. 2007 à 04:35
Salut didoux

Peut-etre que tu trouveras ton bonheur dans cette source

/*  Anciennement ImpFichierTickets
******************************************************************
*                                                                *
*   VERSION AVEC OU SANS CONFIGURATION DE PAGE (marges etc...)   *
*           ( Ligne 181 : avec ou ligne 182 : sans )             **              x0 0, y0 0 par programme                      *
*                                                                *
******************************************************************
*/
/*
 * This example is from the book "Java Foundation Classes in a Nutshell".
 * Written by David Flanagan. Copyright (c) 1999 by O'Reilly & Associates.
 * You may distribute this source code for non-commercial purposes only.
 * You may study, modify, and use this example for any purpose, as long as
 * this notice is retained.  Note that this example is provided "as is",
 * WITHOUT WARRANTY of any kind either expressed or implied.
 */


   import java.awt.*;
   import java.awt.print.*;
   import java.io.*;
   import java.util.Vector;


          
   public class ImpressionFichierTexte implements Pageable, Printable
          
   {
      public static String FONTFAMILY = "Monospaced";
      public static int FONTSIZE;    //  prend la valeur par l'appelant dans fontsize
      public static int FONTSTYLE = Font.PLAIN;
      public static float LINESPACEFACTOR = 1.2f;
  
      PageFormat formatPage;   // Dimensions page, marge, orientation
      Vector lignes;           // Mise en "stream" du fichier texte
      Font font;
      int interlignes;         // Espace imter lignes
      int lignesParPage;       // Nombre de lignes par page
      int nombrePages;         // Nombre de pages
      int baseligne = -1;
  
      static File ImpFichier;
  
   /** Create a PageableText object for a string of text */
             
      public ImpressionFichierTexte(String text, PageFormat formatPage) throws IOException
             
      {
         this(new StringReader(text), formatPage);
         System.out.println("L 45 ImpressionFichierTexte _ String text");
      }
  
   /** Create a PageableText object for a file of text */
             
      public ImpressionFichierTexte(File ImpFichier, PageFormat formatPage) throws IOException
             
     
      //  public ImpressionFichierTexte(File file, PageFormat formatPage) throws IOException
      {
        // this(new FileReader(file), formatPage);
         this(new FileReader(ImpFichier), formatPage);  // ImpFichier = par exemple...
                                                     // ...ImpTicketNomCondoaamm
         System.out.println("L 56 ImpressionFichierTexte _ File");
      }
  
   /** Create a PageableText object for a stream of text */
             
      public ImpressionFichierTexte(Reader stream, PageFormat formatPage) throws IOException
             
      {
         this.formatPage = formatPage;
         System.out.println("L 62 ImpressionFichierTexte _ stream");
     
      // First, read all the text, breaking it into lignes.
      // This code ignores tabs, and does not wrap long lignes.
         BufferedReader in = new BufferedReader(stream);  // stream
         lignes = new Vector();
         String ligne;  int ctr = 0;
         while((ligne = in.readLine()) != null)
         {
            ctr++;
            System.out.println("ligne " + ctr + " = " + ligne);
            lignes.addElement(ligne);
         }
         System.out.println("ctr = " + ctr);
     
      // Create the font we will use, and compute spacing between lignes
         font = new Font(FONTFAMILY, FONTSTYLE, FONTSIZE);
         interlignes = (int) (FONTSIZE * LINESPACEFACTOR);  // 10 * 1.2f
     
      // Figure out how many lignes per page, and how many pages
         System.out.println("L 83 ImpressionFichierTexte _ interlignes = " + interlignes);  //  12
        // lignesParPage = 396/interlignes;  // 36 (informe : 8.5' / 5.5')
         lignesParPage = (int)Math.ceil(formatPage.getImageableHeight()/interlignes);  //floor
         System.out.println("L 86 ImpressionFichierTexte _ "
                           + (double)Math.ceil(formatPage.getImageableHeight()));  //floor
         System.out.println("L 88 ImpressionFichierTexte _ lignesParPage = " + lignesParPage);
         nombrePages = (lignes.size())/lignesParPage; //+ 1;lignes.size()-1)/lign
         System.out.println("L 90 ImpressionFichierTexte _ " + nombrePages + " pages");
         System.out.println("L 91 ImpressionFichierTexte _ lignes.size = " + lignes.size());
      }
  
   // These are the methods of the Pageable interface.
   // Note that the getPrintable() method returns this object, which means
   // that this class must also implement the Printable interface.
             
      public int getNumberOfPages()
             
      {
         return nombrePages;
      }
             
      public PageFormat getPageFormat(int numpage)
             
      {
         return formatPage;
      }
             
      public Printable getPrintable(int numpage)  // num. page = 0 a n
             
      {
         return this;
      }
  
             
      public int print(Graphics g, PageFormat formatPage, int numpage)
             
      {
         if ((numpage < 0) | (numpage >= nombrePages))
            return NO_SUCH_PAGE;
     
         if (baseligne == -1)  // si premiere fois
         {
            FontMetrics fm = g.getFontMetrics(font);
            baseligne = fm.getAscent();
            System.out.println("L119 ImpressionFichierTexte _ baseligne = " + baseligne);
         }
     
      // Clear the background to white.  This shouldn't be necessary, but is
      // required on some systems to workaround an implementation bug
         g.setColor(Color.white);
        // g.fillRect(0, 0, 612, 396);    // informe 21.6/14.0 cm
         g.fillRect((int)formatPage.getImageableX(), (int)formatPage.getImageableY(),
                      (int)formatPage.getImageableWidth(),
                      (int)formatPage.getImageableHeight());
         System.out.println("L129 ImpressionFichierTexte _ x/y/W/H = "
                           + formatPage.getImageableX() + "/" + formatPage.getImageableY() + "/"
                           + formatPage.getImageableWidth() + "/" + formatPage.getImageableHeight());
     
      // Set the font and the color we will be drawing with.
      // Note that you cannot assume that black is the default color!
         g.setFont(font);
         g.setColor(Color.black);
     
      // Figure out which lines of text we will print on this page
         int ligneDebut = numpage * lignesParPage;
         System.out.println("L140 ImpressionFichierTexte _ ligneDebut = " + ligneDebut);
         int ligneFin = ligneDebut + lignesParPage - 1;
         System.out.println("L142 ImpressionFichierTexte _ ligneFin = " + ligneFin);
         if (ligneFin >= lignes.size())
         {
            ligneFin = lignes.size()-1;
            System.out.println("L146 ImpressionFichierTexte _ ligneFin = " + ligneFin);
         }
     
      // Calcul 1ere ligne de chaque page
         int x0 = (int) formatPage.getImageableX();
         int y0 = (int) formatPage.getImageableY() + baseligne;
       //  int x0 = 0;
       //  int y0 = 0;
     
      // Image de la page.
         for(int i=ligneDebut; i <= ligneFin; i++)
         {
         // Lecture ligne
            String ligne = (String)lignes.elementAt(i);
        
         // On n'utilise pas la version Java 2D (bug)
            if (ligne.length() > 0)
               g.drawString(ligne, x0, y0);
        
         // Avance ligne
            y0 += interlignes;
         }
         return PAGE_EXISTS;
      }
  
   //    public static void main(String[] args) throws IOException, PrinterException
             
      public static int init(String Ifichier, int fontsize, double wpap, double hpap,
                        double x, double y, double wi, double hi)
      throws IOException, PrinterException
             
      {
         FONTSIZE = fontsize;
         ImpFichier = new File(Ifichier);
      // Moteur d'impression
         PrinterJob tache = PrinterJob.getPrinterJob();
     
      // Dimension du papier
         PageFormat formatPage = tache.pageDialog(tache.defaultPage());  // Avec config. page
        // PageFormat formatPage = new PageFormat();  // Sans config. page
         Paper papier = new Paper();
         papier.setSize(wpap, hpap);    //  612, 396
        // papier.setSize(612, 394);    //  612, 396
         papier.setImageableArea(x, y, wi, hi);
        // papier.setImageableArea(0, 60, 575, 300);  // 0, 60, 504, 280
         formatPage.setPaper(papier);
     
      // Creation de l'objet PageableText passage des infos a PrinterJob
         tache.setPageable(new ImpressionFichierTexte(new File(Ifichier), formatPage));
        // tache.setPageable(new ImpressionFichierTexte(new String(Ifichier), formatPage));
     
      // Avec fenetre d'impression : choix imprimante,
      //                             nombre de copies, etc...
      //                             Si OK on imprime
      //                             sinon : abandon de l'impression
         if (tache.printDialog())
            tache.print();
     
         return 0;
      }
   }
3
didoux95 Messages postés 845 Date d'inscription mardi 25 avril 2006 Statut Membre Dernière intervention 1 août 2017 2
5 sept. 2007 à 14:57
Salut.
ca m'a donné une idée.
merci.
0
Rejoignez-nous