On Wed, 3 Sep 2025 01:41:44 GMT, Phil Race <[email protected]> wrote:

> So what I think happens is we are setting a range within a range.

I agree with @prrace's assumption.

I temporary changed the CPrinterJob.m and PrinterView.m files to use fix values.
The PrinterView.knowsPageRanges() method returned the range with location=2 and 
length=4.
The CPrinterJob.javaPrinterJobToNSPrintInfo() method used the fromPage and 
toPage values from the table below.
I tested it with a Java program (see the code block below), which always 
returned 5 when calling Pageable.getNumberOfPages().

Pageable.getNumberOfPages() |knowsPageRanges()| javaPrinterJobToNSPrintInfo() | 
printed pages |
|--|---|-----------------|-----------------------------|
|numberOfPages=5|location=2, length=4|fromPage=1, toPage=1| 2|
|numberOfPages=5|location=2, length=4|fromPage=1, toPage=5|2, 3, 4, 5|
|numberOfPages=5|location=2, length=4|fromPage=5, toPage=5| nothing |
|numberOfPages=5|location=2, length=4|fromPage=3, toPage=6| 4, 5 |

In my opinion, the fix is correct, and we still need the `knowsPageRanges()` 
method, which returns the range of available pages with location=1 and 
length=Pageable.getNumberOfPages(). Then, the `javaPrinterJobToNSPrintInfo()` 
method can process the page range that will actually be printed.



public class PageRanges {

    public static final int PAGES_COUNT = 5;

    public static void main(String args[]) throws Exception {
        PrinterJob job = PrinterJob.getPrinterJob();
        if (job.getPrintService() == null) {
            System.out.println("No printers. Test cannot continue.");
            return;
        }

        Printable printable = (g, pf, pi) -> {
            if (pi >= PAGES_COUNT) {
                return Printable.NO_SUCH_PAGE;
            }
            g.drawString("Page : " + (pi+1), 200, 200);
            return Printable.PAGE_EXISTS;
        };


        job.setPageable(new Pageable() {
            @Override
            public int getNumberOfPages() {
                return PAGES_COUNT;
            }

            @Override
            public PageFormat getPageFormat(int pageIndex) throws 
IndexOutOfBoundsException {
                return new PageFormat();
            }

            @Override
            public Printable getPrintable(int pageIndex) throws 
IndexOutOfBoundsException {
                return printable;
            }
        });

        PrintRequestAttributeSet attributes = new 
HashPrintRequestAttributeSet();
        attributes.add(DialogTypeSelection.NATIVE);
        if (!job.printDialog(attributes)) {
            return;
        }
        job.print(attributes);
    }
}

-------------

PR Comment: https://git.openjdk.org/jdk/pull/11266#issuecomment-3414847846

Reply via email to