0

I have been working on implementing a simple application that reads pdf, then put some of the infos in a JPanel, then generate a bufferedimage and try to send it to the printer. the code generates the image perfectly and send it to be printed. it works perfectly with A4 format and many other format, but I'm looking to print it on a pvc cr80 Card (the printer prints 1016*642 pixels)

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Container c = jPanel3;
    BufferedImage im = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
    c.paint(im.getGraphics());


    try {
        String cn = UIManager.getSystemLookAndFeelClassName();
        UIManager.setLookAndFeel(cn); // Use the native L&F
    } catch (Exception cnf) {
    }

    PrinterJob printJob = PrinterJob.getPrinterJob();

    printJob.setPrintable(new Printable() {
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            if (pageIndex > 0) {
                return NO_SUCH_PAGE;
            }


            Graphics2D g2d = (Graphics2D)graphics;


            g2d.translate((int) pageFormat.getImageableWidth(),pageFormat.getImageableHight());

            g2d.drawImage(im, 0, 0,1016,642, null);

            return PAGE_EXISTS;
        }
    });
    boolean ok = printJob.printDialog();
    if (ok) {
        try {
            printJob.print();
        } catch (PrinterException ex) {
            System.out.println("Can't print");
        }
    }

}  

this code doesn't print on the pvc cr80 Card printer (1016*642 pixels). how can I fix that?

Ayman
  • 1
  • 1
  • `c.paint(im.getGraphics());` makes me cry. Don't use `paint` or `paintAll`, instead use `print` or `printAll` instead. Verify the page width and height when `print` is called (from the properties of the `pageFormat`) to make sure they meet your expectations – MadProgrammer Jan 13 '16 at 23:05
  • You might also consider having a look at [this example](http://stackoverflow.com/questions/22241711/setting-print-size-of-a-jlabel-and-put-a-jradiobutton-on-the-print/22242658#22242658) and [this example](http://stackoverflow.com/questions/12764634/printing-a-jframe-and-its-components/12765916#12765916) and [these examples](http://stackoverflow.com/questions/22241711/setting-print-size-of-a-jlabel-and-put-a-jradiobutton-on-the-print/22244116#22244116) and [this example](http://stackoverflow.com/questions/17904518/fit-scale-jcomponent-to-page-being-printed/17961911#17961911) – MadProgrammer Jan 13 '16 at 23:07
  • 1
    Why do you translate x and y to width, not x to width and y to height? – thst Jan 13 '16 at 23:08
  • and [this example](http://stackoverflow.com/questions/28427566/save-jpanel-as-image-hd-quality/28492187#28492187) – MadProgrammer Jan 13 '16 at 23:09
  • 1
    Further to thst's excellent observations, you should be using `PageFormat#getImageableX` and `PageFormat#getImageableY` – MadProgrammer Jan 13 '16 at 23:11
  • @MadProgrammer I was just looking up why it should make sense to translate to the imageable width area at all. I just wanted to write the comment, but you were faster :-) – thst Jan 13 '16 at 23:15
  • @MadProgrammer, I'm using (c.paint(im.getGraphics());) to paint the image as BufferedImage, I tried saving it as .jpg file and it's working perfectly, my problem is with the printing function inside the printJob.setPrintable(...) function. can you please clarify from where is the issue ? – Ayman Jan 13 '16 at 23:44
  • I'm using the PageFormat#getImageableX and PageFormat#getImageableY in this line of code: g2d.translate((int) pageFormat.getImageableWidth(),pageFormat.getImageableHight()); – Ayman Jan 13 '16 at 23:45
  • `paint` won't always work, it's not designed to do what you're trying to do. [`print`](https://docs.oracle.com/javase/8/docs/api/javax/swing/JComponent.html#print-java.awt.Graphics-) and [`printAll`](https://docs.oracle.com/javase/8/docs/api/javax/swing/JComponent.html#printAll-java.awt.Graphics-) are – MadProgrammer Jan 13 '16 at 23:46
  • How is `getImageableX` the same as `getImageableWidth`? – MadProgrammer Jan 13 '16 at 23:46
  • @MadProgrammer I'm sorry, I wasn't clear, what I did is test with PageFormat#getImageableX and Y it wasn't working, then I tested with 0, 0, then after with getImageableWidth; what I wanted to ask you is what g2d.translate() function do exactly ? – Ayman Jan 14 '16 at 00:51
  • @Ayman `Graphics2D#translate` changes the origin of the `Graphics` context, that is, the `0x0` point. Because paper (traditionally) has a margin, you would need to constantly change all your code to include this (so you don't try and print in areas which the print might not be able to print in), so instead, you translate the origin point, so everything is "moved" automatically – MadProgrammer Jan 14 '16 at 00:53
  • @MadProgrammer thank you so much for responding fast, another consern is that Graphics2D#translate accept an int so we have to cast getImageableX to an int; but aren't we going to lose some of its value also, if I may, does g2d.drawImage(im, 0, 0,1016,642, null); adjust the image to fit exactly 1016*642 pixels, I really don't get why this is not printing anything on the physical printer? – Ayman Jan 14 '16 at 01:00
  • @Ayman `translate` is part of the (older) `Graphics` API, so it only supports `int`, it's difficult to say if you would lose any large amount of precision because of this. Yes `drawImage` will scale the image to the exact measurements you supply (so the resulting image will be 1016x642 pixels in size). I tend to print to a PDF driver first for test, this helps eliminate some of the issues, as printers can be fickle some times – MadProgrammer Jan 14 '16 at 01:03
  • I tested the marginal loss already for a A4 printer it's X:17.039999999999964 and Y:12.0 any ways, a pvc cr80 card printer (prints 1016*642 pixels) does not print this and I'm really stuck for days now trying to figure out what is causing the problem, am I doing wrong – Ayman Jan 14 '16 at 01:16
  • I'm assuming the CR80 card is 3.370'x2.125' (8.57665cmx5.408125cm)? – MadProgrammer Jan 14 '16 at 01:24
  • no, it's 13.55'x8.56' (26.881666667x16.98625cm) and 1016x642 pixels, when I run the code and send it to the printer nothing comes out, when I set the graphics with getImageableX and Y it prints a tiny image in the middle of the card – Ayman Jan 14 '16 at 01:42
  • At what DPI are your pixel calculations been made? Java Printing actually works at 72dpi for all it's measurements, so `26.881666667x16.98625cm` would actually be 762.0x481.5 pixels (@72 DPI) – MadProgrammer Jan 14 '16 at 02:55
  • @MadProgrammer how can I change this default value to fit our measurments? – Ayman Jan 14 '16 at 13:18
  • Well, technically you need to work at the assumption of 72dpi, the printer API will take care of the rest – MadProgrammer Jan 14 '16 at 19:27

0 Answers0