vkrausser
vkrausser

Reputation: 393

Printing landscape documents with Java on duplex mode

I've a JasperReports report to be printed on landscape mode on a duplex printer. On this I've to support PCL5 and PCL6 printing drivers.

Searching on the internet, I discovered the following code snippet to do this job:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.attribute.standard.Sides;

public class PrintingTest {

    public static void main(String[] args) {
        try {
            //This are for configuration purpose
            String orientation = "LANDSCAPE";
            String duplexMode = "LONG_EDGE";

            int pageOrientation = 0;

            PrintRequestAttributeSet atr = new HashPrintRequestAttributeSet();
            if ("Landscape".equals(orientation)) {

                atr.add(OrientationRequested.LANDSCAPE);
                pageOrientation = PageFormat.LANDSCAPE;

            } else if ("Reverse_Landscape".equals(orientation)) {

                atr.add(OrientationRequested.REVERSE_LANDSCAPE);
                pageOrientation = PageFormat.REVERSE_LANDSCAPE;

            } else {
                atr.add(OrientationRequested.PORTRAIT);
                pageOrientation = PageFormat.PORTRAIT;
            }

            if ("LONG_EDGE".equals(duplexMode)) {
                atr.add(Sides.TWO_SIDED_LONG_EDGE);
            } else {
                atr.add(Sides.TWO_SIDED_SHORT_EDGE);
            }

            //Printing to the default printer
            PrintService printer = javax.print.PrintServiceLookup
                    .lookupDefaultPrintService();
            //Creating the printing job
            PrinterJob printJob = PrinterJob.getPrinterJob();

            printJob.setPrintService(printer);

            Book book = new Book();
            PageFormat pageFormat = printJob.defaultPage();

            pageFormat.setOrientation(pageOrientation);

            // Appending a exampledocument to the book
            book.append(new ExampleDocument(), pageFormat);

            // Appending another exampledocument to the book
            book.append(new ExampleDocument(), pageFormat);

            // Setting the Pageable to the printjob
            printJob.setPageable(book);

            try {
                // Here a could show the print dialog
                // printJob.printDialog(atr);

                // Here I pass the previous defined attributes
                printJob.print(atr);
            } catch (Exception PrintException) {
                PrintException.printStackTrace();
            }

        } catch (PrinterException ex) {
            ex.printStackTrace();
        }
    }

    public static final int MARGIN_SIZE = 72;

    private static class ExampleDocument implements Printable {

        public int print(Graphics g, PageFormat pageFormat, int page) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.translate(pageFormat.getImageableX(),
                    pageFormat.getImageableY());
            // Only on the first two documents...
            if (page <= 1) {
                // Prints using one inch margin
                g2d.drawString("Printing page " + page + " - duplex...",
                        MARGIN_SIZE, MARGIN_SIZE);
                return (PAGE_EXISTS);
            }

            return (NO_SUCH_PAGE);
        }
    }
}

This works fine on PCL6, but, when tested on PCL5, I noticed that LONG_EDGE and SHORT_EDGE rules are simple ignored. And that in both cases the job is sent as LONG_EDGE. This would not be a problem, except that Java AWT printing API resolves landscape printing by turning all pages 90º anti-clockwise causing the impression that it was printed on SHORT_EDGE mode.

Obs: I was able to print correctly with both configuration, SHORT_EDGE and LONG_EDGE, in portrait and in landscape, by using the SWT API. But I'm not able to convert the jasper printing request in a SWT compatible printing request.

My question is: has anybody ever confronted this situation? Which solution was given to it?

From my observation I've found these possible solutions:

  1. Instead of letting the AWT turn the page and send a portrait print request, force it to send it as a landscape printing request;

  2. Find a way to, when the page is in landscape mode, invert LONG_EDGE and SHORT_EDGE commands. Observe that for this one I would be obligated to correct the problem in which both are treated as LONG_EDGE requests.

  3. Convert the JasperReports to use SWT printing API.

Upvotes: 4

Views: 2906

Answers (1)

sbiz
sbiz

Reputation: 321

Not sure it helps but I had this issue 6 years ago and the only solution was to use the printer's (Xerox) capability to receive the printing directives through an .xpf file. Example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xpif SYSTEM "xpif-v02012.dtd">
<xpif cpss-version="2.01" version="1.0" xml:lang="en">
<xpif-operation-attributes>
    <job-name syntax="name" xml:lang="en" xml:space="preserve">job name</job-name>
    <requesting-user-name syntax="name" xml:space="preserve">domain user</requesting-user-name>
    </xpif-operation-attributes>
    <job-template-attributes>
        <copies syntax="integer">1</copies>
        <finishings syntax="1setOf">
        <value syntax="enum">3</value>
    </finishings>
    <job-recipient-name syntax="name" xml:lang="en-US" xml:space="preserve">domain user</job-recipient-name>
    <job-save-disposition syntax="collection">
        <save-disposition syntax="keyword">none</save-disposition>
    </job-save-disposition>
    <media-col syntax="collection">
        <media-type syntax="keyword">stationery</media-type>
        <media-hole-count syntax="integer">0</media-hole-count>
        <media-color syntax="keyword">blue</media-color>
        <media-size syntax="collection">
            <x-dimension syntax="integer">21000</x-dimension>
            <y-dimension syntax="integer">29700</y-dimension>
        </media-size>
    </media-col>
    <orientation-requested syntax="enum">3</orientation-requested>
    <sheet-collate syntax="keyword">collated</sheet-collate>
    <sides syntax="keyword">one-sided</sides>
    <x-side1-image-shift syntax="integer">0</x-side1-image-shift>
    <y-side1-image-shift syntax="integer">0</y-side1-image-shift>
</job-template-attributes>
</xpif>

which was sent to the printer along with the file needed to be printed.

Upvotes: 1

Related Questions