johnny_016 0 Newbie Poster

Hi, I'm trying to print some text on a credit card size card with a card printer. So, I'm using the print api and it works, the only thing is I want to position text somewhere specific on the card but that I can't change the coordinates of the imageable area. If it is not the default value of imageableX = 72 units and imageableY = 72 units, nothing is printed on the card. Also, it needs to print on LANDSCAPE mode, but it totally messes it up and the imageableX value changes to a different value even though I set it to something else. So here is my code taht I'm testing out:

import java.awt.*;
import java.awt.print.*;

public class CardPrinter implements Printable {

	static public void main(String args[]) {
		PrinterJob printerJob = PrinterJob.getPrinterJob();
		Book book = new Book();
		book.append(new CardPrinter(), new PageFormat());
		
		printerJob.setPageable(book); 
		boolean doPrint = printerJob.printDialog();
		
		if (doPrint) { 
			try {
				printerJob.print();
			} catch (PrinterException exception) {
				System.err.println("Printing error: " + exception);
			}
		}
	}
	
	public int print(Graphics g, PageFormat format, int pageIndex) {
	    double width = 2.13 * 72;
	    double height = 3.38 * 72;
	    
	    Graphics2D g2d = (Graphics2D)g;

	    Paper paper = new Paper();
	    paper.setSize(width, height);
	    paper.setImageableArea(30, 30, 30, 30);

	    format.setPaper(paper);
	    format.setOrientation(PageFormat.PORTRAIT);

	    g2d.translate(format.getImageableX(), format.getImageableY());

	    g2d.drawString("Hello", 10, 10);

                    return Printable.PAGE_EXISTS;
	}
}

Thanks in advance!

Be a part of the DaniWeb community

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