Borders are represented by the Borders collection. This is a collection of Border objects that are accessed by index or by border type. Border type is represented by the BorderType enumeration. Some values of the enumeration are applicable to several or only one document element. For example, BorderType.Bottom is applicable to a paragraph or table cell while BorderType.DiagonalDown specifies the diagonal border in a table cell only.
Both the border collection and each separate border have similar attributes like color, line style, line width, distance from text, and optional shadow. They are represented by properties of the same name. You can achieve different border types by combining the property values. In addition, both Borders and Border objects allow you to reset these values to default by calling the ClearFormatting method. Note that when border properties are reset to default values, the border is invisible.
The Shading class contains shading attributes for document elements. You can set the desired shading texture and the colors that are applied to the background and foreground of the element.
The shading texture is set with a TextureIndex enumeration value that allows the application of various patterns to the Shading object. For example, to set a background color for a document element, use the TextureIndex.TextureSolid value and set the foreground shading color as appropriate.
Example DocumentBuildeApplyBordersAndShading
Shows how to apply borders and shading to a paragraph.
[C#]
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Set paragraph borders
BorderCollection borders = builder.ParagraphFormat.Borders;
borders.DistanceFromText = 20;
borders[BorderType.Left].LineStyle = LineStyle.Double;
borders[BorderType.Right].LineStyle = LineStyle.Double;
borders[BorderType.Top].LineStyle = LineStyle.Double;
borders[BorderType.Bottom].LineStyle = LineStyle.Double;
// Set paragraph shading
Shading shading = builder.ParagraphFormat.Shading;
shading.Texture = TextureIndex.TextureDiagonalCross;
shading.BackgroundPatternColor = System.Drawing.Color.LightCoral;
shading.ForegroundPatternColor = System.Drawing.Color.LightSalmon;
builder.Write("I am a formatted para with double border and nice shading.");
[Visual Basic]
Dim doc As Document = New Document()
Dim builder As DocumentBuilder = New DocumentBuilder(doc)
' Set paragraph borders
Dim borders As BorderCollection = builder.ParagraphFormat.Borders
borders.DistanceFromText = 20
borders(BorderType.Left).LineStyle = LineStyle.Double
borders(BorderType.Right).LineStyle = LineStyle.Double
borders(BorderType.Top).LineStyle = LineStyle.Double
borders(BorderType.Bottom).LineStyle = LineStyle.Double
' Set paragraph shading
Dim shading As Shading = builder.ParagraphFormat.Shading
shading.Texture = TextureIndex.TextureDiagonalCross
shading.BackgroundPatternColor = System.Drawing.Color.LightCoral
shading.ForegroundPatternColor = System.Drawing.Color.LightSalmon
builder.Write("I am a formatted para with double border and nice shading.")
[Java]
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Set paragraph borders
BorderCollection borders = builder.getParagraphFormat().getBorders();
borders.setDistanceFromText(20);
borders.get(BorderType.LEFT).setLineStyle(LineStyle.DOUBLE);
borders.get(BorderType.RIGHT).setLineStyle(LineStyle.DOUBLE);
borders.get(BorderType.TOP).setLineStyle(LineStyle.DOUBLE);
borders.get(BorderType.BOTTOM).setLineStyle(LineStyle.DOUBLE);
// Set paragraph shading
Shading shading = builder.getParagraphFormat().getShading();
shading.setTexture(TextureIndex.TEXTURE_DIAGONAL_CROSS);
shading.setBackgroundPatternColor(Color.CYAN);
shading.setForegroundPatternColor(Color.RED);
builder.write("I am a formatted para with double border and nice shading.");