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

  • Home
  • SEARCH
  • 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 4339750
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:15:26+00:00 2026-05-21T11:15:26+00:00

I’m new to printing with java. First I start to print one page data

  • 0

I’m new to printing with java. First I start to print one page data and all was ok. Now I start to test how to paginate my data. I have a class that implements the printable interface and I have an int variable (curLine) to keep the number of rows I have already print and an variable lines to keep the total number I want to print finally. In public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) method when I use the graphics.drawString(…); method to draw a row I increase the value of the metric(curLine++) but in the printer I didn’t get the results I want.I think that the print methold called twice for a specific indexPage, in that two calls the data printed only one, but the curLine variable increase in the two calls too with result to “lost” rows from the output.

More specific my test class is

public class PrintTest implements Printable {

private final int marginTop = 20;
private final int marginLeft = 10;
private Font mainFont = new Font("Verdana", Font.PLAIN, 10);
private Font bigFont = new Font("Verdana", Font.PLAIN, 14);
private int lines;
private int curLine;

private boolean finish = false;

public PrintTest() {
    curLine = 0;
    lines = 160;
}

public static void main(String[] args) {
    PrinterJob job = PrinterJob.getPrinterJob(); // Use default printer
    job.printDialog();
    PrintTest t = new PrintTest();
    job.setPrintable(t);
    try {
        job.print();
    } catch (PrinterException e) {
        e.printStackTrace();
    }

}

@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
        throws PrinterException {
    //if printed all rows then return no_such_page
    if (finish) {
        return NO_SUCH_PAGE;
    }

    Graphics2D g2 = (Graphics2D) graphics;

    double pageH = pageFormat.getImageableHeight();
    g2.setFont(mainFont);
    int t = g2.getFontMetrics().charWidth('T');
    int lineH = g2.getFontMetrics().getHeight();

    double curY;

    g2.translate(pageFormat.getImageableX() + marginLeft,
            curY = pageFormat.getImageableY() + marginTop);

    while ((curY < (pageH - lineH)) && (curLine < lines)) {
        g2.translate(0, lineH);
        g2.drawString("Print row number #" + curLine, 0, 0);
        curY += lineH;
        curLine++;
    }

    if (curLine >= lines) {
        finish = true;
    }

    return PAGE_EXISTS;

}

}

and you can see the output here https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0ByN6KrI39kzuMTRmZDA5NTMtOGJjZi00ZmRhLThlYWYtMzUwNzE0NTdkMjcz&hl=en&authkey=CLa-svAG

You can see that it wasn’t start from row 0

  • 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-21T11:15:27+00:00Added an answer on May 21, 2026 at 11:15 am

    Note: this solution was originally provided by the question OP.

    public class PrintTest implements Printable {
    
        private final int marginTop = 20;
        private final int marginLeft = 10;
        private Font mainFont = new Font("Verdana", Font.PLAIN, 10);
        private Font bigFont = new Font("Verdana", Font.PLAIN, 14);
        private int lines;
    
        private int headerHeighInLines = 5;
        private int lineH = -1;
        private double pageH = -1;
        private int pages = -1;
    
        private boolean finish = false;
    
        public PrintTest() {
    
            lines = 30;// set the total number of rows we want to print
        }
    
        public static void main(String[] args) {
            PrinterJob job = PrinterJob.getPrinterJob(); // Use default printer
            job.printDialog();
            PrintTest t = new PrintTest();
            job.setPrintable(t);
            try {
                job.print();
            } catch (PrinterException e) {
                e.printStackTrace();
            }
    
        }
    
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
    
            // if printed all pages then no_such_page
            if (pageIndex >= getPages()) {
                return NO_SUCH_PAGE;
            }
    
            Graphics2D g2 = (Graphics2D) graphics;
            AffineTransform defaultTransform = g2.getTransform();
            if (pageH == -1) {
    
                pageH = pageFormat.getImageableHeight();
    
            }
            g2.setFont(mainFont);
            int charW = g2.getFontMetrics().charWidth('T');
            if (lineH == -1) {
                lineH = g2.getFontMetrics().getHeight();
            }
    
            double curY;
    
            g2.translate(pageFormat.getImageableX() + marginLeft, curY = pageFormat.getImageableY() + marginTop);
    
            // if we have the first page we can print a title
            // or a description
            if (pageIndex == 0) {
                g2.translate(0, lineH * headerHeighInLines);
            }
    
            int[] res = getNumLines(pageIndex);
    
            for (int i = res[0]; i < res[1]; i++) {
                g2.translate(0, lineH);
                g2.drawString("Print row number #" + i, 0, 0);
            }
    
            // code to add the page number at footer
            g2.setTransform(defaultTransform);
            g2.drawString("page #" + (pageIndex + 1), (int) (pageFormat.getImageableWidth() - charW * 9),
                    (int) (pageFormat.getImageableHeight() - (double) lineH / 2));
    
            return PAGE_EXISTS;
    
        }
    
        /**
         * 
         * @param pageIndex
         * @return an int array with contains two values, the num of row to start
         *         printing and the num of row to finish the printing in the
         *         specific pageindex
         */
        public int[] getNumLines(int pageIndex) {
    
            int[] res = new int[2];
    
            if (pageIndex == 0) {
                res[0] = 0;
                res[1] = firstPageLines();
                return res;
            }
            double tmpH = pageH - 2 * marginTop;
            double numlines = (double) (tmpH / lineH);
            res[0] = (int) (firstPageLines() + (pageIndex - 1) * numlines);
            res[1] = (int) Math.min((res[0] + numlines), lines + 1);
    
            return res;
        }
    
        public int firstPageLines() {
            double tmpPageH = pageH - 2 * marginTop - lineH * headerHeighInLines;
            // return the minumum of expected lines to print and the total number of
            // lines
            return (int) Math.min((tmpPageH / lineH), lines + 1);
        }
    
        /**
         * 
         * @return int the total number of pages to print
         */
        public int getPages() {
            if (firstPageLines() >= lines) {
                return 1;
            }
            // we substract the lines in the first page
            // because may will be less than in the number of
            // line in the other pages
            // later we increase the total nubmer of pages with 1 for the first page
            int tmpLines = lines - firstPageLines();
    
            int[] tmp = getNumLines(1);
            int linePerPage = tmp[1] - tmp[0];
            pages = tmpLines / linePerPage;
            if (tmpLines % linePerPage > 0) {
                pages++;
            }
            pages++;
    
            return pages;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.