didoux95
Messages postés845Date d'inscriptionmardi 25 avril 2006StatutMembreDernière intervention 1 août 2017
-
4 sept. 2007 à 22:55
didoux95
Messages postés845Date d'inscriptionmardi 25 avril 2006StatutMembreDerniè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).
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.
*/
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
// 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
// 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 (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
// 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();