Skip to main content

How to set custom paper size using printer jobs

19 replies [Last post]
lucho01
Offline
Joined: 2006-05-05
Points: 0

I am trying to set a custom paper size using printer jobs but it does not take my custom paper size and instead it takes the default paper size. The code I am using is as follows:

PageFormat pageFormat = job.defaultPage();
Paper paper = new Paper();
paper.setSize(500,500); // Large Address Dimension
paper.setImageableArea(20, 20, 450, 420);
pageFormat.setPaper(paper);
pageFormat.setOrientation(PageFormat.PORTRAIT);
try {
job.setPrintable(this,pageFormat);
if (job.printDialog()) {
job.print();
}
} catch (PrinterException e) {
e.printStackTrace();
}

What am I doing wrong?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
leonel_gracias
Offline
Joined: 2010-09-07
Points: 0

Thanks.

Your post solved my problem

alladyn
Offline
Joined: 2007-11-29
Points: 0

Hi

I had the same problem with default paper with my software for
printing labels. Finally I discovered that using the Book class
can help. When I used the standard way the paper
always changed to some default size and the imaginable area
was also wrong.

greetings from Poland

here is my class for printing images with Book usage (1.5 JRE):

public class ImagePrinter implements Printable {

private Image image;

public static void printImage(Image aImage) throws PrinterException {
new ImagePrinter(aImage).print();
}

public ImagePrinter(Image aImage) {
image = aImage;
}

public void print() throws PrinterException {
PrinterJob pPrinterJob = PrinterJob.getPrinterJob();
if (pPrinterJob.printDialog()) {
PageFormat pPageFormat = pPrinterJob.defaultPage();
Paper pPaper = pPageFormat.getPaper();
pPaper.setImageableArea(1.0, 1.0, pPaper.getWidth() - 2.0, pPaper.getHeight() - 2.0);
pPageFormat.setPaper(pPaper);
pPageFormat = pPrinterJob.pageDialog(pPageFormat);
Book pBook = new Book();
pBook.append(this, pPageFormat);
pPrinterJob.setPageable(pBook);
pPrinterJob.print();
}
}

public int print(Graphics g, PageFormat aPageFormat, int aPageIndex) {

if (aPageIndex > 0) {
return NO_SUCH_PAGE;
}

Graphics2D g2d = (Graphics2D) g;
AffineTransform pOrigTransform = g2d.getTransform();

g2d.translate(aPageFormat.getImageableX(), aPageFormat.getImageableY());
g2d.drawImage(image, 0, 0, null);

g2d.setTransform(pOrigTransform);
return PAGE_EXISTS;
}
}

snacker
Offline
Joined: 2004-06-25
Points: 0

THANKS FOR POSTING THIS!

You have saved me many hours of debugging!

Phil Race

If your printer doesn't support custom paper sizes, then
setting one will just cause the closest supported paper
size to be used.

However there is also a bug specific to the case when the
end user then changes the printer in the print dialog
to one that is not the system default one where we
validate against the original printer, not the selected one.

That's 6359283 PrinterJob.pageDialog() ret. incorrect PageFormat when non-default
printer used. Its only fixed in JDK 7 builds.
http://bugs.sun.com/view_bug.do?bug_id=6359283

If neither of the above describe your situation, then
try running the latest JRE : 1.6.0_03 (you didn't mention
this so far as I could see).

If none of that helps you would need to provide a complete small
self-contained test program (not just code snippets), and also
the exact JRE version (output from java -version) and
also the O/S and printer model and driver so we could
look further.

-phil.

java2d@JAVADESKTOP.ORG wrote:
> Hi Piet thanks for your help, I am trying to resolve my problem using your code but it did not worked. I am still getting the size A5 no matter what I try to define. I don't know if I am missing something. Please look at the following code and see if you can find what I cant find.
>
> PageFormat pageFormat = createFormat();
> try {
> job.setPrintable(this,pageFormat);
> if (job.printDialog()) {
> job.print();
> }
> } catch (PrinterException e) {
> e.printStackTrace();
> }
>
>
> private PageFormat createFormat() {
> PageFormat format = new PageFormat();
> format.setPaper(new Custom());
> return format;
> }
>
> private class Custom extends Paper {
>
> public Custom() {
> super();
> setSize(491, 594);
> setImageableArea(36.0, 36.0, 522.99212598425197, 450.8897637795276);
> }
> }
> [Message sent by forum member 'lucho01' (lucho01)]
>
> http://forums.java.net/jive/thread.jspa?messageID=241236
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

lucho01
Offline
Joined: 2006-05-05
Points: 0

Hi Phil,

I have tried with the followings jres: jre 1.5_12, jre1.6_3, jre 1.6_EA_5. The printer I am using is Epson LX-300+II. The operating system is windows XP sp2. I have also tried in UBUNTU. I have set the paper size in the server properties of the printer, but it takes the size that it considers is best for printing (A5) instead of the size that I am trying to set.With another program like wordpad the printer takes the custom paper size I have se in the server properties of the printer.

import java.awt.Font;
import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class PrintTest implements Printable {

public PrintTest(){
super();
}
public void print() {
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pageFormat = createFormat();
try {
job.setPrintable(this,pageFormat);
if (job.printDialog()) {
job.print();
}
} catch (PrinterException e) {
e.printStackTrace();
}
}

public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException {
if (pageIndex == 0) {
painter(graphics);
return PAGE_EXISTS;
}
return NO_SUCH_PAGE;
}

void painter(Graphics graphics) {
graphics.setFont(new Font("Sans Serif", Font.PLAIN, 11));
char[] data = "Printing an area. This is a test that is driving me crazy. It is time to come out...."
.toCharArray();
graphics.drawChars(data, 0, data.length, 25, 70);
}

public static void main(String[] args){
PrintTest printTest=new PrintTest();
printTest.print();
}

private PageFormat createFormat() {
PageFormat format = new PageFormat();
format.setPaper(new Custom());
return format;
}

private class Custom extends Paper {

public Custom() {
super();
setSize(560,491);
}
}
}

lucho01
Offline
Joined: 2006-05-05
Points: 0

Hi Phil,

The problem I have posted regarding the custom paper size is very important for one of our customers who has more than 60 clients with the same printer and the same paper format. I need to know if there is another way around to solve this problem.

If this is a bug of the jdk, when it is going to be solved?

Best regards,

Lucho.

Phil Race

java2d@JAVADESKTOP.ORG wrote:
> Hi Phil,
>
> The problem I have posted regarding the custom paper size is very important for one of our customers who has more than 60 clients with the same printer and the same paper format. I need to know if there is another way around to solve this problem.
>

Sorry I've been out and busy. I'll look into more this when I get time.
If you need something more definite, then that's where a support
contract comes in :)

But I did get so far as to install the driver for the Epson printer and
I see no indication that
it supports custom paper sizes. Every windows driver I've seen that does
has to provide
a way for the user to specify that size. You claimed this works in
wordpad, but
again, I don't see that functionality there. How exactly did you do this ?

> If this is a bug of the jdk, when it is going to be solved?
>

Depends. Probably JDK 7 ...

-phil.
> Best regards,
>
> Lucho.
> [Message sent by forum member 'lucho01' (lucho01)]
>
> http://forums.java.net/jive/thread.jspa?messageID=241956
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".
>

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

jg_at_sun
Offline
Joined: 2006-10-17
Points: 0

I also installed the driver from Epson website and did not see a way to specify a custom paper size. Where is the driver from? What version?

lucho01
Offline
Joined: 2006-05-05
Points: 0

The Epson documentation for windows XP states that you can set a custom paper size following these steps:

1. For Windows XP , click Start >Settings > Printer and faxes.
2. Click the EPSON LX-300+II ESC/P icon.
3. Select server Properties from the file menu.
4.Select create a new form.
5.Enter the paper size in the Measurement fields.
6.Click save form and then ok.

Best Regards,

Lucho.

janet_batlle
Offline
Joined: 2007-11-09
Points: 0

I Have de same problem, but when I’m in Netbeans and run the program the code work, but when I’m compile the system and make my *.exe did work.

Some vary cant tell me why.

This is the code

PrinterJob pj = PrinterJob.getPrinterJob();
PageFormat pf = pj.defaultPage();
Paper paper = pf.getPaper();
paper.setSize(612, 264);
paper.setImageableArea(0,0, 612, 264);
pf.setPaper(paper);

try {
if (resF.getRow() != 0) {
do {
pj.setPrintable(new Printable() {
int cont = 0;
public int print(Graphics g, PageFormat pf, int pi)
throws PrinterException {

if (pi >= 1) {
return Printable.NO_SUCH_PAGE;
}

procesarImpresion(g, resF, strSecuencia, strCodnomina, strCargInst, strCargDiv, strMensaje, booEstacerrada, booImprimirdetalles);

return Printable.PAGE_EXISTS;
}
}, pf);

try {
pj.print();
} catch (PrinterException ex) {
this.objUtl.message("information", "Error when processing "+ex)
}

} while(resF.next());
} else {
this.objUtl.message("information", "Error when processing "+e)
}
} catch (Exception e) {
this.objUtl.message("error", "Error when processing "+e)
}
}

janet_batlle
Offline
Joined: 2007-11-09
Points: 0

Sorry is

I Have de same problem, but when I’m in Netbeans and run the program the code work, but when I’m compile the system and make my *.exe don't work.

Some vary cant tell me why.

kanatorn
Offline
Joined: 2006-06-03
Points: 0

I think you forgot to add paper size to printer server.
Try to do follow this tutorial
http://it-develops.blogspot.com/2007/08/java-printer-vs-customize-paper-...

Message was edited by: kanatorn

pietblok
Offline
Joined: 2003-07-17
Points: 0

Hi,

I am not sure what you are doing wrong. However, a long time ago I experimented with printing and maybe some code snippets will help you.

private class A4 extends Paper {

public A4() {
super();
setSize(594.992125984252, 841.8897637795276);
setImageableArea(36.0, 36.0, 522.99212598425197, 769.8897637795276);
}
}

and

private PageFormat createFormat() {
PageFormat format = PageFormat();
format.setPaper(new A4());
return format;
}

for me this worked. Hope it helps.

(Sorry for the bad layout, I just don't know how to get that right)

Piet

pietblok
Offline
Joined: 2003-07-17
Points: 0

Sorry again,

the word "new" dropped from

PageFormat format = PageFormat();

this should read:

PageFormat format = new PageFormat();

Piet

lucho01
Offline
Joined: 2006-05-05
Points: 0

Hi Piet thanks for your help, I am trying to resolve my problem using your code but it did not worked. I am still getting the size A5 no matter what I try to define. I don't know if I am missing something. Please look at the following code and see if you can find what I cant find.

PageFormat pageFormat = createFormat();
try {
job.setPrintable(this,pageFormat);
if (job.printDialog()) {
job.print();
}
} catch (PrinterException e) {
e.printStackTrace();
}

private PageFormat createFormat() {
PageFormat format = new PageFormat();
format.setPaper(new Custom());
return format;
}

private class Custom extends Paper {

public Custom() {
super();
setSize(491, 594);
setImageableArea(36.0, 36.0, 522.99212598425197, 450.8897637795276);
}
}

lucho01
Offline
Joined: 2006-05-05
Points: 0

Sorry the code of the Custom class is below:

private class Custom extends Paper {

public Custom() {
super();
setSize(594,491);
setImageableArea(36.0, 36.0, 522.99212598425197, 450.8897637795276);
}
}

pietblok
Offline
Joined: 2003-07-17
Points: 0

Well, I am not an expert on printing issues, so probably it is as difficult for me as it is for you. The code snippets came from a class that I created some time ago and that seemed to work for me. I remember that it took me some time to get it working at all. The original version of this code dates from the time that there was no javax.print package, so before java 1.4. (I never succeeded to get javax.print classes to really work for me). Maybe you should have a look at the complete code and try to find if there are essential differences with your code.

Here is the code:

http://www.pbjar.org/docs/src/org/pbjar/games/swing/PrintAction.java

If you want to see it in action, go to:

http://www.pbjar.org/webstartdemo.html

and try the Sudoku solver. In the options menu it has the print action.

In the code, forget about the I18nInstaller. That is just to get dynamic internationalization, and I don't think that is bothering you at this moment. :)

Good luck!

pietblok
Offline
Joined: 2003-07-17
Points: 0

I forgot to mention that, at that time, the default paper size was Letter, which was fine for people living in the US, but rather annoying for people living in Europe.

That is why I had to create my own A4 class.

Have a nice weekend.

Phil Race

Right, on windows, the default paper size is based on the default
for the printer which is what makes a lot more sense than a hard-coded
value. I believe this was fixed a LONG time ago (1.3 ie 1999-2000)

-phil.

java2d@JAVADESKTOP.ORG wrote:
> I forgot to mention that, at that time, the default paper size was Letter, which was fine for people living in the US, but rather annoying for people living in Europe.
>
> That is why I had to create my own A4 class.
>
> Have a nice weekend.
> [Message sent by forum member 'pietblok' (pietblok)]
>
> http://forums.java.net/jive/thread.jspa?messageID=241290
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".