Sign In  Sign Up Live-Chat

EDITOR AND MACROS

Last post 07-23-2008, 7:13 PM by mf. 7 replies.
Sort Posts: Previous Next
  •  06-17-2008, 12:48 PM 131682

    EDITOR AND MACROS

    Does the Editor offer any support for macros?  My client wants to insert his company name at the top of documents that come in.  He would like to do it with a simple function rather than hand typing the information each time.  It will actually be the name plus some standard formatting, etc.

    Can you point me in the right direction for an ADD TEXT function, if you have one, or other suggestions?

    Thanks,

    ANDY

     
  •  06-17-2008, 6:49 PM 131729 in reply to 131682

    Re: EDITOR AND MACROS

    Hi Andy,
    there are two things I can think of regarding macros.

    First, VB macros which are stored in the document and loaded when document is opened. This type of macros are not supported, we can't run them.

    Second, macros as a way to automate things in the editor, like automatic typing of text in the document, searching and replacing, formatting, etc. As far as I can understand this is not required because we are in .NET world. You can just use C# as a macro language and extend editor with custom commands. Command can use editor API and perform whatever can be done through the UI.

    If you want to allow on the fly interpretation of the macros, like when you type text in C# right in the document, select it and run instantly, then you can use Reflection and emit dynamic assembly which runs this code.

    All these techniques are quite simple. I might be able to write a command for this just like an example and provide it with documentation.

    For now the simple way of automating is writing custom command for what you want, e.g. if you want to automatically add title to the document with a single click you can write custom command and put a toolbar button for it. The example of this technique is provided in both Desktop and Web demos.

    Kind regards,
    Michael

     
  •  07-17-2008, 2:26 PM 135985 in reply to 131729

    Re: EDITOR AND MACROS

    Thanks, that is almost exactly what I want to do.

    When the user presses SAVE, I want the title to be inserted.  Technically, they won't see the title at this time, but if they bring up the document again, it will be there.  I was looking at the example projects today, but couldn't see how to do this.  I figure I can get the document in memory then use Aspose Words to manipulate it, but is there an easier way?  Can I do it while the editor is open, before the Saving and Closing?

    Thanks again.

    ANDY

     
  •  07-17-2008, 9:45 PM 136007 in reply to 135985

    Re: EDITOR AND MACROS

    Hi Andy,
    here is the full source code you need.

    #region Automatic title insertion on Save/SaveAs
    /// <summary>
    /// This is called to replace Save and Save As commands handlers.
    /// </summary>
    private void PatchSaveAndSaveAsCommands(EditorControl editor)
    {
      const string saveName = "Save";
      mSaveHandler = editor.Commands[saveName].InvokeHandler;
      editor.Commands[saveName].InvokeHandler =
    new CommandHandler(MySaveHandler);

      const
    string saveAsName = "Save As";
      mSaveAsHandler = editor.Commands[saveAsName].InvokeHandler;
      editor.Commands[saveAsName].InvokeHandler =
    new CommandHandler(MySaveAsHandler);
    }

    /// <summary>
    /// This is called when Save command is invoked from the toolbar or menu.
    /// </summary>
    private void MySaveHandler(EditorControl editor)
    {
      CheckAndPrependDocumentTitle(editor.Document);
      mSaveHandler(editor);
    }
    private CommandHandler mSaveHandler;

    /// <summary>
    /// This is called when SaveAs command is invoked from the toolbar or menu.
    /// </summary>
    private void MySaveAsHandler(EditorControl editor)
    {
      CheckAndPrependDocumentTitle(editor.Document);
      mSaveAsHandler(editor);
    }
    private CommandHandler mSaveAsHandler;

    /// <summary>
    /// This is called before standard Save or Save As commands are processed.
    /// Method will check if document has required title and if not will add it.
    /// Title instantly appears in the document and stays there even if document
    /// is not actually saved (because user cancels Save As dialog for example).
    /// </summary>
    private static void CheckAndPrependDocumentTitle(Document document)
    {
      Debug.Assert(document !=
    null);

     
    // This is the title we are going to insert. Note that it has paragraph break.
     
    const string Title = "Automatic title of the document.\r";

      // This is the style we'd like for the title. It's built-in style so it will be in place.
      const string StyleName = "Heading 1";

     
    // Check if document already has title.
     
    bool hasTitle = false, hasStyle = false;
      Range range = document.GetRange();
      if ( range.Length >= Title.Length )
      {
        range.SetStartEnd(0, Title.Length);
        hasTitle = (range.Text == Title);
        if ( hasTitle )
          hasStyle = (range.StyleName == StyleName);
      }

     
    // Insert title if not there.
     
    if ( !hasTitle )
      {
        range.SetStartEnd(0, 0);
        range.Text = Title;
      }

     
    // Apply style if needed.
     
    if ( !hasStyle )
        range.StyleName = StyleName;
    }
    #endregion

    Add this code to your form and after editor object is created call PatchSaveAndSaveAsCommands method. I've checked this code, it works.

    Kind regards,
    Michael

     
  •  07-22-2008, 3:38 PM 136521 in reply to 136007

    Re: EDITOR AND MACROS

    I saved my document to disk, then used Aspose.Words to modify the document (add titles, replace text, format the document to company standards).  I now need to redisplay the document in the Editor.  User will select a document and it appears in the editor and they can then make minor changes to it. So, I have a aspx form in the top half of the screen and the editor in the bottom.  Users will press SAVE DOCUMENT button and I want to automatically redisplay the updated document in the editor.

    My code is as follows (when the user presses SAVE, it starts this procedure):

    function PrepareCleanDocument(fName)

    { parent.main.SaveDocument();        -- This saves the document to the disk

    document.forms[0].item("TheSave").click();  -- This uses Aspose.Words and formats the document

    PrepareloadDocument(fName);  -- This should reload the document

    }

    The document saves, and then gets updated, however, the updated changes don't appear in the editor.  I assume the PrepareloadDocument would reload the document, but it does not.  I am sure it is something simple, any help would be appreciated.  By the way, the PrepareLoadDocument does work, it is the function that gets called at the loading of the page.

    ANDY

     
  •  07-22-2008, 7:17 PM 136533 in reply to 136521

    Re: EDITOR AND MACROS

    Hi Andy,
    if this is jscript code on the web page then I need to understand how your application works.

    Do you have Aspose.Words on the client computer? If you do then you have changed document on the client computer as well. In this case you don't have to re-load the document, just open it from the local disk.

    In general there is no concept of re-loading in the editor. There is a document object. You can construct it from file (name) or stream. You can also use Send and Receive methods on the editor to save and open it from the server. If you have updated document on the server then you need to invoke Receive and need to implement event handler on the server to process this request. If you have updated document on the local computer then you need to construct new Document object from it and assign it to the Document property of the editor.

    Kind regards,
    Michael

     

     
  •  07-23-2008, 3:36 PM 136675 in reply to 136533

    Re: EDITOR AND MACROS

    Going back to the sample code you sent is the EditorControl Parameter the Aspose.Editor.Client. EditorControl or Aspose.Editor.Server.EditorControl.  I put in CLIENT and it compiled, however, when I attempted to call the PATCHSAVEANDSAVEAS

    EditorControl.OnPageLoad();

    PatchSaveAndSaveAsCommands(EditorControl);

    I got an error that Editor.Server cannot be converted to Editor.Client.  So, I changed the parameter to be Editor.Server, but that does not have commands for various lines in your procedure.  Let me know which items I am doing wrong.

     

    Thanks.

     
  •  07-23-2008, 7:13 PM 136689 in reply to 136675

    Re: EDITOR AND MACROS

    Dear Andy,
    sorry for not pointing that this is client code and that this is desktop example.
    If you are in ASP.NET application then you need to create a plug-in module to run this code.

    This is simple:

    1. Create class library project "MyPlugin", reference Aspose.Editor.Client.DLL.

    2. Create class in the library:
    ---cut---
    namespace MyNamespace
    {
       
    [Aspose.Editor.Client.AsposeEditorPluginAttribute("MyPlugin")]
       public class MyClass
       {
          
    public MyClass(Aspose.Editor.Client.EditorControl editor)
          {
             PatchSaveAndSaveAsCommands(editor);
          }
       }
    }
    ---cut---

    3. On the page where editor control is in the <head> section of html put the
    following code:
    ---cut---
    <link rel="configuration" href="ClientBin/Aspose.Editor.Client.DLL.CONFIG">
    ---cut---
    This code tells CLR where configuration file for the client dll is.

    4. Create file "Aspose.Editor.Client.DLL.CONFIG" in the  ClientBin directory (this is where Aspose.Editor.Client.DLL is). Here is the contents of the file:
    ---cut---
    <configuration>
      <appSettings>
        <add key="Aspose.Editor.Plugin.My" value="MyPlugin"/>
      </appSettings>
    </configuration>
    ---cut---
    These settings specify plugin modules used by client.

    5. Build the library and put the output into the ClientBin directory.

    Now run your application, plugin will load into the client and make necessary changes to the commands.

    P.S. You can see sample plugin modules in the demo application for the version 3.0 of the editor. Get latest version if you haven't yet.

    P.S. If you don't want to get version 3.0 you can run this code using technique shown in the web demo for that version. It doesn't use plugins but it uses additional control on the page.

    Kind regards,
    Michael

     
View as RSS news feed in XML