ARTICLE UNDER CONSTRUCTION
Shading & fill format
Version 6.1.0
In this article
This article describes, how the background color is changed using a shading or fill format.
Introduction
Depending on the object type, a Shading or a FillFormat object is used to manage the background color.
A shading can be set at the Shading property in ParagraphFormat objects (see Paragraph) as well as in Table, Column, Row and Cell objects (see Contents / Tables).
A fill format can be set at the FillFormat property in objects derived from Shape. This includes TextFrame (see Contents / TextFrames) and Image (see Contents / Images) objects. Also, Chart objects and parts of it like TextArea, PlotArea, Series and Point objects provide that property (see Contents / Charts).
The Shading and FillFormat classes provide quite the same functionality and are therefore introduced together in this article.
Setting the background color
To set the background color of an object, you only have to set the Color property of its Shading or FillFormat object.
For objects using shadings, like ParagraphFormat, it’s done like this:
// Set the Shading color.
format.Shading.Color = Colors.Aqua;
For objects using fill formats, like TextFrame, it’s done like this:
// Set the FillFormat color.
textFrame.FillFormat.Color = Colors.Aqua;
See full SimpleExample sample in PDFsharp.Sample repository. Show resulting PDF
Color is a property of the type Color (see Colors for more information).
Visibility
As long as the Visible property is not set to false, the shading or fill format will be rendered, if the Color property is set. Due to this you usually don’t have to set Visible to true.
When it comes to inheritance (see Document / Formatting for more information), you may want an object not to inherit the shading or fill format of another one. In that case, you can simply set the Visible property to false for the object that inherits the background color:
// Set shading for all cells.
table.Shading.Color = Colors.Aqua;
…
// Set shading visibility to false for cellA2.
cellA2.Shading.Visible = false;
See full Visibility sample in PDFsharp.Sample repository. Show resulting PDF
Color is a property of the type Color (see Colors for more information).
Nesting
As a document is a tree of objects, you can nest objects with different shadings or fill formats. Especially when working with tables or text frames you should keep in mind that the shading you set on a ParagraphFormat instance, for example directly on the table or text frame, defines not the background of the table or text frame itself. As the name states, the ParagraphFormat class is used to format paragraphs. So, the shading set on the ParagraphFormat instance is inherited to the contained paragraphs. The table, however, has its own shading and the text frame its own fill format.
The following example demonstrates the difference:
// Set cell shading for cellA1.
cellA1.Shading.Color = Colors.Aqua;
// Set paragraph shading for cellA1.
cellA1.Format.Shading.Color = Colors.CornflowerBlue;
// Add paragraph to cellA1.
cellA1.AddParagraph("Text A1");
See full Nesting sample in PDFsharp.Sample repository. Show resulting PDF
Color is a property of the type Color (see Colors for more information).
Shading in tables
If you want to color an area inside of a table, you may use the SetShading method of the Table class. With this function, you just have to pass the indexes of the start column and row, the count of the columns and rows and the color:
// Set a shading for B2 to C3.
table.SetShading(1, 1, 2, 2, Colors.Cyan);
See full SetShadingMethod sample in PDFsharp.Sample repository. Show resulting PDF