printing problem

A

anders

Hello,

I try to print the content of a JTextArea to my local printer
using Java 1.4.2 on Winwdos XP SP1 Home Edition.

Earlier I used Printjob instead of Printerjob and it produced
some output however it keept removing the first few lines, mabe because
of the JScrollpane? So I went for Printerjob instead.

The System.out.println(off +" - "+len +" - "+str);
line inside my print function does show all the correct text
in the Systemoutput window but on the printer only 3 blank pages will
appear.

any Idea?

[...]

PrintArea pa = new PrintArea(ta); // ta is the JTextArea that
contains my text, approx. 2 A4-pages

PrinterJob pj = PrinterJob.getPrinterJob( );
System.out.println(pj);
PageFormat pf = pj.defaultPage();
pf.setOrientation(PageFormat.LANDSCAPE);
pa.setPageFormat(pf);
pj.setPrintable( pa );
try { pj.print( );}
catch( Exception ex){ System.out.println("Exception"+ex); }

[...]




class PrintArea extends JTextArea implements Printable, Pageable {
int cnt = 0;
// printing constants
private static final int page = 33;
private static final Font printFont = new Font("Courier",Font.PLAIN,
10);
private int i = 1;
//lines start at 0
private int ls = 0;
private int lc = 1;
// this one holds all the text
private JTextArea ta1;

public PrintArea(JTextArea tta) {
super();
setDoubleBuffered(false);
setLineWrap(false);
setFont(printFont);
setRows(page + 3);
setColumns(132);
this.ta1 = tta;
lc = ta1.getLineCount() - 1;
}


public int print(Graphics g, PageFormat pf, int i) throws
PrinterException {
i = i+1;
int gBW = (int)getBounds().width;
int gBH = (int)getBounds().height;

// set text on first call, print resulting graphics on second call
if(cnt == 0) {
if(ls > lc) return Printable.NO_SUCH_PAGE;
try {
int off = ta1.getLineStartOffset(ls);
int le = (((i*page) < lc) ? (i*page) : lc);
int len = ta1.getLineEndOffset(le) - off;
String str = ta1.getText(off,len);
System.out.println(off +" - "+len +" - "+str);
setText(str);
for(int j=0; j<(page - (le-ls)); j++) append("\n");
append("\n \t \t \t \t \t \t \tPage "+i);
ls = le + 1;
}
catch(Exception ex) { return Printable.NO_SUCH_PAGE; }
cnt++;
}
else {
// create a 2D image for easier manipulation
Graphics2D g2 = (Graphics2D)g;
// align image on paper, .5 inch margin works on my printers
g2.translate(pf.getImageableX()/2,pf.getImageableY()/2);
// clip to proper size
g2.setClip(0,0,gBW,gBH);
paint(g2);
cnt = 0;
} return Printable.PAGE_EXISTS;
}

// Pageable implementation
PageFormat pf = null;

public int getNumberOfPages() {
return (int)((lc / page) + (((lc % page) > 1) ? 1 : 0));
}

public PageFormat getPageFormat(int pageIndex) throws
IndexOutOfBoundsException {
return pf;
}

public void setPageFormat(PageFormat pgForm) { pf = pgForm; }

public Printable getPrintable(int pageIndex) throws
IndexOutOfBoundsException
{
return this;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top