pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Roberto Nibali <rnib...@gmail.com>
Subject Re: Equivalent replacements for drawLine and drawPolygon?
Date Tue, 08 Sep 2015 21:11:28 GMT

On Thu, Aug 27, 2015 at 10:36 PM, John Hewson <john@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]));

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()

Improvements are still possible.

Thanks and best regards

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message