Java Print API跳过第一页

时间:2011-11-21 19:19:09

标签: java printing pagination

我需要一些Java Print API的建议。我有一个程序通过Vector并在自己的行上打印每个项目。我试图这样做,当行数达到一定数量时,它会启动一个新页面。我一直在寻找一个Java Tutorial example来解决它,但它仍然没有用。在调试模式下,一切似乎都正常工作,但由于某种原因,它在第一张纸上打印第二页并将第二张纸留空。有谁知道这可能导致什么?

编辑:这是打印方法的代码:

    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 个答案:

答案 0 :(得分:0)

我发现教程示例有效,所以我最终复制了print()方法,然后使用我的特定drawString命令对其进行修改。我仍然不知道我所写的内容有什么问题,但现在一切正常。