KryptonPrintDocument
Overview
KryptonPrintDocument extends the standard .NET PrintDocument class to provide full integration with the Krypton Toolkit theming system. It enables printing with palette-based colors, fonts, and styles, ensuring that printed output matches the visual appearance of your Krypton-themed application.
This class provides:
- Palette-aware color retrieval for text, backgrounds, and borders
- Themed font support
- Helper methods for drawing themed content
- Automatic palette change handling
- Integration with the global Krypton palette system
Namespace
using Krypton.Toolkit;
Class Hierarchy
PrintDocument
└── KryptonPrintDocument
Features
Core Features
- Palette Integration: Full integration with Krypton palette system
- Themed Colors: Retrieve text, background, and border colors from the current palette
- Themed Fonts: Access fonts defined in the palette
- Helper Methods: Convenient methods for drawing themed content
- Global Palette Support: Automatically responds to global palette changes
- Custom Palette Support: Can use custom palettes or specific palette modes
Printing Features
- Standard PrintDocument: Inherits all standard
PrintDocumentfunctionality - PrintPage Event: Supports standard
PrintPageevent handling - Document Properties: Standard document name, printer settings, etc.
- Themed Drawing: Helper methods for drawing with palette colors
API Reference
Constructors
KryptonPrintDocument()
Initializes a new instance of the KryptonPrintDocument class.
Example:
var document = new KryptonPrintDocument();
Notes:
- Automatically subscribes to global palette changes
- Sets palette to global default (
PaletteMode.Global) - Initializes with default text and background styles
Properties
PaletteMode (PaletteMode)
Gets or sets the palette mode to be applied.
- Type:
PaletteMode - Default Value:
PaletteMode.Global - Category: Visuals
- Description: Palette applied to printing.
Valid Values:
PaletteMode.Global: Uses the current global palettePaletteMode.ProfessionalSystem: Professional system palettePaletteMode.ProfessionalOffice2003: Office 2003 palettePaletteMode.Office2007Blue: Office 2007 Blue palettePaletteMode.Office2007Silver: Office 2007 Silver palettePaletteMode.Office2007White: Office 2007 White palettePaletteMode.Office2007Black: Office 2007 Black palettePaletteMode.Office2010Blue: Office 2010 Blue palettePaletteMode.Office2010Silver: Office 2010 Silver palettePaletteMode.Office2010White: Office 2010 White palettePaletteMode.Office2010Black: Office 2010 Black palettePaletteMode.Office2013White: Office 2013 White palettePaletteMode.Office2013LightGray: Office 2013 Light Gray palettePaletteMode.Office2013DarkGray: Office 2013 Dark Gray palettePaletteMode.Microsoft365DarkGray: Microsoft 365 Dark Gray palettePaletteMode.Microsoft365Black: Microsoft 365 Black palettePaletteMode.Microsoft365Blue: Microsoft 365 Blue palettePaletteMode.Microsoft365White: Microsoft 365 White palettePaletteMode.SparkleBlue: Sparkle Blue palettePaletteMode.SparkleOrange: Sparkle Orange palettePaletteMode.SparklePurple: Sparkle Purple palettePaletteMode.Custom: Uses a custom palette (set viaPaletteproperty)
Example:
document.PaletteMode = PaletteMode.Office365Blue;
Notes:
- Setting to
PaletteMode.Customrequires setting thePaletteproperty - Changing this property raises the
PaletteChangedevent - Resets
Palettetonullwhen changed fromCustom
Palette (PaletteBase?)
Gets and sets the custom palette implementation.
- Type:
PaletteBase? - Default Value:
null - Category: Visuals
- Description: Custom palette applied to printing.
Example:
var customPalette = new PaletteCustom();
document.Palette = customPalette;
document.PaletteMode = PaletteMode.Custom;
Notes:
- Setting a custom palette automatically sets
PaletteModetoCustom - Setting to
nullreverts toPaletteMode.Global - Changing this property raises the
PaletteChangedevent
UsePaletteColors (bool)
Gets or sets a value indicating whether to use palette colors when printing.
- Type:
bool - Default Value:
true - Category: Behavior
- Description: Indicates whether to use palette colors when printing.
Example:
document.UsePaletteColors = true; // Use themed colors
document.UsePaletteColors = false; // Use default colors (Black/White)
Notes:
- When
false, color methods return default colors (Black for text, White for background) - Useful for printing documents that should always use standard colors
- Does not affect standard
PrintDocumentfunctionality
TextStyle (PaletteContentStyle)
Gets or sets the text style used for retrieving text colors from the palette.
- Type:
PaletteContentStyle - Default Value:
PaletteContentStyle.LabelNormalPanel - Category: Visuals
- Description: Text style used for retrieving text colors from the palette.
Common Values:
PaletteContentStyle.LabelNormalPanelPaletteContentStyle.LabelBoldPanelPaletteContentStyle.LabelItalicPanelPaletteContentStyle.LabelTitlePanelPaletteContentStyle.LabelSecondaryPanel- And many more...
Example:
document.TextStyle = PaletteContentStyle.LabelBoldPanel;
Notes:
- Affects the color and font returned by
GetTextColor()andGetFont() - Choose a style that matches your document's text appearance needs
BackgroundStyle (PaletteBackStyle)
Gets or sets the background style used for retrieving background colors from the palette.
- Type:
PaletteBackStyle - Default Value:
PaletteBackStyle.PanelClient - Category: Visuals
- Description: Background style used for retrieving background colors from the palette.
Common Values:
PaletteBackStyle.PanelClientPaletteBackStyle.PanelAlternatePaletteBackStyle.PanelCustom1PaletteBackStyle.PanelCustom2PaletteBackStyle.PanelCustom3- And many more...
Example:
document.BackgroundStyle = PaletteBackStyle.PanelAlternate;
Notes:
- Affects the color returned by
GetBackgroundColor() - Choose a style that matches your document's background appearance needs
Events
PaletteChanged
Occurs when the palette changes.
- Type:
EventHandler? - Category: Property Changed
- Description: Occurs when the value of the Palette property is changed.
Example:
document.PaletteChanged += (sender, e) =>
{
// Palette has changed, update print preview if needed
RefreshPreview();
};
Notes:
- Raised when
PaletteModeorPaletteproperty changes - Also raised when global palette changes (if using
PaletteMode.Global)
Themed Printing Helper Methods
GetTextColor(PaletteState state = PaletteState.Normal)
Gets the text color from the current palette.
Parameters:
state(PaletteState): The palette state to use. Default:PaletteState.Normal
Returns:
Color: The text color from the palette, orColor.Blackif palette is not available orUsePaletteColorsisfalse.
Example:
var textColor = document.GetTextColor();
var disabledColor = document.GetTextColor(PaletteState.Disabled);
Notes:
- Returns
Color.BlackifUsePaletteColorsisfalse - Returns
Color.Blackif palette isnull - Uses
TextStyleproperty to determine which color to retrieve
GetBackgroundColor(PaletteState state = PaletteState.Normal)
Gets the background color from the current palette.
Parameters:
state(PaletteState): The palette state to use. Default:PaletteState.Normal
Returns:
Color: The background color from the palette, orColor.Whiteif palette is not available orUsePaletteColorsisfalse.
Example:
var backColor = document.GetBackgroundColor();
var pressedColor = document.GetBackgroundColor(PaletteState.Pressed);
Notes:
- Returns
Color.WhiteifUsePaletteColorsisfalse - Returns
Color.Whiteif palette isnull - Uses
BackgroundStyleproperty to determine which color to retrieve
GetBorderColor(PaletteState state = PaletteState.Normal)
Gets the border color from the current palette.
Parameters:
state(PaletteState): The palette state to use. Default:PaletteState.Normal
Returns:
Color: The border color from the palette, orColor.Blackif palette is not available orUsePaletteColorsisfalse.
Example:
var borderColor = document.GetBorderColor();
Notes:
- Returns
Color.BlackifUsePaletteColorsisfalse - Returns
Color.Blackif palette isnull - Uses
PaletteBorderStyle.ControlClientfor border style
GetFont(PaletteState state = PaletteState.Normal)
Gets the font from the current palette.
Parameters:
state(PaletteState): The palette state to use. Default:PaletteState.Normal
Returns:
Font: The font from the palette, or a default font (Arial, 12pt) if palette is not available orUsePaletteColorsisfalse.
Example:
var font = document.GetFont();
var boldFont = new Font(document.GetFont(), FontStyle.Bold);
Notes:
- Returns
new Font("Arial", 12)ifUsePaletteColorsisfalse - Returns
new Font("Arial", 12)if palette isnull - Uses
TextStyleproperty to determine which font to retrieve - Caller is responsible for disposing the returned font if needed
DrawThemedText(Graphics graphics, string text, Font? font, Rectangle bounds, StringFormat? format = null, PaletteState state = PaletteState.Normal)
Draws text using palette colors.
Parameters:
graphics(Graphics): The Graphics object to draw ontext(string): The text to drawfont(Font?): The font to use (ifnull, uses palette font)bounds(Rectangle): The bounding rectangle for the textformat(StringFormat?): The StringFormat to use. Default:StringFormat.GenericDefaultstate(PaletteState): The palette state to use. Default:PaletteState.Normal
Example:
document.DrawThemedText(e.Graphics, "Hello, World!", null,
new Rectangle(100, 100, 200, 50));
Notes:
- Does nothing if
graphicsisnullortextis null/empty - Uses palette font if
fontparameter isnull - Creates and disposes a
SolidBrushinternally
DrawThemedRectangle(Graphics graphics, Rectangle bounds, PaletteState state = PaletteState.Normal)
Draws a rectangle with themed background and border.
Parameters:
graphics(Graphics): The Graphics object to draw onbounds(Rectangle): The bounding rectanglestate(PaletteState): The palette state to use. Default:PaletteState.Normal
Example:
document.DrawThemedRectangle(e.Graphics,
new Rectangle(50, 50, 500, 700));
Notes:
- Does nothing if
graphicsisnull - Fills the rectangle with background color, then draws border
- Creates and disposes brushes/pens internally
DrawThemedLine(Graphics graphics, int x1, int y1, int x2, int y2, PaletteState state = PaletteState.Normal)
Draws a line using the border color from the palette.
Parameters:
graphics(Graphics): The Graphics object to draw onx1(int): The x-coordinate of the first pointy1(int): The y-coordinate of the first pointx2(int): The x-coordinate of the second pointy2(int): The y-coordinate of the second pointstate(PaletteState): The palette state to use. Default:PaletteState.Normal
Example:
document.DrawThemedLine(e.Graphics, 100, 200, 500, 200);
Notes:
- Does nothing if
graphicsisnull - Creates and disposes a
Peninternally
GetTextBrush(PaletteState state = PaletteState.Normal)
Gets a SolidBrush with the text color from the palette.
Parameters:
state(PaletteState): The palette state to use. Default:PaletteState.Normal
Returns:
Brush: ASolidBrushwith the palette text color.
Example:
using (var brush = document.GetTextBrush())
{
e.Graphics.DrawString("Text", font, brush, point);
}
Notes:
- Important: Caller is responsible for disposing the returned brush
- Use within a
usingstatement for proper resource management
GetBackgroundBrush(PaletteState state = PaletteState.Normal)
Gets a SolidBrush with the background color from the palette.
Parameters:
state(PaletteState): The palette state to use. Default:PaletteState.Normal
Returns:
Brush: ASolidBrushwith the palette background color.
Example:
using (var brush = document.GetBackgroundBrush())
{
e.Graphics.FillRectangle(brush, bounds);
}
Notes:
- Important: Caller is responsible for disposing the returned brush
- Use within a
usingstatement for proper resource management
GetBorderPen(PaletteState state = PaletteState.Normal, float width = 1.0f)
Gets a Pen with the border color from the palette.
Parameters:
state(PaletteState): The palette state to use. Default:PaletteState.Normalwidth(float): The pen width. Default:1.0f
Returns:
Pen: APenwith the palette border color.
Example:
using (var pen = document.GetBorderPen(state: PaletteState.Normal, width: 2.0f))
{
e.Graphics.DrawRectangle(pen, bounds);
}
Notes:
- Important: Caller is responsible for disposing the returned pen
- Use within a
usingstatement for proper resource management
Usage Examples
Basic Themed Printing
using Krypton.Toolkit;
using System.Drawing;
using System.Drawing.Printing;
var document = new KryptonPrintDocument
{
DocumentName = "Themed Report",
PaletteMode = PaletteMode.Office365Blue
};
document.PrintPage += (sender, e) =>
{
var doc = (KryptonPrintDocument)sender;
// Get themed colors
var textColor = doc.GetTextColor();
var backColor = doc.GetBackgroundColor();
var font = doc.GetFont();
// Fill background
using (var brush = new SolidBrush(backColor))
{
e.Graphics.FillRectangle(brush, e.MarginBounds);
}
// Draw text
using (var brush = new SolidBrush(textColor))
{
e.Graphics.DrawString("Themed Document", font, brush,
e.MarginBounds.X, e.MarginBounds.Y);
}
e.HasMorePages = false;
};
document.Print();
Using Helper Methods
document.PrintPage += (sender, e) =>
{
var doc = (KryptonPrintDocument)sender;
// Draw themed rectangle (background + border)
doc.DrawThemedRectangle(e.Graphics, e.MarginBounds);
// Draw themed text
var textBounds = new Rectangle(
e.MarginBounds.X + 50,
e.MarginBounds.Y + 50,
e.MarginBounds.Width - 100,
100);
doc.DrawThemedText(e.Graphics, "Document Title",
new Font("Arial", 18, FontStyle.Bold),
textBounds);
// Draw themed line separator
doc.DrawThemedLine(e.Graphics,
e.MarginBounds.X,
e.MarginBounds.Y + 150,
e.MarginBounds.Right,
e.MarginBounds.Y + 150);
e.HasMorePages = false;
};
Custom Palette Usage
// Create custom palette
var customPalette = new PaletteCustom();
// Configure document with custom palette
var document = new KryptonPrintDocument
{
Palette = customPalette,
PaletteMode = PaletteMode.Custom,
TextStyle = PaletteContentStyle.LabelBoldPanel,
BackgroundStyle = PaletteBackStyle.PanelAlternate
};
document.PrintPage += (sender, e) =>
{
// Use themed colors from custom palette
var doc = (KryptonPrintDocument)sender;
// ... printing logic
};
Responding to Palette Changes
var document = new KryptonPrintDocument();
document.PaletteChanged += (sender, e) =>
{
// Update preview or refresh document when palette changes
if (previewControl != null)
{
previewControl.Invalidate();
}
};
// Later, when user changes theme
KryptonManager.GlobalPaletteMode = PaletteMode.Office2013DarkGray;
// PaletteChanged event will fire automatically
Multi-Page Document with Themed Colors
int currentPage = 0;
const int totalPages = 5;
var document = new KryptonPrintDocument
{
DocumentName = "Multi-Page Report",
PaletteMode = PaletteMode.Microsoft365Blue
};
document.PrintPage += (sender, e) =>
{
var doc = (KryptonPrintDocument)sender;
// Fill page background
using (var brush = doc.GetBackgroundBrush())
{
e.Graphics.FillRectangle(brush, e.MarginBounds);
}
// Draw page header
using (var brush = doc.GetTextBrush())
using (var font = new Font(doc.GetFont(), FontStyle.Bold))
{
e.Graphics.DrawString($"Page {currentPage + 1} of {totalPages}",
font, brush, e.MarginBounds.X, e.MarginBounds.Y);
}
// Draw page content
var contentY = e.MarginBounds.Y + 50;
using (var brush = doc.GetTextBrush())
{
e.Graphics.DrawString($"This is page {currentPage + 1}",
doc.GetFont(), brush,
e.MarginBounds.X, contentY);
}
currentPage++;
e.HasMorePages = currentPage < totalPages;
};
document.Print();
Disabling Palette Colors
// Print with standard colors regardless of palette
var document = new KryptonPrintDocument
{
UsePaletteColors = false // Always use Black/White
};
document.PrintPage += (sender, e) =>
{
// GetTextColor() will return Color.Black
// GetBackgroundColor() will return Color.White
var textColor = document.GetTextColor(); // Always Black
// ... printing logic
};
Integration with Krypton Toolkit
Global Palette Integration
KryptonPrintDocument automatically integrates with the global Krypton palette:
- Default Behavior: Uses
PaletteMode.Globalby default - Automatic Updates: Subscribes to
KryptonManager.GlobalPaletteChangedevent - Dynamic Theming: Changes when global palette changes (if using
PaletteMode.Global)
Related Components
KryptonPrintPreviewDialog: UsesKryptonPrintDocumentfor previewKryptonPrintPreviewControl: DisplaysKryptonPrintDocumentin previewKryptonPrintDialog: Standard print dialog that works withKryptonPrintDocument
Design-Time Support
Toolbox Integration
- Toolbox Item:
true - Toolbox Bitmap:
ToolboxBitmaps.KryptonPrintDocument.bmp - Default Property:
DocumentName - Designer Category: Code
Property Grid Support
All properties are visible in the Visual Studio property grid with appropriate categories:
- Behavior:
UsePaletteColors - Visuals:
PaletteMode,Palette,TextStyle,BackgroundStyle
Thread Safety
This class is not thread-safe. All operations should be performed on the UI thread, especially when handling the PrintPage event.
Disposal Pattern
The class properly unsubscribes from global palette changes:
protected override void Dispose(bool disposing)
{
if (disposing)
{
KryptonManager.GlobalPaletteChanged -= OnGlobalPaletteChanged;
}
base.Dispose(disposing);
}
Best Practice: Always dispose the document when finished:
using (var document = new KryptonPrintDocument())
{
// Use document
document.Print();
}
Performance Considerations
- Color Retrieval: Palette color retrieval is fast but involves method calls
- Font Creation:
GetFont()creates a newFontinstance each time (consider caching) - Brush/Pen Creation: Helper methods create new brushes/pens; dispose them properly
- Global Palette Subscription: Minimal overhead; unsubscribes on disposal
Best Practices
- Dispose Resources: Always dispose brushes, pens, and fonts returned by helper methods
- Cache Fonts: If using the same font multiple times, cache the result of
GetFont() - Use Helper Methods: Prefer
DrawThemedText()andDrawThemedRectangle()for simpler code - Handle Palette Changes: Subscribe to
PaletteChangedif you need to refresh previews - Choose Appropriate Styles: Select
TextStyleandBackgroundStylethat match your document design
Limitations
- Font Disposal:
GetFont()returns a new font each time; caller must dispose if needed - Brush/Pen Disposal:
GetTextBrush(),GetBackgroundBrush(), andGetBorderPen()require disposal - State Support: Some palette states may not be fully supported for printing scenarios
- Color Fallbacks: Always falls back to default colors if palette is unavailable
See Also
KryptonPrintPreviewDialog- Preview dialog componentKryptonPrintPreviewControl- Preview control componentKryptonPrintDialog- Print dialog component