Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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 ==========
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic