pdf links

PDF Rendering
Convert PDF to Image (.NET)
Convert PDF to image on Android (Xamarin)
Convert PDF to image on iOS (Xamarin)
Convert PDF to image in Windows Store apps (.NET)
Convert PDF to image in Windows Phone apps (.NET)
PDF to image in Universal Windows Store apps (.NET)
Free PDF Viewer control for Windows Forms (.NET)
How to integrate PDF Viewer control in WPF app (.NET)
Creating WPF PDF Viewer supporting bookmarks (.NET)
Cross-platform PDF Viewer using GTK# (MONO)
Silverlight PDF viewer control (Silverlight 5)
Multithreaded PDF rendering (.NET)
Convert pdf to image in Silverlight app (C# sample)
How to set fallback fonts for PDF rendering (C#)
Avoiding the out-of-memory exception on rendering (C#)
PDF viewer single page application (WebAPI, AngularJS)
PDF viewer control for Windows 10 universal applications
Use custom ICC profile for CMYK to RGB conversion
PDF layers - separate images, text, annotations, graphics

PDF Forms Creation PDF Security
Conversion to PDF/A
Other topics
PDF Document Manipulation PDF Content Generation
Fixed and Flow layout document API (.NET)
Creation of grids and tables in PDF (C# sample)
How to create interactive documents using Actions (C# sample)
Text flow effects in PDF (C# sample)
How to generate ordered and bulleted lists in PDF (C# sample)
Convert HTML to PDF using flow layout API (C# sample)
How to use custom fonts for PDF generation (.NET)
Create document with differently sized pages (C#)
Create PDF documents using MONO (C#/MONO/Windows/OSX)
How to use background images for content elements (C#/PDF Kit/FlowLayout)
Add transparent images to PDF document (C#)
Draw round rect borders in PDF documents(C#)
ICC color profiles and and ICC based colors in PDF (C#)
How to use bidirectional and right to left text in PDF (C#)
Create PDF documents from XML templates (C# sample)
How to resize PDF pages and use custom stamps (C#)
Add header and footer to PDF page (.NET sample)
How to use clipping mask for drawing on PDF page
Fill graphics path with gradient brushes in PDF (Shadings)
Apitron PDF Kit and Rasterizer engine settings
Add layers to PDF page (optional content, C# sample)
How to create free text annotation with custom appearance
PDF Content Extraction PDF Navigation
PDF to TIFF conversion
Contact us if you have a PDF related question and we'll cover it in our blog.

2016-01-07

How to apply clipping mask for drawings on PDF page

Introduction


When you work on PDF export feature for your custom CAD application or just want to take advantage of the advanced PDF drawing capabilities you often need the clipping feature. The PDF supports both clipping paths usage and text-based clipping and in this article we’ll show you how to use them and apply the clipping while drawing on PDF page. (In addition, there are soft mask and image mask features which are described in our book).

Apitron PDF Kit provides a fixed layout API that supports all possible drawing features defined in PDF standard. When you need to add a drawing, you create a ClippedContent object that acts as a canvas for your drawing. Its constructor accepts clipping path object that gets set as the clipping mask for your drawing. If you need to combine the current clipping with another, you create an additional clipping content object and add it inside the current one.

Text clipping works a bit different. You can set any text string as a clipping mask by creating a regular TextObject, setting its rendering mode to the one that affects clipping, and adding it to page’s content. This text then becomes a part of the clipping mask created by intersecting it with the current clipping path.


The code


Code sample, provided below, demonstrates regular clipping as well as text-based clipping.

// demonstrates how to use text string as clipping path
public static ClippedContent DrawContentUsingTextClipping()
{
    ClippedContent clippedContent = new ClippedContent(0,0,200,200);  
         
    // create text object
    TextObject clipText = new TextObject(StandardFonts.HelveticaBold, 30);           
    // set text rendering mode that applies clipping
    clipText.SetTextRenderingMode(RenderingMode.SetAsClipping);
    clipText.AppendText("Text clipping!");
           
    // set current fill color
    clippedContent.SetDeviceNonStrokingColor(RgbColors.Red.Components);

    // position the text
    clippedContent.Translate(0, 20);
    clippedContent.AppendText(clipText);
    clippedContent.Translate(0, -30);

    // draw image through our clipping
    clippedContent.AppendImage("gradient",0,0,200,200);
    return clippedContent;
}

// demonstrates how to use regular path as clipping path
public static ClippedContent DrawContentUsingClippingPath()
{
    // create base clipping path comprising two circles drawn
    // in different directions one inside another
    Path clippingPath = new Path();
    clippingPath.AppendPath(Path.CreateCircle(150, 600, 100));
    clippingPath.AppendPath(Path.CreateCircle(150, 600, 50, false));

    // create clipped content object and set its clipping path,
    // we also set the clipping rule to even-odd to get the
    // donut-shaped clipping area
    ClippedContent clippedContent = new ClippedContent(clippingPath, FillRule.EvenOdd);

    // set current fill color
    clippedContent.SetDeviceNonStrokingColor(RgbColors.Red.Components);
    // draw rectanle through our clipping
    clippedContent.FillPath(Path.CreateRect(50, 500, 300, 200));
    return clippedContent;
}

static void Main(string[] args)
{           
    // create document and register image resource
    FixedDocument doc = new FixedDocument();
    doc.ResourceManager.RegisterResource(new Image("gradient",
         "../../data/gradient.jpg"));      

    // create page and append our clipped contents to it
    Page page = new Page();
    page.Content.AppendContent(DrawContentUsingClippingPath());            
    page.Content.Translate(250,700);
    page.Content.AppendContent(DrawContentUsingTextClipping());

    // append page to document and save it
    doc.Pages.Add(page);
    using (Stream stream = File.Create("clippedContent.pdf"))
    {
        doc.Save(stream); 
    }

    Process.Start("clippedContent.pdf");
}


The result looks as follows (the complete code sample can be found in our github repo):

Pic. 1 Clipping based on graphics path and text string

Pic. 1 Clipping based on graphics path and text string

Here, we created a donut-shaped clipping path using a path based on two circles drawn in opposite directions and setting an even-odd rule for it. Text string becomes a clipping path when we set its text rendering mode to SetAsClipping.

Conclusion


With the Apitron PDF Kit for .NET library you can use advanced PDF graphic features such as clippings, shadings and patterns to create complex drawings demonstrating rich visual effects. Everything that can be drawn using PDF graphics system, can be drawn using fixed layout API provided by our library.

Downloadable version of this article can be found by the following link [PDF].

No comments:

Post a Comment