Add Curve Shapes to PDF in Python

Add Curve object

Aspose.PDF for Python via .NET lets you add Curve shapes to PDF pages through the Graph class.

This article shows how to create both outlined and filled curves.

Follow the steps below:

  1. Create Document instance.
  2. Create Graph object with certain dimensions.
  3. Set border for Graph object.
  4. Add Graph object to paragraphs collection of page.
  5. Save our PDF file.
import aspose.pdf as ap
import aspose.pdf.drawing as drawing

def add_curve(outfile: str):
    document = ap.Document()
    page = document.pages.add()
    graph = drawing.Graph(400, 200)
    graph.border = ap.BorderInfo(ap.BorderSide.ALL, ap.Color.green)

    curve1 = drawing.Curve([10, 10, 50, 60, 70, 10, 100, 120])
    curve1.graph_info.color = ap.Color.green_yellow
    graph.shapes.add(curve1)

    page.paragraphs.add(graph)
    document.save(outfile)

The following picture shows the result executed with our code snippet:

Drawing Curve

Create Filled Curve Object

This example shows how to add a Curve object that is filled with color.

import aspose.pdf as ap
import aspose.pdf.drawing as drawing


def add_curve_filled(outfile: str):
    document = ap.Document()
    page = document.pages.add()
    graph = drawing.Graph(400, 200)
    graph.border = ap.BorderInfo(ap.BorderSide.ALL, ap.Color.green)

    curve1 = drawing.Curve([10, 10, 50, 60, 70, 10, 100, 120])
    curve1.graph_info.fill_color = ap.Color.green_yellow
    graph.shapes.add(curve1)

    page.paragraphs.add(graph)
    document.save(outfile)

Result of adding a filled curve:

Filled Curve