Clip PDF page content in C#

Shapes
2/28/2014

Downloads

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 step1.png

The combined result:

Clip PDF content.png

C# code sample

1 private static Page ClipContent(Page page, Rectangle eraserRect) 2 { 3 // extract all graphics as shapes 4 ShapeCollection shapes = page.CreateShapes(); 5 6 Rectangle visibleRectangle = page.MediaBox; 7 8 // create a rectangular path that encloses the entire page 9 // (the 'true' argument closes the path) 10 FreeHandPath visibleArea = new FreeHandPath(true); 11 visibleArea.Segments.Add(new FreeHandStartSegment( 12 visibleRectangle.Left, visibleRectangle.Bottom)); 13 visibleArea.Segments.Add(new FreeHandLineSegment( 14 visibleRectangle.Left + visibleRectangle.Width, 15 visibleRectangle.Bottom)); 16 visibleArea.Segments.Add(new FreeHandLineSegment( 17 visibleRectangle.Left + visibleRectangle.Width, 18 visibleRectangle.Bottom + visibleRectangle.Height)); 19 visibleArea.Segments.Add(new FreeHandLineSegment( 20 visibleRectangle.Left, 21 visibleRectangle.Bottom + visibleRectangle.Height)); 22 23 // create a path for the rectangular area that we want to exclude 24 FreeHandPath exludedArea = new FreeHandPath(true); 25 exludedArea.Segments.Add(new FreeHandStartSegment( 26 eraserRect.Left, eraserRect.Bottom)); 27 exludedArea.Segments.Add(new FreeHandLineSegment( 28 eraserRect.Left + eraserRect.Width, 29 eraserRect.Bottom)); 30 exludedArea.Segments.Add(new FreeHandLineSegment( 31 eraserRect.Left + eraserRect.Width, 32 eraserRect.Bottom + eraserRect.Height)); 33 exludedArea.Segments.Add(new FreeHandLineSegment( 34 eraserRect.Left, 35 eraserRect.Bottom + eraserRect.Height)); 36 37 // now combine the two paths (see illustration above) using even-odd fill rule 38 // closed paths inside other paths will alternate from being included to being 39 // excluded and vice versa 40 // this results in the combined path as shown above 41 ClipShape clipShape = new ClipShape(); 42 clipShape.FillRule = FillRule.EvenOdd; 43 clipShape.Paths.Add(visibleArea); 44 clipShape.Paths.Add(exludedArea); 45 46 // insert the clipShape at the very start because the clipping 47 // affects all subsequent shapes in the collection 48 shapes.Insert(0, clipShape); 49 50 // create a new page based on the original page without the graphics (empty) 51 page = page.Clone(PageCloneSettings.NoOriginalGraphics); 52 page.Overlay.Add(shapes); 53 54 return page; 55 }

VB.NET code sample

1 Private Function ClipContent(page As Page, eraserRect As Rectangle) As Page 2 ' extract all graphics as shapes 3 Dim shapes As ShapeCollection = page.CreateShapes() 4 5 Dim visibleRectangle As Rectangle = page.MediaBox 6 7 ' create a rectangular path that encloses the entire page 8 ' (the 'true' argument closes the path) 9 Dim visibleArea As New FreeHandPath(True) 10 visibleArea.Segments.Add(New FreeHandStartSegment(visibleRectangle.Left, visibleRectangle.Bottom)) 11 visibleArea.Segments.Add(New FreeHandLineSegment(visibleRectangle.Left + visibleRectangle.Width, visibleRectangle.Bottom)) 12 visibleArea.Segments.Add(New FreeHandLineSegment(visibleRectangle.Left + visibleRectangle.Width, visibleRectangle.Bottom + visibleRectangle.Height)) 13 visibleArea.Segments.Add(New FreeHandLineSegment(visibleRectangle.Left, visibleRectangle.Bottom + visibleRectangle.Height)) 14 15 ' create a path for the rectangular area that we want to exclude 16 Dim exludedArea As New FreeHandPath(True) 17 exludedArea.Segments.Add(New FreeHandStartSegment(eraserRect.Left, eraserRect.Bottom)) 18 exludedArea.Segments.Add(New FreeHandLineSegment(eraserRect.Left + eraserRect.Width, eraserRect.Bottom)) 19 exludedArea.Segments.Add(New FreeHandLineSegment(eraserRect.Left + eraserRect.Width, eraserRect.Bottom + eraserRect.Height)) 20 exludedArea.Segments.Add(New FreeHandLineSegment(eraserRect.Left, eraserRect.Bottom + eraserRect.Height)) 21 22 ' now combine the two paths (see illustration above) using even-odd fill rule 23 ' closed paths inside other paths will alternate from being included to being 24 ' excluded and vice versa 25 ' this results in the combined path as shown above 26 Dim clipShape As New ClipShape() 27 clipShape.FillRule = FillRule.EvenOdd 28 clipShape.Paths.Add(visibleArea) 29 clipShape.Paths.Add(exludedArea) 30 31 ' insert the clipShape at the very start because the clipping 32 ' affects all subsequent shapes in the collection 33 shapes.Insert(0, clipShape) 34 35 ' create a new page based on the original page without the graphics (empty) 36 page = page.Clone(PageCloneSettings.NoOriginalGraphics) 37 page.Overlay.Add(shapes) 38 39 Return page 40 End Function

The initial page and the result.

result of clippling content of a PDF.png