Let’s ignore images for a second. The basic building block of the FixedDocument is the TextBlock , the issue is, there are two levels between them, and each of the levels decide a different property of the page. Let’s go from the top down. Let’s take a look at the FixedDocument class first. We want to initialize a FixedDocument object then assign properties like page size, then add PageContent to the Document. The PageContent object is simply a container for the purposes of this article. In the PageContent we will add our FixedPage object(s) that represent our individual page(s) in the overall document. Finally at the bottom of the hierarchy is the lowly TextBlock object, which defines things like font size, font family, color, margins, border, line spacing, etc. The document hierarchy is presented below.

Fixed Document Hierarchy

Despite some of this looking kind of irregular, our process is this:

Initialize the FixedDocument Set the page size for the overall document. Initialize a FixedPage object. Set the FixedPage width and height. Initialize our TextBlock 's and set their text, margin, font size, font color, and font family. Add TextBlock to the FixedPage Add FixedPage to PageContent Add PageContent to FixedDocument Send the FixedDocument to the printer.

Let’s dive into the TextBlock first. It’s the most “atomic” of the data types we are using. Its properties that we should find important are TextBlock.Text which contains the textual content to be displayed or printed. We also have a list of properties to control our font such as TextBlock.FontFamily , TextBlock.Margin, TextBlock.Padding,TextBlock.FontSize , TextBlock.FontStretch , TextBlock.FontStyle , and TextBlock.FontWeight . Just from looking at these properties, we can tell what each one does intuitively, but for clarity, I will map these out. First the Font Family defines the font that should be used in print or display. This is the font name we’re all used to. (Times New Roman, Arial, etc.) Font Size is obviously how big the font should be in display or print. Stretch, Style, and Weight are a little more nebulous for those that aren’t familiar with type and layout at all. Stretch refers to how far apart each character in the string is from each other when displayed. Style refers to if it’s normal, italic, or bold, and it defaults to normal. Weight refers to how thick the line of the character is in display. It can be used to make things bolder or lighter depending on the setting. (font weight choices aren’t covered here.) Margin and Padding refer to the space between the container and other content for margin, and the space between the container and the content inside the object. (Just like in HTML/CSS)

So let’s get practical with this. We initialize our TextBlock , set its Text property, set the font, size, margins, and padding. Then we add it to our fixed page. Let’s see what the code for that requires.

TextBlock blk = new TextBlock();

blk.Text = "This is the text that will display";

blk.FontFamily = new FontFamily("FontName");

// a FontFamily object that we pass the string name of the font

// like "Arial".

blk.FontSize = 12; //simply an integer.

blk.FontStretch = FontStretches.Expanded;

// Font Stretch is an Enumerated Type, so we need to

// initialize it from the Enumerated Type FontStretches

// Enumerated Types are those that have predefined values

blk.FontStyle = FontStyles.Italic; // Another Enumerated Type blk.FontWeight = FontWeights.Bold; //Another Enumerated Type

blk.TextWrapping = TextWrapping.Wrap // Another Enumerated Type

blk.Margin = new Thickness(5,10,5,10)

// A thickness object defines how large the margin or padding should // be. One can set uniform thickness by passing a single Float

// value, or optionally set each side of the box manually like

// above. The parameters are new Thickness(left, top, right, bottom)

// This is very similar to the CSS margin property (EX. margin: 1em // 2em 1em 2em)

blk.Padding = new Thickness(2);

//setting all padding to 2 on all sides.