Add Curve Object to PDF file

The following code snippet also work with Aspose.PDF.Drawing library.

Add Curve object

A graph Curve is a connected union of projective lines, each line meeting three others in ordinary double points.

Aspose.PDF for .NET shows how to use Bézier curves in your Graphs. Bézier curves are widely used in computer graphics to model smooth curves. The curve is completely contained in the convex hull of its control points, the points may be graphically displayed and used to manipulate the curve intuitively. The entire curve is contained in the quadrilateral whose corners are the four given points (their convex hull).

In this article, we will investigate simply graph curves, and filled curves, that you can create in your PDF document.

Follow the steps below:

  1. Create Document instance.
  2. Create Drawing object with certain dimensions.
  3. Set Border for Drawing object.
  4. Add Graph object to paragraphs collection of page.
  5. Save our PDF file.
private static void ExampleCurve()
{
    // The path to the document directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();

    // Create Document instance using 'using' block to ensure proper disposal
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page to pages collection of PDF file
        var page = document.Pages.Add();

        // Create Drawing object with certain dimensions
        var graph = new Aspose.Pdf.Drawing.Graph(400, 200);

        // Set border for Drawing object
        var borderInfo = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Green);
        graph.Border = borderInfo;

        // Create a curve and set its properties
        var curve1 = new Aspose.Pdf.Drawing.Curve(new float[] { 10, 10, 50, 60, 70, 10, 100, 120 })
        {
            GraphInfo = 
            { 
                Color = Aspose.Pdf.Color.GreenYellow 
            }
        };

        // Add the curve to the graph shapes
        graph.Shapes.Add(curve1);

        // Add Graph object to paragraphs collection of page
        page.Paragraphs.Add(graph);

        // Save PDF file
        document.Save(dataDir + "DrawingCurve1_out.pdf");
    }
}

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.

private static void CurveFilled()
{
    // The path to the document directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();

    // Create Document instance using 'using' block to ensure proper disposal
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page to pages collection of PDF file
        var page = document.Pages.Add();

        // Create Drawing object with certain dimensions
        var graph = new Aspose.Pdf.Drawing.Graph(400, 200);

        // Set border for Drawing object
        var borderInfo = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, Aspose.Pdf.Color.Green);
        graph.Border = borderInfo;

        // Create a curve and set fill color
        var curve1 = new Aspose.Pdf.Drawing.Curve(new float[] { 10, 10, 50, 60, 70, 10, 100, 120 })
        {
            GraphInfo = 
            { 
                FillColor = Aspose.Pdf.Color.GreenYellow 
            }
        };

        // Add the curve to the graph shapes
        graph.Shapes.Add(curve1);

        // Add Graph object to paragraphs collection of page
        page.Paragraphs.Add(graph);

        // Save PDF file
        document.Save(dataDir + "DrawingCurve2_out.pdf");
    }
}

Look at the result of adding a filled Curve:

Filled Curve