Background Color:
Background Pattern:
Home Recent Changes Show All Pages

Writing your own components


Scryber is extensible in may ways and there are templates, data-bindings and components that can be combined to create many different views in a PDF and you can always revert to coding the components after parsing, but sometimes the best way is to create your own standard component.

If you have not read An Introduction to the XML Parser, read it now before moving on.

MyCurrentDate class

As an example, we can create a new component called 'CurrentDate' that will spit out the name of the current date when the file was generated and allow the option for different formatting of the date string

First task is to create a new .NET 3.5 class library, lets call it MyPDFComponents, and add a class called MyCurrentDate that inherits from the PDFTextBase class in the referenced scryber components and common assembly. Within there we will need a string property to hold the format and that will also set our base text to the correct value.

using System;
using Scryber;
using Scryber.Components;

namespace MyPDFComponents
    public class MyCurrentDate : Scryber.Components.PDFTextBase
        private string _format;

        public string DateFormat
            get { return _format; }
                _format = value;
                //Set the text for this component
                if (string.IsNullOrEmpty(value))
                    this.BaseText = DateTime.Now.ToShortDateString();
                    this.BaseText = String.Format(value, DateTime.Now);

        public MyCurrentDate()
            : base((PDFObjectType)"MYCD")
                  this.DateFormat = string.Empty;


There are couple of things to note here. 1 we have a parameterless constructor where we pass a PDFObjectType instance (cast from a string) to the base constructor for ease of identification, and 2 we initialize the DateFormat in the constructor so that a value is always set.

We now have a fully capable PDF component that can be used in the content of any pdf document.
Let's try it out.

Adding our component to a document

If you do not have an executable or web project in your current solution, then add a new project and follow the how to CodeProject Rss feed if you need assistance in getting a pdfx document template set up and rendering.

Once you do have everything set up, then we can add a reference to our date component.
Firstly let's reference the MyPDFComponents project and then we can add it to a new or existing pdfx document template.
In this file declare a new namespace pointing to our components assembly and namespace with a prefix of my.
Finally declare an element in the page content to our component <my:CurrrentDate /> so the final document looks like that below.

<?xml version="1.0" encoding="utf-8" ?>
<pdf:Document xmlns:pdf="Scryber.Components, Scryber.Components, Version=, Culture=neutral, PublicKeyToken=872cbeb81db952fe"
              xmlns:styles="Scryber.Styles, Scryber.Styles, Version=, Culture=neutral, PublicKeyToken=872cbeb81db952fe"
              xmlns:data="Scryber.Data, Scryber.Components, Version=, Culture=neutral, PublicKeyToken=872cbeb81db952fe"
              xmlns:my="MyPDFComponents, MyPDFComponents">
    <pdf:Page >
        <my:CurrentDate />

As a check, let's generate the document and see what is produced

Now we can see our component having text generated and put on the page. Because we are inheriting from PDFVisualComponent (via PDFTextBase - see Component Basics), we also get the entire style capability and also full flowing of text, so let's take advantage of this and add some style to the component output and wrap it in a Div component.

        <pdf:Div styles:margins="80pt" styles:bg-color="#FFFFCC" styles:padding="10pt" styles:h-align="Center"
                 styles:font-family="Times" styles:font-size="40pt" styles:fill-color="#CC0000" >
         Generated on <my:CurrentDate format="{0:dd MMMM yyyy}" styles:fill-color="#00CCCC" />

A quick check of the output and we can see all these styles and positioning coming through onto our generated component. No extra code to write, or layout to perform.

The only thing it needs now is an xsd schema to give anyone who uses your date component a fighting chance of correctly adding it to the page. We'll leave that to you.

Rate This Page: Poor Great   |  Rate Content |
Average rating:  4   
Number of Ratings : 1
Add Comment
No Comments Yet