DEV Community

IDRSolutions
IDRSolutions

Posted on

How to draw shapes in PDF files in Java (Tutorial)

Drawing shapes in Java for PDFs is easy with our PDF toolkit JPedal.

It only takes three steps:

  1. Download the JPedal trial jar
  2. Create your shape using Java’s Shape interface
  3. Run the following code snippet
final PdfManipulator pdf = new PdfManipulator();
pdf.loadDocument(new File("inputFile.pdf"));
final Shape shape = new Rectangle2D.Float(56.7f, 596.64f, 131.53f, 139.25f);
final DrawParameters params = new DrawParameters();
params.setStrokeColor(new float[] {1, 0, 0});
params.setFillRule(DrawParameters.STROKE);
pdf.addShape(1, shape, params);
pdf.apply();
pdf.writeDocument(new File("outputFile.pdf"));
pdf.closeDocument();
Enter fullscreen mode Exit fullscreen mode

For more information on drawing shapes in Java for PDFs, and other manipulations you can perform with JPedal, check out our support site.

We have been working with PDF technology for the more than 2 decades! Please feel free to read our other blog posts to better understand the PDF format.

Top comments (0)