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?
我需要一些Java Print API的建議。我有一個程序通過Vector並在自己的行上打印每個項目。我試圖這樣做,當行數達到一定數量時,它會啟動一個新頁面。我一直在尋找一個Java Tutorial示例來解決它,但它仍然無法正常工作。在調試模式下,一切似乎都正常工作,但由於某種原因,它在第一張紙上打印第二頁並將第二張紙留空。有誰知道是什么原因引起的?
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 个解决方案
#1
0
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.
我發現教程示例有效,所以我最終從它復制了print()方法,然后用我特定的drawString命令修改它。我仍然不知道我所寫的內容有什么問題,但現在一切正常。