Any Plugin Code Examples in C#?

This forum is for all Flare issues not related to any of the other categories.
Post Reply
dandam
Propeller Head
Posts: 27
Joined: Wed May 02, 2012 11:36 am
Location: Overland Park, KS

Any Plugin Code Examples in C#?

Post by dandam »

Has anyone written a Flare API plugin using C#? If so, are you willing to share the code? I'd like to learn more about how to write plugins. Partly because I'd like to automate some tasks in my 3,000+ page reference guide, but mostly because I'd like to practice coding in C#.

FYI--Although I've been studying C# for about a year now, I had no idea I could use that knowledge to improve my Flare skills. I just read Chapter 10 of "MadCap Flare for Programmers" by Tregner and Owens, and my head nearly exploded. Cool stuff. Thanks guys!
Matt Danda
MadCap Advanced Developer (MAD)
Overland Park, KS
Paulie
Sr. Propeller Head
Posts: 140
Joined: Sun Mar 01, 2015 3:01 pm

Re: Any Plugin Code Examples in C#?

Post by Paulie »

Hi there,

Here is a real basic, but working example that adds a toolbar strip and button. Clicking the button will copy the inner html of the currently selected topic's body tag to the clipboard.

I assume you have downloaded the Flare API documentation already. Most of the below code was originally sourced from there, with a few hacks of my own to get things working.

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using B3.PluginAPIKit;
using System.Windows.Forms;
using System.Windows.Input;
using System.Drawing;
using System.Windows.Media.Imaging;
using System.Xml;

namespace DemoPlugin
    
{
    
//This class is required for the Flare application to connect to:
    public class DemoPlugin : IPlugin
    {
        public IHost mHost; 
        private bool mActivated;
        
        private INavContext mNav;
        

        public bool IsActivated
        {
            get { return mActivated; }
        }

        public string GetVersion()
        {
            return Application.ProductVersion;
        }

        public string GetAuthor()
        {
            return "Company Name";
        }

        public string GetDescription()
        {
            return "Plugin description. ";
        }

        public string GetName()
        {
            return "Plugin name";
        }

        public void Initialize(IHost host)
        {
            mHost = host;


        }




        //This function is called when Flare starts the plugin:
        public void Execute()
        {
            mActivated = true;
            
            //Create the custom toolbar:
            mNav = mHost.GetNavContext();
            CreateCustombar();

            
        }

        //Stop the plugin:
        public void Stop()
        {
            mHost.Dispose();
            mActivated = false;
            MessageBox.Show(GetName() + " deactivated!");
        }




        //Add the toolbar and button:
        private void CreateCustombar()
        {
            ICustomToolBar toolBar = mNav.CreateCustomToolBar("Name shown in the View > Toolbars menu");
            toolBar.AddButton("Button Name", new ButtonCommand(mHost));
           // toolBar.AddSeparator();
         //   toolBar.AddMenuButton(RibbonViewModel.MenuData);

        
            
        }



    }



    //Class to handle the custom button click event:
    public class ButtonCommand : ICommand
    {
        private IHost _host;
        public ButtonCommand(IHost host)
        {
            _host = host;

        }
        
        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
           //button clicked so get the <body> tag contents and send it to the clipboard:

            String Tempstr = _host.GetEditorContext().GetActiveDocument().GetDocumentXml();
            if(Tempstr.Length>0)
            {

                XmlTextReader reader = new XmlTextReader(new System.IO.StringReader(Tempstr));
                reader.ReadToDescendant("body");
                StringBuilder sb = new StringBuilder();
                sb.Append(reader.ReadInnerXml());
                //convert bold to strong (try find and replace for now - if issues do a better solution)
                sb.Replace("<b", "<strong");
                sb.Replace("</b", "</strong");
                String OutputText = sb.ToString();


                try
                {
                    System.Windows.Forms.Clipboard.SetText(OutputText);
                    MessageBox.Show("Text successfully copied to the clipboard","Text Copied",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message,"Cannot Copy to the Clipboard",MessageBoxButtons.OK,MessageBoxIcon.Error);
                                       
                }
               

              
            }
          
        
        }


        public  event EventHandler CanExecuteChanged
        {
            add
            {
                
            }
            remove
            {
               
            }
        }


   

    }




}
"In an ideal world, software should be simple, well designed, and completely intuitive to end users. In the real world, good documentation is king."
dandam
Propeller Head
Posts: 27
Joined: Wed May 02, 2012 11:36 am
Location: Overland Park, KS

Re: Any Plugin Code Examples in C#?

Post by dandam »

Thanks! It's neat to see how others use the Plugin capabilities in Flare.

FWIW: I'm trying to build a plugin that reads the selected text, generates a string that includes the selected text, then inserts a bookmark using that string as the bookmark name.
Matt Danda
MadCap Advanced Developer (MAD)
Overland Park, KS
Post Reply