Among the classes that let me make groups of objects and apply common properties to them (TransformationGroup, TransparencyGroup, etc.) I could not find one that can "clip" its child contents, stopping them from drawing outside a given rectangular area.
I imagined it would be something like ClipGroup, and would give analogous capability to IntersectClipRect in Win32, or to HTML5 Canvas's "clip" method. That is, it should interact predictably with coordinate transformations: rotate, scale, translate, then clip, then more transformations, then draw content, then clip again, then more content, etc. The coordinates of the clip region should be affect by outer transformations just like anything else.
I did find I could get the effect I wanted, but it seems like a hack, or at least very long-winded and probably wasteful. Also it is slightly inaccurate (edges don't quite line-up, etc). Is there a better way?
My trick is to "buffer" the content that needs to be clipped in separate single-page PDF document, save it to a temporary file, then reload it as an ImportedPageArea. I create the single-page PDF so that it has page dimensions the size of the desired clip rectangle. As a result when it is drawn on the "real" page by ImportedPageArea, it is clipped. Detailed walkthrough:
I define an AreaGroup:
var outerArea = new AreaGroup(0, 0);
realPage.Add(outerArea);
This is just to provide a place for me to insert the finished content later on when it's ready (so I don't have to do things in a different sequence).
Then I create a TransformationGroup:
var innerArea = new TransformationGroup(0, 0, 0, 0)
{ X = -rect.Left, Y = -rect.Top }
Note that 'rect' defines where I want to clip to. So if top-left of rect is at (100, 0), then any output with x < 100 will be drawing outside the visible area and will thus be clipped.
Then I add all my to-be-clipped elements to innerArea. Note that in here I can use further transformations, etc. or even apply this clipping technique recursively.
After all inner content has been built up, I "clean up" as follows: I define a single-page document such that the page has the width/height of my clip region:
var innerDoc = new Document();
var innerPage = new Page(rect.Width, rect.Height);
innerPage.Dimensions.SetMargins(0, 0);
innerDoc.Pages.Add(innerPage);
Then I add the innerArea to it (with all the content to be clipped):
innerPage.Elements.Add(innerArea);
Then I save the document:
var innerTempFilePath = AllocTempFile();
innerDoc.Draw(innerTempFilePath);
Then I import the inner PDF:
var importedPage = new ImportedPageContents(
innerTempFilePath, 1, PageBoundary.TrimBox);
Finally I can place it as an element on the outer document, at the right offset, by adding it to the outerArea I created at the beginning:
var pageData = new ImportedPageArea(
importedPage, rect.Left, rect.Top);
outerArea.Add(pageData);
Note that if I zoom in on the resulting PDF I can actually see a slight inaccuracy where the clipped content doesn't quite line up with the outer content. Is there something I'm getting wrong in the above approach that would cause the nested content to be positioned incorrectly?
Applying clipping to group of page elements
Re: Applying clipping to group of page elements
Support · Dec 17, 2013Hello,
It is not possible to clip the contents within the AreaGroup, TransformationGroup or TransparencyGroup page elements.
Please send over the following details to [^a|mailto:support@cete.com|opennewwindow|support@cete.com^] so we can look into it further.
1. Sample code that we can execute on our end to recreate the issue
2. Output PDF that contains the inaccuracy.
3. Screenshot of the inaccuracy you are noticing.
Thanks,
ceTe Software Support Team.