> What do you think is the simplest way to draw a circle of a certain radius 
or diameter [...]?
> I hoped for "addCircle" but didn't find that. 

I found this example source code:
https://svn.apache.org/viewvc/pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateRadioButtons.java?revision=1910171&view=co
 

And it contains a small inconspicuous helper method "drawCircle", which 
actually solves the problem with only a few lines of code and without any 
additional dependencies! 😃
I've renamed and slightly altered it (closeAndStroke). 

Have a look at this runnable example code:

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;

public class CircleTest {

  public static void main( String[] args ) throws IOException {
    File outputFile = Files.createTempFile( "PDFCircleTest_", ".pdf" ).toFile();
    try( PDDocument pdDocument = new PDDocument() ) {
      PDPage page = new PDPage( PDRectangle.A4 );
      pdDocument.addPage( page );
      try( PDPageContentStream cs = new PDPageContentStream( pdDocument, page ) 
) {
        cs.setStrokingColor( 0 );
        addCircle( cs, 200, 600, 80 );
      }
      pdDocument.save( outputFile );
    }
    Desktop.getDesktop().open( outputFile );
  }
  
  // Taken and modified from source: 
  // 
https://svn.apache.org/viewvc/pdfbox/branches/2.0/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/CreateRadioButtons.java?view=markup
  private static void addCircle( PDPageContentStream cs, float x, float y, 
float r) throws IOException {
    // http://stackoverflow.com/a/2007782
    float magic = r * 0.551784f;
    cs.moveTo(x, y + r);
    cs.curveTo(x + magic, y + r, x + r, y + magic, x + r, y);
    cs.curveTo(x + r, y - magic, x + magic, y - r, x, y - r);
    cs.curveTo(x - magic, y - r, x - r, y - magic, x - r, y);
    cs.curveTo(x - r, y + magic, x - magic, y + r, x, y + r);
    cs.closeAndStroke();
  }

}


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: users-h...@pdfbox.apache.org

Reply via email to