This section describes the concept of Aspose.Editor commands, how applications can use standard commands and create custom commands.
Command explained
Aspose.Editor control provides a solid way to do common tasks using preset commands. Command is a function which performs specific task on the editor control and a set of properties around this function like name and description. EditorControl.Commands collection provides a way to enumerate all available commands or find command by name.
The main purpose for a command is to uniquely represent a function which can be associated with an interface graphical element like tool bar button or menu item. In this way command can be bind to several interface elements and at the same time represent single point to access properties like text, image, and enabled state.
Standard and custom commands
There is a set of commands which are already implemented and available to application right away. These are standard commands and they cover such tasks as open/save/close document files via dialogs, change document formatting, printing and preview and others. Most standard commands appear on the standard menu bar and tool bar. Custom commands are dynamically added by application and extend existing set of standard commands with custom actions.
The table below presents all the standard commands currently available in the control:
|
Command Name
|
Description
|
|
New
|
Opens new blank document.
|
|
Open
|
Opens document from a file.
|
|
Save
|
Saves currently opened document into a file.
|
|
Save As
|
Saves currently opened document into a file with specified name and location.
|
|
Close
|
Closes currently opened document.
|
|
Print
|
Prints current document.
|
|
Print Preview
|
Shows print preview dialog.
|
|
Thumbnails
|
Generates thumbnails images of the current document.
|
|
Page Setup
|
Sets up current section page properties.
|
|
Cut
|
Cuts current selection.
|
|
Copy
|
Copies current selection into the clipboard.
|
|
Paste
|
Replaces current selection with the clipboard contents.
|
|
Undo
|
Undo last edit operation.
|
|
Redo
|
Redo last edit operation.
|
|
Find and Replace
|
Opens find and replace dialog.
|
|
Select All
|
Selects the whole document.
|
|
Page Width
|
Scales the view to see the width of the page.
|
|
Show All
|
Shows/hides all the formatting marks.
|
|
Text Width
|
Scales the view to see the width of the text.
|
|
20% Zoom
|
Scales the view to 20%.
|
|
50% Zoom
|
Scales the view to 50%.
|
|
100% Zoom
|
Scales the view to 100%.
|
|
200% Zoom
|
Scales the view to 200%.
|
|
500% Zoom
|
Scales the view to 500%.
|
|
Whole Page
|
Scales the view to see the whole page.
|
|
Break
|
Shows dialog to insert break.
|
|
Line Break
|
Inserts a line break.
|
|
Column Break
|
Inserts a column break.
|
|
Page Break
|
Inserts a page break.
|
|
Section Break
|
Inserts a section break.
|
|
Bold
|
Toggles bold style for the current selection or paragraph.
|
|
Italic
|
Toggles italic style for the current selection or paragraph.
|
|
Align Right
|
Aligns current paragraph right.
|
|
Align Left
|
Aligns current paragraph left.
|
|
Align Center
|
Centers current paragraph.
|
|
Justify
|
Justifies current paragraph.
|
|
Font
|
Opens font format dialog.
|
|
Paragraph
|
Opens paragraph format dialog.
|
|
Columns
|
Sets up columns of the current section.
|
|
Style
|
Opens style list dialog.
|
|
Clear
|
Clears formatting of the current selection.
|
|
About
|
Opens about dialog.
|
Sample code
Commands can be used in the following ways:
- Application can get command instance using EditorControl.Commands collection,
- Method Invoke can be used on command instance to directly execute command function,
- Properties Name, Text, Description, Image, Shortcut store command specific information,
- Method GetMenuItem and GetToolBarButton return interface elements which can be used in menus and tool bars. These elements are returned configured to represent command with Text, ToolTipText, Icon and Tag. When used in standard controls they will invoke command when clicked.
- Custom command can be created using one of Commands.Add methods,
- Existing command can be modified by changing InvokeHandler and EnabledHandler delegates.
Note. You can look through the demo projects to see how commands work in every detail.
In order to get command instance application need to use Commands collection. Command can be executed directly by calling Invoke or can be bind to menu item or tool bar. For this purpose Command implements two methods GetMenuItem and GetToolBarButton which return corresponding interface elements.
[C#]
// Get instance of "New" command.
Command cmdNew = editorControl.Commands["New"];
// Append command button to the first standard tool bar
StandardToolBar toolBar = editorControl.Bars.Tools[0] as StandardToolBar;
ToolBarButton toolBarButton = cmdNew.GetToolBarButton();
toolBar.Buttons.Add(toolBarButton);
// Ask user if command shall be executed right away and execute it if required
if ( MessageBox.Show("You are about to execute command \"" + cmdNew.Name +
"\".\n\rDo you want to continue?",
Application.ProductName, MessageBoxButtons.YesNo) == DialogResult.Yes )
{
cmdNew.Invoke();
}