Java Print API skipping first page

那年仲夏 提交于 2019-12-24 19:09:11

问题


I need some advice with the Java Print API. I have a program that goes through a Vector and prints each item on its own line. I'm trying to make it so when the line count reaches a certain number, it starts a new page. I've been looking at a Java Tutorial example to figure it out, but it's still not working. In debug mode, everything appears to be working properly, but for some reason it's printing the second page on the first sheet of paper and leaving the second sheet blank. Does anyone know what could be causing this?

EDIT: Here's the code for the print method:

    public int print(Graphics g, PageFormat pf, int page)
        throws PrinterException {
    page = pageNum;
    Paper paper = pf.getPaper();
    pf.setOrientation(PageFormat.PORTRAIT);
    paper.setSize(8.5 * 72, 11 * 72);
    paper.setImageableArea(0.5 * 72, 0.5 * 72, 7.5 * 72, 10 * 72);
    pf.setPaper(paper);

    if (page > pageTot) {
        return NO_SUCH_PAGE;
    }
    if (cov) {
        g = drawCenteredString(dateS, 108, g, 56, Font.BOLD);
        g = drawCenteredString("Don Stewart Daily Fulfillment", 216, g, 38,
                Font.BOLD);
        g = drawCenteredString(((FileNode) ((TreePath) printPaths.get(j))
                .getPathComponent(((TreePath) printPaths.get(j))
                        .getPathCount() - 5)).toString(), 324, g, 64,
                Font.BOLD);
        g = drawCenteredString(((TreePath) printPaths.get(j))
                .getPathComponent(
                        ((TreePath) printPaths.get(j)).getPathCount() - 3)
                .toString(), 432, g, 42, Font.BOLD);
        g = drawCenteredString("File Name Printed: "
                + ((TreePath) printPaths.get(j)).getLastPathComponent()
                        .toString(), 540, g, 14, Font.BOLD);
        g = drawCenteredString("File Location: "
                + ((FileNode) ((TreePath) printPaths.get(j))
                        .getLastPathComponent()).getFile()
                        .getAbsolutePath(), 648, g, 12, Font.PLAIN);
    }


    if (summ) {
        int lineCount;
        lineCount = 0;
        int lineSpacing = 14;
        int lineStart = 13 * 14;
        g.setFont(new Font("Dialog", Font.PLAIN, 10));
        boolean color = true;
            g = drawCenteredString(dateS, 72, g, 38, Font.BOLD);
            g = drawCenteredString("Don Stewart Daily Summary List - "
                    + (pageNum + 1) + " of " + pageTot, 120, g, 20, Font.BOLD);
            g.setFont(new Font("Dialog", Font.PLAIN, 10));
            g.drawString(
                    ((TreePath) printPaths.get(x-1))
                            .getPathComponent(
                                    ((TreePath) printPaths.get(x-1))
                                            .getPathCount() - 3).toString()
                            + " : "
                            + ((TreePath) printPaths.get(x-1))
                                    .getPathComponent(
                                            ((TreePath) printPaths.get(x-1))
                                                    .getPathCount() - 5)
                                    .toString(), 36, lineCount
                            * lineSpacing + lineStart);
            lineCount++;

        int k;
        for (k = x; k < printPaths.size() && lineCount <= 41; k++) {
            String type = ((TreePath) printPaths.get(k)).getPathComponent(
                    ((TreePath) printPaths.get(k)).getPathCount() - 5)
                    .toString();
            String date = ((TreePath) printPaths.get(k)).getPathComponent(
                    ((TreePath) printPaths.get(k)).getPathCount() - 3)
                    .toString();
            String typeU = ((TreePath) printPaths.get(k - 1))
                    .getPathComponent(
                            ((TreePath) printPaths.get(k)).getPathCount() - 5)
                    .toString();
            String dateU = ((TreePath) printPaths.get(k - 1))
                    .getPathComponent(
                            ((TreePath) printPaths.get(k)).getPathCount() - 3)
                    .toString();
            if (!(type == typeU) && (date == dateU)) {
                lineCount++;
                g.setColor(c1);
                g.drawString(date + " : " + type, 36, lineCount
                        * lineSpacing + lineStart);
                // lineCount++;
            }
            if (color)
                g.setColor(c1);
            else
                g.setColor(c2);
            g.drawString(((TreePath) printPaths.get(k))
                    .getLastPathComponent().toString(), 54, lineCount
                    * lineSpacing + lineStart);
            color = !color;
            lineCount++;
        }
        pageNum++;
        x = k;
    }
    return PAGE_EXISTS;
}

回答1:


I discovered that the tutorial example worked, so I ended up copying over the print() method from it, then modifying it with my specific drawString commands. I still don't know what was wrong with what I had written, but it's all working now.



来源:https://stackoverflow.com/questions/8217266/java-print-api-skipping-first-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!