Clip PDF page content in C#

Clip PDF page content in C#

This sample demonstrates how to mask out a rectangular area on a PDF page using a clip shape in C#. This will not erase the actual graphics. Masked out text will still be selectable for example.

Clip shape is an object that masks out graphics so that only the content inside the clip shape is visible. In other words, anything outside the clip shape will not be visible. In this sample we will create a clip shape that preserves the whole page except a specified rectangular area. To achieve that effect, we will combine two rectangular paths using the even-odd fill rule like this:

Two paths:

Clipping A PDF Step1The combined result:

Clip PDF Content

private static Page ClipContent(Page page, Rectangle eraserRect)
{
    // extract all graphics as shapes
    ShapeCollection shapes = page.CreateShapes();

    Rectangle visibleRectangle = page.MediaBox;

    // create a rectangular path that encloses the entire page
    // (the 'true' argument closes the path)
    FreeHandPath visibleArea = new FreeHandPath(true);
    visibleArea.Segments.Add(new FreeHandStartSegment(
        visibleRectangle.Left, visibleRectangle.Bottom));
    visibleArea.Segments.Add(new FreeHandLineSegment(
        visibleRectangle.Left + visibleRectangle.Width,
        visibleRectangle.Bottom));
    visibleArea.Segments.Add(new FreeHandLineSegment(
        visibleRectangle.Left + visibleRectangle.Width,
        visibleRectangle.Bottom + visibleRectangle.Height));
    visibleArea.Segments.Add(new FreeHandLineSegment(
        visibleRectangle.Left,
        visibleRectangle.Bottom + visibleRectangle.Height));

    // create a path for the rectangular area that we want to exclude
    FreeHandPath exludedArea = new FreeHandPath(true);
    exludedArea.Segments.Add(new FreeHandStartSegment(
        eraserRect.Left, eraserRect.Bottom));
    exludedArea.Segments.Add(new FreeHandLineSegment(
        eraserRect.Left + eraserRect.Width,
        eraserRect.Bottom));
    exludedArea.Segments.Add(new FreeHandLineSegment(
        eraserRect.Left + eraserRect.Width,
        eraserRect.Bottom + eraserRect.Height));
    exludedArea.Segments.Add(new FreeHandLineSegment(
        eraserRect.Left,
        eraserRect.Bottom + eraserRect.Height));

    // now combine the two paths (see illustration above) using even-odd fill rule
    // closed paths inside other paths will alternate from being included to being 
    // excluded and vice versa
    // this results in the combined path as shown above
    ClipShape clipShape = new ClipShape();
    clipShape.FillRule = FillRule.EvenOdd;
    clipShape.Paths.Add(visibleArea);
    clipShape.Paths.Add(exludedArea);

    // insert the clipShape at the very start because the clipping
    // affects all subsequent shapes in the collection
    shapes.Insert(0, clipShape);

    // create a new page based on the original page without the graphics (empty)
    page = page.Clone(PageCloneSettings.NoOriginalGraphics);
    page.Overlay.Add(shapes);

    return page;
}
Private Function ClipContent(page As Page, eraserRect As Rectangle) As Page
    ' extract all graphics as shapes
    Dim shapes As ShapeCollection = page.CreateShapes()

    Dim visibleRectangle As Rectangle = page.MediaBox

    ' create a rectangular path that encloses the entire page
    ' (the 'true' argument closes the path)
    Dim visibleArea As New FreeHandPath(True)
    visibleArea.Segments.Add(New FreeHandStartSegment(visibleRectangle.Left, visibleRectangle.Bottom))
    visibleArea.Segments.Add(New FreeHandLineSegment(visibleRectangle.Left + visibleRectangle.Width, visibleRectangle.Bottom))
    visibleArea.Segments.Add(New FreeHandLineSegment(visibleRectangle.Left + visibleRectangle.Width, visibleRectangle.Bottom + visibleRectangle.Height))
    visibleArea.Segments.Add(New FreeHandLineSegment(visibleRectangle.Left, visibleRectangle.Bottom + visibleRectangle.Height))

    ' create a path for the rectangular area that we want to exclude
    Dim exludedArea As New FreeHandPath(True)
    exludedArea.Segments.Add(New FreeHandStartSegment(eraserRect.Left, eraserRect.Bottom))
    exludedArea.Segments.Add(New FreeHandLineSegment(eraserRect.Left + eraserRect.Width, eraserRect.Bottom))
    exludedArea.Segments.Add(New FreeHandLineSegment(eraserRect.Left + eraserRect.Width, eraserRect.Bottom + eraserRect.Height))
    exludedArea.Segments.Add(New FreeHandLineSegment(eraserRect.Left, eraserRect.Bottom + eraserRect.Height))

    ' now combine the two paths (see illustration above) using even-odd fill rule
    ' closed paths inside other paths will alternate from being included to being 
    ' excluded and vice versa
    ' this results in the combined path as shown above
    Dim clipShape As New ClipShape()
    clipShape.FillRule = FillRule.EvenOdd
    clipShape.Paths.Add(visibleArea)
    clipShape.Paths.Add(exludedArea)

    ' insert the clipShape at the very start because the clipping
    ' affects all subsequent shapes in the collection
    shapes.Insert(0, clipShape)

    ' create a new page based on the original page without the graphics (empty)
    page = page.Clone(PageCloneSettings.NoOriginalGraphics)
    page.Overlay.Add(shapes)

    Return page
End Function

The initial page and the result.

Result Of Clippling Content Of A PDF