Hi,
I have a printable class that needs to print some text only on 1 page.
The thing is that when I hit print the printer prints 3 pages:
page 1 - with correct content
page 2 - empty page
page 3 - page with content translated some how
then the printing stops.

Here is the code:

private static final double FACTOR = 2.83;
private double width, height;

private void setupJob(){
		
		// page width and height
		width = 75*FACTOR;
		height = 50*FACTOR;
		
		job = PrinterJob.getPrinterJob();		
			
		pf = new PageFormat();
		//PageFormat pf =job.pageDialog(job.defaultPage());
		
		aset = new HashPrintRequestAttributeSet();
		
		// Create a letter sized piece of paper with one inch margins
		paper = new Paper();
		// resize the paper and the imageable area
		paper.setSize(width, height);
		paper.setImageableArea(0, 0, width, height);
		// set paper for the pageFormat
		pf.setPaper(paper);
		
		// set the job printable by calling the painter of this object with the pf pageFormat
		job.setPrintable(this, pf);
		job.setCopies(1);		
		
		// set the media printable area in mm
		aset.add(new MediaPrintableArea( 0f, 0f, 75f, 50f, MediaPrintableArea.MM));		
	
		try {
			job.print(aset);
		} catch (PrinterException e) {
			e.printStackTrace();
		}		
	}

@Override
	public int print(Graphics g, PageFormat pf, int page)
			throws PrinterException {	
		if (page > 0) { // We have only one page 
			return NO_SUCH_PAGE;
		}else{		
			
			Graphics2D g2d = (Graphics2D)g;
			// no translation needed
			// g2d.translate(11.34f, 0);
			// scale to fit page size
			double sx = 0, sy = 0;
			sx = width/(75*4);
			sy = height/(50*4);
			double scale = Math.min(sx, sy);
			g2d.scale(scale,scale);
		
			// draw the elements
			for (int i=0; i<4; i++){
				texts[i].drawText(g);
			}
		}
		return PAGE_EXISTS;
	}

Any help would be appreciated!
Thanks!

Recommended Answers

PageFormat.LANDSCAPE ?
check Class PrinterJob , method pageDialog(PageFormat page) : displays page set up dialog.
your width=212.25
height=141.5

Jump to Post

In case LANDSCAPE orientation , how many pages are printing? Three or one?
With 212.25 exceeds 210 A4 - size.
Method pageDialog shows you your printer set up.

Your saying that the width must be < the height?

No. Proportions gives info about orientation to set up.
Combine.

Jump to Post

All 8 Replies

PageFormat.LANDSCAPE ?
check Class PrinterJob , method pageDialog(PageFormat page) : displays page set up dialog.
your width=212.25
height=141.5

Thanks,
but I have to setup the page properties without the pageDialog.

With LANDSCAPE orientation the printed text is rotated ...

Your saying that the width must be < the height?

In case LANDSCAPE orientation , how many pages are printing? Three or one?
With 212.25 exceeds 210 A4 - size.
Method pageDialog shows you your printer set up.

Your saying that the width must be < the height?

No. Proportions gives info about orientation to set up.
Combine.

I don't know why it's not working for you. This class definitely allows me to print multi-page files. Are you sure the page is fully loaded before you try to start printing? Could that be your problem

Printing in Java is a pretty complex thing but if you know the right settings it is very user friendly

How can we know whether the print job print the page successfully or not(on printer).......?

vow vow vow vow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.