Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1072121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:48:46+00:00 2026-05-16T20:48:46+00:00

This is the basic printing program example from the Sun tutorial : PrinterJob job

  • 0

This is the basic printing program example from the Sun tutorial:

PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(new HelloWorldPrinter());
boolean doPrint = job.printDialog();
if (doPrint) {
    try {
        job.print();
    } catch (PrinterException e) {
        /* The job did not successfully complete */
    }
}

When the user is shown the print dialog (on the second line), he can choose to print only a range of pages from the document. Can I somehow get the number of pages that are to be printed? For example, if I have a 25 page document, but the user chooses to print the range 4-10, then only 7 pages will be printed. Is there someway to access that information?

I need this in order to show a progress bar which increases with each page printed, but for that I need to know the total number of pages that are going to be printed.

So how can I get that number?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:48:46+00:00Added an answer on May 16, 2026 at 8:48 pm

    I’ve managed to find a solution.

    The printDialog() method displays a native print dialog, but the printDialog(PrintRequestAttributeSet attributes) method shows a cross-platform dialog. The PrintRequestAttributeSet parameter is filled out with the user’s selections, including the page range selected to be printed. Thus, after returning from the printDialog method, the page range can be queried, like in the following code sequence:

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(new HelloWorldPrinter());
    HashPrintRequestAttributeSet printParams = new HashPrintRequestAttributeSet();
    boolean ok = job.printDialog(printParams);
    if (ok) {
        PageRanges pageRanges = (PageRanges) printParams.get(PageRanges.class);
        int pagesToBePrinted = getNumberOfPages(pageRanges);
        try {
            job.print(printParams);
        } catch (PrinterException e) {
            /* The job did not successfully complete */
        }
    }
    

    Note that the printParams has to be given to the print() method as well. From the PageRanges object, the page ranges can be obtained in array format, i.e. an array of 1-length arrays meaning a single page each or 2-length arrays meaning contiguous ranges of pages. See the javadoc for more details. To calculate the total number of pages is straightforward:

    int getNumberOfPages(PageRanges pageRanges) {
        int pages = 0;
        int[][] ranges = pageRanges.getMembers();
        for (int i = 0; i < ranges.length; i++) {
            pages += 1;
            if (ranges[i].length == 2) {
                pages += ranges[i][1] - ranges[i][0];
            }
        }
        pages = Math.min(pages, totalPagesOfDocument);
        return pages;
    }
    

    If the user doesn’t select a page range, but the ‘All pages’ option instead, then the PageRanges will contain the range (1, Integer.MAX_VALUE). So I say that if the computed value exceeds the number of pages of the document, then the number of pages to be printed is the total number of the document’s pages (which I hope you know from somewhere).

    The algorithm is perhaps overkill, as probably the PageRanges will only be a simple n – m range, but better safe than sorry.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading this basic tutorial on canvas elements . The (almost) in the following
I have picked a basic example of printing Hello World on screen when the
This basic app which was given to me as an project should run quite
I've created this basic 3D Demo using OpenGL/SDL. I handled the keyboard callback so
Hi I have this basic random quote script, but I would like it to
I have a js.erb with this basic content: $(#currencies).html(<%= options_for_select([['Dollar', '$']])%>); And the html
Forgive my ignorance in asking this basic question but I've become so used to
I'm trying to learn this basic thing about processors that should be taught in
Its a interview question. Interviewer asked this basic shell script question when he understand
This is basic stuff but I can't get this following form to process reliably.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.