Am 31.07.2025 um 06:22 schrieb Tilman Hausherr:

I think the bug is in the jdk, and I'll write something small later to prove this.

And here it is, I asked ChatGPT ("Create a program in java that uses the Pageable interface to print a 10 pages document with the numbers from 1 to 10. The print option dialog should be used for additional manual configuration.") and modified it slightly:


public class PageableTest extends Book
{
    private final int numPages = 2;

    public static void main(String[] args)
    {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPageable(new PageableTest());

        // Show print dialog for manual configuration
        if (job.printDialog())
        {
            try
            {
                job.print();
            }
            catch (PrinterException e)
            {
                e.printStackTrace();
            }
        }
    }

    // Return the number of pages
    @Override
    public int getNumberOfPages()
    {
        return numPages;
    }

    // Not needed for this example
    @Override
    public PageFormat getPageFormat(int pageIndex) throws IndexOutOfBoundsException
    {
        if (pageIndex >= numPages)
        {
            throw new IndexOutOfBoundsException("Invalid page index");
        }
        return PrinterJob.getPrinterJob().defaultPage();
    }

    // Return the Printable that will render the content
    @Override
    public Printable getPrintable(int pageIndex) throws IndexOutOfBoundsException
    {
        if (pageIndex >= numPages)
        {
            throw new IndexOutOfBoundsException("Invalid page index");
        }

        return new Printable()
        {
            @Override
            public int print(Graphics g, PageFormat pf, int index) throws PrinterException
            {
                Graphics2D g2d = (Graphics2D) g;
                g2d.translate(pf.getImageableX(), pf.getImageableY());
                g2d.setFont(new Font("Serif", Font.BOLD, 72));
                g2d.drawString(String.valueOf(index + 1), 100, 100);
                return Printable.PAGE_EXISTS;
            }
        };
    }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: users-h...@pdfbox.apache.org

Reply via email to