Drawing shapes in Java for PDFs is easy with our PDF toolkit JPedal.
It only takes three steps:
- Download the JPedal trial jar
- Create your shape using Java’s Shape interface
- 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();
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.