hi all
i'm trying to print the output of my application
no problems with the 1st page but i'd like to tell to my app to print in a brand new page if the content exceed the printable length of the first page. i have 2 classes: printer and Document. the last one implements Printable
Printer code
PrinterJob printJob = PrinterJob.getPrinterJob ();
//--- Create a new book to add pages to
Book book = new Book ();
//--- Add the cover page using the default page format for this print job
//book.append (new IntroPage (), printJob.defaultPage ());
//--- Add the document page using a landscape page format
PageFormat documentPageFormat = new PageFormat ();
documentPageFormat.setOrientation (PageFormat.LANDSCAPE);
//Document doc=new Document();
//book.append (doc, documentPageFormat);
//book.append (new Getter(), documentPageFormat);
book.append (new Document(), documentPageFormat);
//--- Tell the printJob to use the book as the pageable object
printJob.setPageable (book);
System.out.println("AAAAAAAA" +book.getNumberOfPages());
//--- Show the print dialog box. If the user click the
//--- print button we then proceed to print else we cancel
//--- the process.
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
}
Document code:
public class Document implements Printable
{
private final static int POINTS_PER_INCH = 72;
Calendar now;
DateFormat df;
Date date;
double width;
Point2D.Double pen;
Graphics2D g2d;
LineBreakMeasurer lineBreaker;
AttributedString paragraphText;
TextLayout layout;
double maxPen=0.0;
static int x1,x2,x3,x4,x5,x6,x7,x8,x9,x10;
static double intestY, textX, textY;
public Document()
{
}
public int print (Graphics g, PageFormat pageFormat, int page)
{
pen = new Point2D.Double();
//setto calendario e DateFormat per orario
df=new SimpleDateFormat("HH:mm:ss:SSS");
//now=GregorianCalendar.getInstance();
//date=now.getTime();
//--- Create the Graphics2D object
g2d = (Graphics2D) g;
//--- Translate the origin to 0,0 for the top left corner
g2d.translate(pageFormat.getImageableX (), pageFormat.getImageableY ());
g2d.setColor(new Color(204,204,204));
Font titleFont = new Font ("arial", Font.PLAIN, 10);
Font textFont = new Font ("arial", Font.PLAIN, 8);
g2d.setFont (titleFont);
//--- Compute the horizontal center of the page
FontMetrics fontMetrics = g2d.getFontMetrics ();
String text = "ABSOL s.r.l. - LISTA APPUNTAMENTI DI Marcello Baiguera";
textX=(pageFormat.getImageableWidth() - fontMetrics.stringWidth(text))/2;
textY =7.2;
//g2d.drawString (text, (int) textX, (int) textY);
g2d.setFont (textFont);
fontMetrics = g2d.getFontMetrics ();
//linea orizzontale sotto il titolo
textY=textY + fontMetrics.getDescent();
//g2d.drawLine(0, (int)textY, (int)pageFormat.getImageableWidth(), (int)textY);
//prima linea verticale - margine sx
//g2d.drawLine(x1, (int)textY, x1, (int)pageFormat.getImageableHeight());
//seconda linea vert
x2=x1+fontMetrics.stringWidth("ven. 31/12/2003 ");
//g2d.drawLine(x2, (int)textY, x2, (int)pageFormat.getImageableHeight());
//terza linea vert
x3=x2+fontMetrics.stringWidth("88:88 ");
//g2d.drawLine(x3, (int)textY, x3, (int)pageFormat.getImageableHeight());
//quarta linea vert
x4=x3+fontMetrics.stringWidth("Marcello Impegnato text text text text");
//g2d.drawLine(x4, (int)textY, x4, (int)pageFormat.getImageableHeight());
//quinta linea vert
x5=x4+fontMetrics.stringWidth("Sig D'alessio Gigione ");
//g2d.drawLine(x5, (int)textY, x5, (int)pageFormat.getImageableHeight());
//sesta linea vert
x6=x5+fontMetrics.stringWidth("Via del PonteVico, 222222 ");
//g2d.drawLine(x6, (int)textY, x6, (int)pageFormat.getImageableHeight());
//settima linea vert
x7=x6+fontMetrics.stringWidth("del Bagnolo Del Mella (bs) ");
//g2d.drawLine(x7, (int)textY, x7, (int)pageFormat.getImageableHeight());
//ottava linea vert
x8=x7+fontMetrics.stringWidth("333/78547888 ");
//g2d.drawLine(x8, (int)textY, x8, (int)pageFormat.getImageableHeight());
//nona linea vert
x9=x8+fontMetrics.stringWidth("Rinnovo Scadenza Il ");
//g2d.drawLine(x9, (int)textY, x9, (int)pageFormat.getImageableHeight());
//decima linea vert
x10=x9+fontMetrics.stringWidth("Sandropolitan")+2;
//g2d.drawLine(x10, (int)textY, x10, (int)pageFormat.getImageableHeight());
intestY=textY+12;
text = "DATA";
textX= x1+((x2-x1)-fontMetrics.stringWidth(text))/2;
//g2d.drawString (text, (int) textX, (int) intestY);
text = "ORA";
textX= x2+((x3-x2)-fontMetrics.stringWidth(text))/2;
//g2d.drawString (text, (int) textX, (int) intestY);
text = "CLIENTE";
textX= x3+((x4-x3)-fontMetrics.stringWidth(text))/2;
//g2d.drawString (text, (int) textX, (int) intestY);
text = "REFERENTE";
textX= x4+((x5-x4)-fontMetrics.stringWidth(text))/2;
//g2d.drawString (text, (int) textX, (int) intestY);
text = "INDIRIZZO";
textX= x5+((x6-x5)-fontMetrics.stringWidth(text))/2;
//g2d.drawString (text, (int) textX, (int) intestY);
text = "CITTA'";
textX= x6+((x7-x6)-fontMetrics.stringWidth(text))/2;
//g2d.drawString (text, (int) textX, (int) intestY);
text = "TELEFONO";
textX= x7+((x8-x7)-fontMetrics.stringWidth(text))/2;
//g2d.drawString (text, (int) textX, (int) intestY);
text = "MOTIVAZIONE";
textX= x8+((x9-x8)-fontMetrics.stringWidth(text))/2;
//g2d.drawString (text, (int) textX, (int) intestY);
text = "APP. CON";
textX= x9+((x10-x9)-fontMetrics.stringWidth(text))/2;
//g2d.drawString (text, (int) textX, (int) intestY);
textY=intestY+6;
//g2d.drawLine(0, (int)textY, (int)pageFormat.getImageableWidth(), (int)textY);
textY=textY+12;
double initialTextY=textY;
System.out.println("maximum Y: "+pageFormat.getImageableY());
Vector maxPens=new Vector();
boolean vai=true;
for(int i=0; i<12; i++ )
{
if(textY<400.00 && vai)
{
System.out.println("");
System.out.println("-- "+i+" --"+df.format(new Date()));
//data
text = Integer.toString(i);
//textX= x1+((x2-x1)-fontMetrics.stringWidth(text))/2;
pen.x=x1+2;
printJustified(text, x2-x1-4,textY);
//ora
text = "10:00";
//textX= x2+((x3-x2)-fontMetrics.stringWidth(text))/2;
pen.x=x2+2;
//printJustified(text, x3-x2,textY);
//cliente
text = "Costruzioni Benedini";
pen.x=x3+2;
//printJustified(text, x4-x3-4,textY);
//referente
text = "Sig. Alessandro mmmm mmmm mmm mmmm mm ";
pen.x=x4+2;
//printJustified(text, x5-x4-4, textY);
//indirizzo
text = "Via del Ca'";
pen.x=x5+2;
//printJustified(text, x6-x5-4,textY);
//citta' + (prov)
text = "Pozzolengo S. (VR)";
pen.x=x6+2;
//printJustified(text, x7-x6-4,textY);
//telefono
text = "335/6998523";
pen.x=x7+2;
//printJustified(text, x8-x7-4,textY);
//motivazione
//text = "Primo Appun";
pen.x=x8+2;
//printJustified(text, x9-x8-4,textY);
//Appuntamento con
//text = "Alessandro";
pen.x=x9+2;
//printJustified(text, x10-x9-4,textY);
//textY=pen.y+12;
try
{
textY=Double.valueOf(maxPens.get(i).toString()).doubleValue()+12;
System.out.println(i+" recuperato");
}
catch(ArrayIndexOutOfBoundsException a)
{
maxPens.add(Double.toString(maxPen));
textY=maxPen+12;
System.out.println(i+" aggiunto");
}
}
else
{
System.out.println("aaaaaaaaaaaaaa ("+textY+")");
vai=false;
}
}
//--- Validate the page
return (PAGE_EXISTS);
//return(NO_SUCH_PAGE);
}
void printJustified(String text, double width, double y)
{
pen.y=y;
String txt=text;
System.out.println("text: "+text+" - pen.y= "+y);
paragraphText = new AttributedString (txt);
paragraphText.addAttribute (TextAttribute.FONT, new Font ("arial", Font.PLAIN, 8));
//--- Create a LineBreakMeasurer to wrap the text for the TextLayout object
//--- Note the second parameter, the FontRendereContext. I have set the second
//--- parameter antiAlised to true and the third parameter useFractionalMetrics
//--- to true to get the best possible output
lineBreaker = new LineBreakMeasurer (paragraphText.getIterator(), new FontRenderContext (null, true, true));
//--- Create a Vector to temporaly store each line of text
Vector lines = new Vector ();
//--- Get the output of the LineBreakMeasurer and store it in a Vector
while ((layout = lineBreaker.nextLayout ((float) width)) != null) {
lines.add (layout);
}
pen.y = pen.y - 7.373047;
//--- Scan each line of the paragraph and justify it except for the last line
for (int i = 0; i < lines.size (); i++)
{
//--- Get the line from the vector
layout = (TextLayout) lines.get (i);
//--- Check for the last line. When found print it
//--- with justification off
//--- Align the Y pen to the ascend of the font, remember that
//--- the ascend is origin (0, 0) of a font. Refer to figure 1
pen.y += layout.getAscent ();
//--- Draw the line of text
layout.draw (g2d, (float) pen.x, (float) pen.y);
//--- Move the pen to the next position adding the descent and
//--- the leading of the font
pen.y += layout.getDescent () + layout.getLeading ();
if (pen.y>maxPen)
{
maxPen=pen.y;
System.out.println("-----------------------------------------------txt: "+txt+" new maxPeny= "+maxPen);
}
}
}
}
in Document i use the printJustified method that simply splits the text in rows.
i'll don't explain the method 'cause my question is another.
thanx a lot
please!!!
sandro