当我在 Apache PDFBox 2.x 中为带有 moveTo / lineTo / stroke 的版本切换已弃用的 drawLine() 方法时,为什么我的线条消失了?

Why are my lines disappearing when I switch the deprecated drawLine() method for the version with moveTo / lineTo / stroke in Apache PDFBox 2.x?

我正在通过这个例子创建一个 table:http://fahdshariff.blogspot.de/2010/10/creating-tables-with-pdfbox.html

现在我已经从 Apache PDFBox 1.8 切换到 2.0,所以一些方法现在已被弃用,包括 drawLine() 方法。

你现在必须结合使用其他3种方法来完成画线任务的API changelog states:

org.apache.pdfbox.pdmodel.PDPageContentStream.drawLine(float, float, float, float):
Use PDPageContentStream.moveTo(float, float) 
followed by PDPageContentStream.lineTo(float, float) 
followed by PDPageContentStream.stroke()

因此我更改了这一行

final float tableWidth = page.findMediaBox().getWidth() - (2 * margin);

到这一行:

final float tableWidth = page.getMediaBox().getWidth() - (2 * margin);

并更改这两行

// draw the rows
contentStream.drawLine(margin, nexty, margin + tableWidth, nexty);
(...)
// draw the columns
contentStream.drawLine(nextx, y, nextx, y - tableHeight);

对此:

// draw the rows
contentStream.moveTo(margin, nexty);
contentStream.lineTo(margin, nexty);
contentStream.stroke();
(...)
// draw the columns
contentStream.moveTo(nextx, y - tableHeight);
contentStream.lineTo(nextx, y - tableHeight);
contentStream.stroke();

但是如果我这样做,所有的行都会消失并且不会出现在 PDF 中:

奇怪的是,如果我将已弃用的 drawLine() 方法与 moveTo / lineTo / stroke 方法混合使用,我可以获得行或列:

// draw the rows
float nexty = y;
for (int i = 0; i <= rows; i++) {
    contentStream.drawLine(margin, nexty, margin + tableWidth, nexty);
    // contentStream.moveTo(margin, nexty);
    // contentStream.lineTo(margin, nexty);
    // contentStream.stroke();
    nexty -= rowHeight;
}

// draw the columns
float nextx = margin;
for (int i = 0; i <= cols; i++) {
    // contentStream.drawLine(nextx, y, nextx, y - tableHeight);
    contentStream.moveTo(nextx, y - tableHeight);
    contentStream.lineTo(nextx, y - tableHeight);
    contentStream.stroke();
    nextx += colWidth;
}

有这样的结果:

为列启用已弃用的 drawLine 方法

// draw the rows
float nexty = y;
for (int i = 0; i <= rows; i++) {
    // contentStream.drawLine(margin, nexty, margin + tableWidth,
    // nexty);
    contentStream.moveTo(margin, nexty);
    contentStream.lineTo(margin, nexty);
    contentStream.stroke();
    nexty -= rowHeight;
}

// draw the columns
float nextx = margin;
for (int i = 0; i <= cols; i++) {
    contentStream.drawLine(nextx, y, nextx, y - tableHeight);
    // contentStream.moveTo(nextx, y - tableHeight);
    // contentStream.lineTo(nextx, y - tableHeight);
    // contentStream.stroke();
    nextx += colWidth;
}

有这样的结果:

所以画线背后的数学似乎没问题。但是不知何故有一个我不理解的副作用,所以线条消失了?

如何将已弃用的 drawLine 方法切换到未弃用的版本?

示例代码是 MVCE,因此如果您想测试我的问题,只需将示例代码从此处复制到您选择的编辑器中:http://fahdshariff.blogspot.de/2010/10/creating-tables-with-pdfbox.html

contentStream.moveTo(margin, nexty);
contentStream.lineTo(margin, nexty);
contentStream.stroke();

是一个。应该是:

contentStream.moveTo(margin, nexty);
contentStream.lineTo(margin + tableWidth, nexty);
contentStream.stroke();

如果你画一条垂直线,你必须分别增加y坐标:

contentStream.moveTo(nextx, y); 
contentStream.lineTo(nextx, y - tableHeight); 

我建议你做一个像这样的小辅助方法:

private void drawLine( PDPageContentStream content, float xstart, float ystart, float xend, float yend ){
    content.moveTo(xstart, ystart); // moves "pencil" to a position
    content.lineTo(xend, yend);     // creates an invisible line to another position
    content.stroke();               // makes the line visible
}