【问题标题】:Java Print API skipping first pageJava Print API 跳过第一页
【发布时间】:2011-11-23 07:41:38
【问题描述】:

我需要一些关于 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 页(请记住,在 Java 中,第一个数字通常是 0)
  • 我只是用一些打印语句来测试它,它确实从 0 开始。之前我不小心将它设置为 1,但现在已经修复了。然而,有趣的是,页数上升到 3,而它应该只上升到 1。我试图找出原因,因为这似乎是造成它的原因。
  • 不幸的是我不习惯这个库。我可以建议您复制/粘贴教程示例并确保它可以正常工作,然后开始一次添加一个部分并查看它在哪里中断。这应该可以让您查明错误的原因。

标签: java printing pagination


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    相关资源
    最近更新 更多