You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Roberto Nibali <rn...@gmail.com> on 2015/08/27 00:32:16 UTC
Equivalent replacements for drawLine and drawPolygon?
Hi
[Using PDFBox from latest SVN checkout]
Today, I toyed around by stamping objects onto a PDF and quickly moved
towards a set of abstracted functions to do so (one to draw a line, one to
set a text, one to draw a rectangle). When doing so, I hit some obsolete
functionality inside PDFBox, which I believe has no equivalent counterpart.
Consider the following methods:
private void addRect(PDDocument srcDoc, final PDPage page,
final float posX, final float posY, final float
offX, final float offY) throws IOException {
float[] rectX = new float[]{posX, posX + offX, posX + offX, posX};
float[] rectY = new float[]{posY, posY, posY + offY, posY + offY};
PDPageContentStream contentStream = new
PDPageContentStream(srcDoc, page, true, true,true);
contentStream.drawPolygon(rectX, rectY);
contentStream.close();
}
private void addLine(PDDocument srcDoc, final PDPage page,
final float fromX, final float fromY, final float
toX, final float toY) throws IOException {
PDPageContentStream contentStream = new
PDPageContentStream(srcDoc, page, true, true,true);
contentStream.drawLine(fromX, fromY, toX, toY);
//TODO needed??: contentStream.closeAndStroke();
contentStream.close();
}
Both of them contain deprecated method calls. However, I fail to see how
the suggested replacement functionality allows me to write the methods in
such an easy way I have done currently. Any suggestions?
Out of curiosity, is the closeAndStroke() call needed for the drawline()
functionality? I does not seem to make any difference to the end result.
Thanks and best regards
Roberto
Re: Equivalent replacements for drawLine and drawPolygon?
Posted by Roberto Nibali <rn...@gmail.com>.
Hi
On Thu, Aug 27, 2015 at 10:36 PM, John Hewson <jo...@jahewson.com> wrote:
> Those methods are deprecated in 1.8 and have been removed in 2.0. I’d
> recommend following
> the advice in the JavaDoc for those methods:
>
> @deprecated Use {@link #moveTo} and {@link #lineTo} methods instead.
>
> It’s trivial to call those methods for each point in an array. Note that
> there is no such thing as a
> line or a polygon in PDF which is why those methods were remove. There are
> only paths.
>
>
For my use case, I have solved it as follows (in case somebody else might
need it):
private void drawRect(PDDocument srcDoc, final PDPage page,
final float posX, final float posY, final float
offX, final float offY) throws IOException {
final float[] rectX = new float[]{posX, posX + offX, posX + offX, posX};
final float[] rectY = new float[]{posY, posY, posY + offY, posY + offY};
drawPolygon(srcDoc, page, rectX, rectY);
}
private void drawPolygon(PDDocument srcDoc, final PDPage page, float[]
x, float[] y) throws IOException {
if (x.length != y.length) {
throw new IllegalArgumentException("Error: some points are
missing coordinates");
}
PDPageContentStream contentStream = new
PDPageContentStream(srcDoc, page, true, true,true);
contentStream.moveTo(x[0], y[0]);
for (int i = 0; i < x.length; i++) {
contentStream.lineTo(x[i], y[i]);
}
//IntStream.range(0, x.length).forEach(i ->
contentStream.lineTo(x[i], y[i]));
contentStream.closePath();
contentStream.stroke();
contentStream.close();
}
private void drawLine(PDDocument srcDoc, final PDPage page,
final float fromX, final float fromY, final
float toX, final float toY) throws IOException {
PDPageContentStream contentStream = new
PDPageContentStream(srcDoc, page, true, true,true);
contentStream.moveTo(fromX, fromY);
contentStream.lineTo(toX, toY);
//TODO: What's the perceived difference between close() and closeAndStroke()
//contentStream.closeAndStroke();
contentStream.stroke();
contentStream.close();
}
Improvements are still possible.
Thanks and best regards
Roberto
Re: Equivalent replacements for drawLine and drawPolygon?
Posted by John Hewson <jo...@jahewson.com>.
Those methods are deprecated in 1.8 and have been removed in 2.0. I’d recommend following
the advice in the JavaDoc for those methods:
@deprecated Use {@link #moveTo} and {@link #lineTo} methods instead.
It’s trivial to call those methods for each point in an array. Note that there is no such thing as a
line or a polygon in PDF which is why those methods were remove. There are only paths.
— John
> On 26 Aug 2015, at 15:32, Roberto Nibali <rn...@gmail.com> wrote:
>
> Hi
>
> [Using PDFBox from latest SVN checkout]
>
> Today, I toyed around by stamping objects onto a PDF and quickly moved
> towards a set of abstracted functions to do so (one to draw a line, one to
> set a text, one to draw a rectangle). When doing so, I hit some obsolete
> functionality inside PDFBox, which I believe has no equivalent counterpart.
> Consider the following methods:
>
> private void addRect(PDDocument srcDoc, final PDPage page,
> final float posX, final float posY, final float
> offX, final float offY) throws IOException {
> float[] rectX = new float[]{posX, posX + offX, posX + offX, posX};
> float[] rectY = new float[]{posY, posY, posY + offY, posY + offY};
> PDPageContentStream contentStream = new
> PDPageContentStream(srcDoc, page, true, true,true);
> contentStream.drawPolygon(rectX, rectY);
> contentStream.close();
> }
>
> private void addLine(PDDocument srcDoc, final PDPage page,
> final float fromX, final float fromY, final float
> toX, final float toY) throws IOException {
> PDPageContentStream contentStream = new
> PDPageContentStream(srcDoc, page, true, true,true);
> contentStream.drawLine(fromX, fromY, toX, toY);
> //TODO needed??: contentStream.closeAndStroke();
> contentStream.close();
> }
>
> Both of them contain deprecated method calls. However, I fail to see how
> the suggested replacement functionality allows me to write the methods in
> such an easy way I have done currently. Any suggestions?
>
> Out of curiosity, is the closeAndStroke() call needed for the drawline()
> functionality? I does not seem to make any difference to the end result.
>
> Thanks and best regards
> Roberto