Borders and Shading
Borders and Shading
Border and shading formatting in Aspose.Words FOSS for .NET is modeled by two related attribute classes: Border, which describes a single border line, and Shading, which describes a background fill pattern. Paragraphs, tables, cells, and fonts each expose a BorderCollection (multiple Border sides) and a single Shading object through their respective formatting properties.
Working with Individual Borders
A Border object represents one border line and exposes LineStyle (an enum value from LineStyle, such as Single, Double, Dot, or Wave), LineWidth, Color, DistanceFromText, Shadow, and theme-aware ThemeColor/TintAndShade properties. Call ClearFormatting() on a Border to reset it to its default (no border) state.
BorderCollection groups multiple Border objects for an object’s sides. It exposes named properties for the four edges — Left, Right, Top, Bottom — plus Horizontal and Vertical for interior table borders. The BorderCollection indexer retrieves a specific side using a BorderType enum value (Left, Right, Top, Bottom, Horizontal, Vertical, DiagonalDown, DiagonalUp, or None) as the key. Setting LineWidth, LineStyle, or Color directly on the collection applies that value to every border it contains at once; ClearFormatting() clears all borders in the collection together.
Applying Shading
Shading carries the background pattern for an object: BackgroundPatternColor and ForegroundPatternColor define the two pattern colors, and Texture (a TextureIndex enum value) selects the pattern — solid fills use TextureIndex.TextureSolid, while percentage and directional patterns (Texture10Percent through Texture97Pt5Percent, TextureHorizontal, TextureDiagonalCross, and others) reproduce Word’s shading gallery. ForegroundPatternThemeColor and BackgroundPatternThemeColor provide theme-color equivalents, alongside tint/shade adjustment via ForegroundTintAndShade and BackgroundTintAndShade. ClearFormatting() resets a Shading object to no fill.
Tips and Best Practices
- Set
TexturetoTextureIndex.TextureSolidand useBackgroundPatternColorfor a plain background fill — the most common shading case. - Use the
BorderCollectionindexer (borders[BorderType.Horizontal]/borders[BorderType.Vertical]) to reach the interior grid lines of a table, which are not exposed as named properties. - Setting properties directly on a
BorderCollection(rather than on an individualLeft/Right/Top/BottomBorder) is the fastest way to apply uniform borders to all sides. - Remember
LineStyle.Noneremoves the visible line without clearing the rest of the border’s configured width or color — useClearFormatting()for a full reset. - Table cell and paragraph borders are independent
BorderCollectioninstances; setting one does not affect the other.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
Border not visible after setting Color | LineStyle left at LineStyle.None | Set LineStyle to a visible style such as Single before saving |
| Shading appears as a solid fill when a pattern was expected | Texture left at its default | Explicitly set Texture to the desired pattern constant |
| Interior table borders unaffected by edge border changes | Horizontal/Vertical are separate from Left/Right/Top/Bottom | Set BorderCollection.Horizontal and .Vertical explicitly |
BorderCollection indexer returns unexpected border | Wrong BorderType value passed | Confirm the BorderType enum value matches the intended side |
FAQ
How do I apply the same border to all four sides of a table cell?
Set LineStyle, LineWidth, and Color directly on the cell’s BorderCollection rather than on each of Left, Right, Top, and Bottom individually — the collection-level setters apply to every border it contains.
What is the difference between Border and Shading?
Border describes a line (style, width, color) drawn along an edge; Shading describes a background fill pattern behind the content. They are independent — an object can have shading without borders, or borders without shading.
Which enum controls the visual line pattern of a border?
LineStyle, with values such as Single, Double, Dot, DashSmallGap, and Wave.
Can I remove a border entirely?
Yes — call ClearFormatting() on the Border or BorderCollection, or set LineStyle to LineStyle.None.
API Reference Summary
| Class / Method | Description |
|---|---|
Border | A single border line: style, width, color, shadow |
BorderCollection | The set of borders (Left, Right, Top, Bottom, Horizontal, Vertical) for an object |
BorderType | Enum identifying a border side |
LineStyle | Enum of visual line styles (Single, Double, Dot, etc.) |
Shading | Background pattern fill: colors and Texture |
TextureIndex | Enum of shading pattern textures |