当我在Apache PDFBox 2.x中使用moveTo/lineTo/stroke的版本切换不推荐的drawLine()方法时,为什么我的线条会消失?

ham*_*314 4 java pdfbox

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

现在我已经从Apache PDFBox 1.8切换到2.0,因此现在不推荐使用某些方法,包括drawLine()-method.

API变更规定,你必须使用的其他3种方法相结合现在完成画线的任务:

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()
Run Code Online (Sandbox Code Playgroud)

因此我改变了这一行

final float tableWidth = page.findMediaBox().getWidth() - (2 * margin);
Run Code Online (Sandbox Code Playgroud)

到这一行:

final float tableWidth = page.getMediaBox().getWidth() - (2 * margin);
Run Code Online (Sandbox Code Playgroud)

并改变这两行

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

对此:

// 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();
Run Code Online (Sandbox Code Playgroud)

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

emptyLines

奇怪的是,如果我将弃用的drawLine()-method与moveTo / lineTo / stroke-methods 混合使用,我可以得到行或列:

// 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;
}
Run Code Online (Sandbox Code Playgroud)

有这个结果:

rowLines

drawLine为列添加不推荐的-method

// 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;
}
Run Code Online (Sandbox Code Playgroud)

有这个结果:

columnLines

所以这背后的数学用于绘制线条似乎没问题.但不知怎的,有一个我不理解的副作用,所以线条消失了?

如何将已弃用的drawLine-method 切换为不推荐的版本?

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

Fil*_*dor 10

contentStream.moveTo(margin, nexty);
contentStream.lineTo(margin, nexty);
contentStream.stroke();
Run Code Online (Sandbox Code Playgroud)

是一个.它应该是:

contentStream.moveTo(margin, nexty);
contentStream.lineTo(margin + tableWidth, nexty);
contentStream.stroke();
Run Code Online (Sandbox Code Playgroud)

如果绘制垂直线,则必须分别增加y坐标:

contentStream.moveTo(nextx, y); 
contentStream.lineTo(nextx, y - tableHeight); 
Run Code Online (Sandbox Code Playgroud)

我建议你做一个像这样的小帮手方法:

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
}
Run Code Online (Sandbox Code Playgroud)

  • 我已经为即将推出的版本改进了drawLine和addLine的弃用通知,所以希望下一个人能更好地理解这一点. (2认同)