• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Multipage print question need help

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My print source code is enclosed. It can print multipage but jump page . It only print page 2,4,6,8....., can't print 1,3,5,7..... Who can give me a guidance? Thank in advance!
Ken

====== Source Code: begin ==========
package logger;
import javax.print.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.print.*;

public class Log2Printer implements Printable {
public Log2Printer() {}
public void print() {
try {
this.getPrintContent();
DocFlavor df = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintService ds = PrintServiceLookup.lookupDefaultPrintService();
Doc d = new SimpleDoc(this, df, null);
DocPrintJob job = ds.createPrintJob();
job.print(d, null);
}
catch (Exception pe) {pe.printStackTrace();}
}
private Vector m_vecPrintContent = new Vector();
private void getPrintContent() {
try {
FileReader fr = new FileReader("C:/a.txt");
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
m_vecPrintContent.add(line);
line = br.readLine();
}
}
catch (Exception e) {e.printStackTrace();}
}
public int print(Graphics g, PageFormat pf, int pageIndex) {
Graphics2D g2D = (Graphics2D) g;
g2D.translate(pf.getImageableX(), pf.getImageableY());
Font font = new Font("Serif", Font.PLAIN, 14);
g2D.setFont(font);
int pageWidth = g2D.getClipBounds().width;
int pageHeight = g2D.getClipBounds().height;
FontMetrics fontMetrics = g2D.getFontMetrics(font);
int position = fontMetrics.getHeight();
int height = fontMetrics.getAscent();
while (!m_vecPrintContent.isEmpty()) {
String line = (String)m_vecPrintContent.firstElement();
int lineWidth = fontMetrics.stringWidth(line);
while (lineWidth > pageWidth) {
String lineCopy = line;
String firstPart = "";
while (lineWidth > pageWidth) {
int index = lineCopy.lastIndexOf(' ');
index = (index != -1)? index : (int)((double)lineCopy.length()*((double)pageWidth/(double)lineWidth));
firstPart = lineCopy.substring(0, index);
lineWidth = fontMetrics.stringWidth(firstPart);
lineCopy = firstPart;
}
g2D.drawString(firstPart, 0, position);
System.out.println(firstPart);
position += height;
line = line.substring(firstPart.length(), line.length()).trim();
lineWidth = fontMetrics.stringWidth(line);
if (position >= pageHeight) {
m_vecPrintContent.set(0,line);
return Printable.PAGE_EXISTS;
}
}
g2D.drawString(line, 0, position);
System.out.println(line);
m_vecPrintContent.remove(0);
position += height;
if (position >= pageHeight) return Printable.PAGE_EXISTS;
}
return Printable.NO_SUCH_PAGE;
}

public static void main(String[] args) {
Log2Printer lp = new Log2Printer();
lp.print();
}
}
====== Source Code: end ==========
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic