Skip to main content

Add Shapes to PDF Documents

  • 2 minutes to read

The PDF Document API uses path fragments to draw vector shapes on a page. A PathFragment object defines lines, curves, and filled regions. You can use it to create custom shapes, such as rectangles, triangles, and Bézier curves.

Add a Shape

Create a PathFragment object to define a shape and its appearance. Use GraphicsPath to specify geometry.

The following code snippet adds a triangle to a page:

import com.devexpress.docs.pdf.*;
import com.devexpress.drawing.*;
import com.devexpress.system.drawing.*;
import com.devexpress.drawing.printing.*;

import java.nio.channels.*;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws Exception {

        // Create a new PDF document.
        try (PdfDocument pdfDocument = new PdfDocument()) {

            // Add a Letter-size page to the document.
            Page page = pdfDocument.getPages().add(DXPaperKind.LETTER);

            // Create a triangle geometry.
            GraphicsPath triangle = new GraphicsPath(new PointF(50, 500));
            triangle.appendLineSegment(new PointF(50, 300));
            triangle.appendLineSegment(new PointF(250, 300));
            triangle.setClosed(true);

            // Create a path fragment for a triangle shape.
            PathFragment triangleFragment = new PathFragment();

            // Assign geometry to the fragment.
            triangleFragment.getPaths().add(triangle);

            // Specify how the shape is rendered (fill + outline).
            triangleFragment.setAction(PathShapeAction.FILL_AND_STROKE);

            // Set fill and outline colors.
            triangleFragment.setFill(Fill.createSolid(PdfColor.getOrange()));
            triangleFragment.setOutline(
                    Outline.create(Fill.createSolid(PdfColor.getDarkOrange())));

            // Add the shape to the page.
            page.addFragment(triangleFragment);

            // Save the document to a PDF file.
            try (WritableByteChannel writableByteChannel =
                     FileChannel.open(Path.of("result.pdf"),
                         StandardOpenOption.CREATE,
                         StandardOpenOption.WRITE,
                         StandardOpenOption.TRUNCATE_EXISTING)) {

                pdfDocument.save(writableByteChannel);
            }
        }
    }
}

Add a Bézier Curve

The following code snippet creates a Bézier curve and adds it to the page:

GraphicsPath bezier = new GraphicsPath(new PointF(210, 375));

bezier.appendBezierSegment(
        new PointF(210, 320),
        new PointF(350, 320),
        new PointF(350, 375));

bezier.appendBezierSegment(
        new PointF(350, 430),
        new PointF(210, 430),
        new PointF(210, 375));

PathFragment bezierFragment = new PathFragment();
bezierFragment.getPaths().add(bezier);
bezierFragment.setAction(PathShapeAction.FILL_AND_STROKE);
bezierFragment.setFill(Fill.createSolid(PdfColor.getOrange()));
bezierFragment.setOutline(Outline.create(Fill.createSolid(PdfColor.getDarkOrange())));

page.addFragment(bezierFragment);

Add a Rectangle Shape

Use the PathFragment.rectangle method to create rectangular shapes. Specify the position, size, fill color, and outline.

PathFragment rectangle = PathFragment.rectangle(new RectangleF(100, 100, 200, 200));
rectangle.setOutline(Outline.create(Fill.createSolid(PdfColor.getDarkOrange()), 5));
rectangle.setFill(Fill.createSolid(PdfColor.getOrange()));

page.addFragment(rectangle);

Next Steps