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.


How to add layers to PDF page using optional content


While working on exporting the PDF document, sometimes you need many versions of the same content to be on one page along with an option to show only one version at once.

A multilanguage report or manual are perfect examples of such documents. Instead of producing a separate file for each language you could create a single file which would contain all the necessary information. A user would be able to switch content versions with a single click by selecting the appropriate layer.

Another example of layered content structure is an engineering drawing or complex schema composed of different logically separated parts which could be made visible or invisible on demand.
All these things are made possible using PDF feature called optional content - see the section “8.11 Optional Content” of the PDF specification for the details. The Apitron PDF Kit .NET component provides an API for layers manipulation and creation. Using this product you can easily create layered content in your PDF documents.

In general, the creation of the multiple layers on PDF page looks as follows:

1. Create several OptionalContentGroup objects and register them as document resources – these objects represent layer identifiers in PDF.

2. Create the OptionalContentConfiguration object, set its properties controlling the behavior and visual layer structure shown in reader’s UI. This object combines layers together and you can use it to define initially visible layers, locked layers, layers that should work as radio buttons etc. You can also define the visual tree structure – parent layer nodes and child nodes.

3. Create and initialize the OptionalContentProperties object required by the FixedDocument object – this object is used to define the default configuration to be used by the PDF reader to show layers, and to specify the list of layers (OptionalContentGroups resource ids) actually referenced in document’s content (cause not all registered layer ids may be in use).

4. Use ClippedContent objects to define the layers and assign their OptionalContentID property to the one of the registered layer ids (Optional Content Group resource IDs). Put these objects on PDF page using Page.Content.AppendContent(…) method.

The code demonstrating these steps can be found in the next section.

The code

class Program
    static void Main(string[] args)
        using (Stream stream = File.Create("manual.pdf"))
            // create our PDF document
            using (FixedDocument doc = new FixedDocument())
                // turn on the layers panel when opened
                doc.PageMode = PageMode.UseOC;

                // register image resource
                    new Apitron.PDF.Kit.FixedLayout.Resources.XObjects.Image(

                // FIRST STEP: create layer definitions,
                // they should be registered as document resources
                OptionalContentGroup group0 = new OptionalContentGroup("group0",
                    "Page layers"IntentName.View);                   

                OptionalContentGroup group1 = new OptionalContentGroup("group1"
                    "Chair image"IntentName.View);

                OptionalContentGroup group2 = new OptionalContentGroup("English", "English",

                OptionalContentGroup group3 = new OptionalContentGroup("Dansk", "Dansk",

                OptionalContentGroup group4 = new OptionalContentGroup("Deutch", "Deutch",

                OptionalContentGroup group5 = new OptionalContentGroup("Русский", "Русский",

                OptionalContentGroup group6 = new OptionalContentGroup("Nederlands"
                    "Nederlands", IntentName.View);

                OptionalContentGroup group7 = new OptionalContentGroup("Français"
                    "Français", IntentName.View);

                OptionalContentGroup group8 = new OptionalContentGroup("Italiano"
                    "Italiano", IntentName.View);

                // SECOND STEP:
                // create the configuration, 
                // it allows to combine the layers together in any order     
                // Default configuration:            
                OptionalContentConfiguration config = new OptionalContentConfiguration(
                // add groups to lists which define the rules controlling 
                // their visibility                  
                // ON groups
                // OFF groups              

                // lock the image layer

                // make other layers working as radio buttons
                // only one translation will be visible at time
                config.RadioButtonGroups.Add(new[] { group2, group3, group4, group5, 
                    group6, group7, group8 });                    

                // show only groups referenced by visible pages
                config.ListMode = ListMode.VisiblePages;
                // initialize the states for all content groups
                // for the default configuration it should be on
                config.BaseState = OptionalContentGroupState.On;
                // set the name of the presentation tree
                config.Order.Name = "Default config";      
                // create a root node + sub elements            
                config.Order.Entries.Add(new OptionalContentGroupTree(group1, group2, 
                    group3, group4, group5, group6, group7, group8));

                // FINAL step:
                // assign the configuration properties to document
                // all configurations and groups should be specified
                doc.OCProperties = new OptionalContentProperties(config, new
                    OptionalContentConfiguration[] {}, new[] { group0, group1, group2, 
                    group3, group4, group5, group6, group7, group8 });

                // create page and assing top layer id to its content
                // it will allow you to completely hide page's
                // content using the configuration we have created                   
                Page page = new Page();
                page.Content.OptionalContentID = "group0";

                // create image layer
                ClippedContent imageBlock = new ClippedContent(0, 0, 245, 300);
                // set the layer id
                imageBlock.OptionalContentID = "group1";
                imageBlock.AppendImage("chair", 0, 0, 245, 300);

                // put the layer on page
                page.Content.Translate(0, 530);

                // append text layers
                // add the page to the document and save it



    static void AppendTextLayers(Page page)
        page.Content.Translate(250, 325);

        // evaluate each property of a resource dictionary and add text to the PDF page
        foreach (PropertyInfo info in typeof(strings).GetRuntimeProperties())
            if (info.PropertyType == typeof(string))
                ClippedContent textContent = new ClippedContent(0, 0, 300, 500);
                // assign layer id
                textContent.OptionalContentID = info.Name;
                textContent.Translate(0, 0);

                // preprocess parsed elements and set additional properties
                // for better visual appearance
                IEnumerable<ContentElement> elements =
                foreach (Br lineBreak in elements.OfType<Br>())
                    lineBreak.Height = 10;  

                foreach (Section subSection in elements.OfType<Section>())
                    subSection.Font = 
                    new Apitron.PDF.Kit.Styles.Text.Font("HelveticaBold", 14);

                // draw text
                textContent.AppendContentElement(new Section(elements), 300, 500);
                // put the text layer on page


You can see that we used content elements from FlowLayout API to prepare translated text blocks, more information about the Fixed and Flow layout API can be found by this link.

We exactly followed the algorithm described in the Introduction section:

1.  Created layer identifier resources and registered them

2. Created default layers configuration in a form of tree view and configured layers to work as radio buttons, except the image layer which we marked as locked to demonstrate this feature

3. Let the document know about the created configuration and layers used

4. Marked all layers with corresponding registered layer ids

5.  Saved the PDF document

The complete code sample can be downloaded from our github repo (link).

Resulting PDF document looks as follows:

Pic. 1 Multilanguage PDF document with layers

Pic. 1 Multilanguage PDF document with layers

You see the locked layer containing chair image and language layers available for viewing. These language layers work as radio buttons group, when one is turned on others go off.


The Apitron PDF Kit for .NET is a powerful tool for creation and manipulation of PDF and PDF/A documents. It’s cross-platform and can be used to create .NET, Mono and Xamarin applications for Windows, iOS, Android and other operation systems. You can read more about the library on the product page. Contact us if you have any questions and we’ll be glad to assist you.

No comments:

Post a Comment