GdPicture.NET.14
GdPicture14 Namespace / GdPicturePDF Class / ClipPath Method
Example





In This Topic
ClipPath Method (GdPicturePDF)
In This Topic
Modifies the currently specified clipping path by intersecting it with the currently constructed path, defined within a currently selected page of the loaded PDF document. This method uses the nonzero winding number rule to determine which regions lie inside the clipping path. In other words, this method sets the clipping path operator "W". The clipping path is part of the graphics state and thus it is affected by the currently specified graphics state parameters (see PDF Reference, Section "Clipping Path Operators").
Syntax
'Declaration
 
Public Function ClipPath() As GdPictureStatus
public GdPictureStatus ClipPath()
public function ClipPath(): GdPictureStatus; 
public function ClipPath() : GdPictureStatus;
public: GdPictureStatus ClipPath(); 
public:
GdPictureStatus ClipPath(); 

Return Value

A member of the GdPictureStatus enumeration. If the method has been successfully followed, then the return value is GdPictureStatus.OK.

We strongly recommend always checking this status first.

Remarks
This method is only allowed for use with non-encrypted documents.

Just to remind you that the initial clipping path includes the entire page.

For further assistance, please see the Path Construction and Painting section of the GdPicturePDF class in the Reference Guide, as well as the Clipping Path Operators section in the PDF Reference.

Example
How to utilize a clipping path to draw only some parts of your images.
How to draw only some parts of Bézier curves limited by the specified clipping path.
Dim caption As String = "Example: ClipPath"
Dim gdpicturePDF As New GdPicturePDF()
Dim status As GdPictureStatus = gdpicturePDF.NewPDF()
If status = GdPictureStatus.OK Then
    gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft)
    If (gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) = GdPictureStatus.OK) AndAlso
       (gdpicturePDF.AddEllipseToPath(300, 400, 400, 200) = GdPictureStatus.OK) AndAlso
       (gdpicturePDF.SetFillColor(0, 255, 255) = GdPictureStatus.OK) AndAlso
       (gdpicturePDF.FillPath() = GdPictureStatus.OK) AndAlso
       (gdpicturePDF.AddEllipseToPath(300, 400, 400, 200) = GdPictureStatus.OK) AndAlso
       (gdpicturePDF.ClipPath() = GdPictureStatus.OK) AndAlso
       (gdpicturePDF.EndPath() = GdPictureStatus.OK) AndAlso
       (gdpicturePDF.SetFillColor(128, 128, 0) = GdPictureStatus.OK) AndAlso
       (gdpicturePDF.BeginPath(50, 20) = GdPictureStatus.OK) AndAlso
       (gdpicturePDF.AddCurveToPath1(10, 500, 200, 400) = GdPictureStatus.OK) AndAlso
       (gdpicturePDF.FillPath() = GdPictureStatus.OK) AndAlso
       (gdpicturePDF.BeginPath(550, 20) = GdPictureStatus.OK) AndAlso
       (gdpicturePDF.AddCurveToPath2(510, 500, 300, 400) = GdPictureStatus.OK) AndAlso
       (gdpicturePDF.FillPath() = GdPictureStatus.OK) AndAlso
       (gdpicturePDF.BeginPath(50, 750) = GdPictureStatus.OK) AndAlso
       (gdpicturePDF.AddCurveToPath3(10, 550, 300, 480, 500, 600) = GdPictureStatus.OK) AndAlso
       (gdpicturePDF.FillPath() = GdPictureStatus.OK) Then
        status = gdpicturePDF.SaveToFile("test_ClipPath1.pdf")
        If status = GdPictureStatus.OK Then
            MessageBox.Show("The example has been followed successfully and the file has been saved.", caption)
        Else
            MessageBox.Show("The example has been followed successfully, but the file can't be saved. Status: " + status.ToString(), caption)
        End If
    Else
        MessageBox.Show("The example has not been followed successfully." + vbCrLf + "The last known status is " + gdpicturePDF.GetStat().ToString(), caption)
    End If
Else
    MessageBox.Show("The NewPDF() method has failed with the status: " + status.ToString(), caption)
End If
gdpicturePDF.Dispose()
string caption = "Example: ClipPath";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
GdPictureStatus status = gdpicturePDF.NewPDF();
if (status == GdPictureStatus.OK)
{
    gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft);
    if ((gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) == GdPictureStatus.OK) &&
        (gdpicturePDF.AddEllipseToPath(300, 400, 400, 200) == GdPictureStatus.OK) &&
        (gdpicturePDF.SetFillColor(0, 255, 255) == GdPictureStatus.OK) &&
        (gdpicturePDF.FillPath() == GdPictureStatus.OK) &&
        (gdpicturePDF.AddEllipseToPath(300, 400, 400, 200) == GdPictureStatus.OK) &&
        (gdpicturePDF.ClipPath() == GdPictureStatus.OK) &&
        (gdpicturePDF.EndPath() == GdPictureStatus.OK) &&
        (gdpicturePDF.SetFillColor(128, 128, 0) == GdPictureStatus.OK) &&
        (gdpicturePDF.BeginPath(50, 20) == GdPictureStatus.OK) &&
        (gdpicturePDF.AddCurveToPath1(10, 500, 200, 400) == GdPictureStatus.OK) &&
        (gdpicturePDF.FillPath() == GdPictureStatus.OK) &&
        (gdpicturePDF.BeginPath(550, 20) == GdPictureStatus.OK) &&
        (gdpicturePDF.AddCurveToPath2(510, 500, 300, 400) == GdPictureStatus.OK) &&
        (gdpicturePDF.FillPath() == GdPictureStatus.OK) &&
        (gdpicturePDF.BeginPath(50, 750) == GdPictureStatus.OK) &&
        (gdpicturePDF.AddCurveToPath3(10, 550, 300, 480, 500, 600) == GdPictureStatus.OK) &&
        (gdpicturePDF.FillPath() == GdPictureStatus.OK))
    {
        status = gdpicturePDF.SaveToFile("test_ClipPath1.pdf");
        if (status == GdPictureStatus.OK)
            MessageBox.Show("The example has been followed successfully and the file has been saved.", caption);
        else
            MessageBox.Show("The example has been followed successfully, but the file can't be saved. Status: " + status.ToString(), caption);
    }
    else
        MessageBox.Show("The example has not been followed successfully.\nThe last known status is " + gdpicturePDF.GetStat().ToString(), caption);
}
else
    MessageBox.Show("The NewPDF() method has failed with the status: " + status.ToString(), caption);
gdpicturePDF.Dispose();
How to draw only a part of your image into a clipping path defined by the shape of an ellipse.
Dim caption As String = "Example: ClipPath"
Dim gdpicturePDF As New GdPicturePDF()
If (gdpicturePDF.NewPDF() = GdPictureStatus.OK) AndAlso
   (gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) = GdPictureStatus.OK) Then
    Dim image_name As String = gdpicturePDF.AddJpegImageFromFile("image.jpg")
    If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
        Dim width As Single = gdpicturePDF.GetPageWidth()
        If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
            Dim height As Single = gdpicturePDF.GetPageHeight()
            If gdpicturePDF.GetStat() = GdPictureStatus.OK Then
                'the new height of the destination area
                height = height / 2
                If (gdpicturePDF.SelectPage(1) = GdPictureStatus.OK) AndAlso
                   (gdpicturePDF.DrawImage(image_name, 0, height, width, height) = GdPictureStatus.OK) AndAlso
                   (gdpicturePDF.BeginPath(0, 0) = GdPictureStatus.OK) AndAlso
                   (gdpicturePDF.AddEllipseToPath(width / 2, height / 2, width / 2, height / 2) = GdPictureStatus.OK) AndAlso
                   (gdpicturePDF.ClipPath() = GdPictureStatus.OK) AndAlso
                   (gdpicturePDF.DrawImage(image_name, 0, 0, width, height) = GdPictureStatus.OK) Then
                    Dim status As GdPictureStatus = gdpicturePDF.SaveToFile("test_ClipPath2.pdf")
                    If status = GdPictureStatus.OK Then
                        MessageBox.Show("The example has been followed successfully and the file has been saved.", caption)
                    Else
                        MessageBox.Show("The example has been followed successfully, but the file can't be saved. Status: " + status.ToString(), caption)
                    End If
                Else
                    MessageBox.Show("The example has not been followed successfully." + vbCrLf + "The last known status is " + gdpicturePDF.GetStat().ToString(), caption)
                End If
            Else
                MessageBox.Show("The GetPageHeight() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
            End If
        Else
            MessageBox.Show("The GetPageWidth() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
        End If
    Else
        MessageBox.Show("The AddJpegImageFromFile() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
    End If
Else
    MessageBox.Show("The NewPDF() method or the NewPage() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption)
End If
gdpicturePDF.Dispose()
string caption = "Example: ClipPath";
GdPicturePDF gdpicturePDF = new GdPicturePDF();
if ((gdpicturePDF.NewPDF() == GdPictureStatus.OK) &&
    (gdpicturePDF.NewPage(PdfPageSizes.PdfPageSizeA4) == GdPictureStatus.OK))
{
    string image_name = gdpicturePDF.AddJpegImageFromFile("image.jpg");
    if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
    {
        float width = gdpicturePDF.GetPageWidth();
        if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
        {
            float height = gdpicturePDF.GetPageHeight();
            if (gdpicturePDF.GetStat() == GdPictureStatus.OK)
            {
                //the new height of the destination area
                height = height / 2;
                if ((gdpicturePDF.SelectPage(1) == GdPictureStatus.OK) &&
                    (gdpicturePDF.DrawImage(image_name, 0, height, width, height) == GdPictureStatus.OK) &&
                    (gdpicturePDF.BeginPath(0, 0) == GdPictureStatus.OK) &&
                    (gdpicturePDF.AddEllipseToPath(width / 2, height / 2, width / 2, height / 2) == GdPictureStatus.OK) &&
                    (gdpicturePDF.ClipPath() == GdPictureStatus.OK) &&
                    (gdpicturePDF.DrawImage(image_name, 0, 0, width, height) == GdPictureStatus.OK))
                {
                    GdPictureStatus status = gdpicturePDF.SaveToFile("test_ClipPath2.pdf");
                    if (status == GdPictureStatus.OK)
                        MessageBox.Show("The example has been followed successfully and the file has been saved.", caption);
                    else
                        MessageBox.Show("The example has been followed successfully, but the file can't be saved. Status: " + status.ToString(), caption);
                }
                else
                    MessageBox.Show("The example has not been followed successfully.\nThe last known status is " + gdpicturePDF.GetStat().ToString(), caption);
            }
            else
                MessageBox.Show("The GetPageHeight() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
        }
        else
            MessageBox.Show("The GetPageWidth() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
    }
    else
        MessageBox.Show("The AddJpegImageFromFile() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
}
else
    MessageBox.Show("The NewPDF() method or the NewPage() method has failed with the status: " + gdpicturePDF.GetStat().ToString(), caption);
gdpicturePDF.Dispose();
See Also