OnClick event handler

<< Click to Display Table of Contents >>

Navigation:  Create a PDF Editor > .NET (C#) Example >

OnClick event handler

Since just one integer is enough to identify an action, the event handler for the click event can be implemented very versatile. It is even possible to get information which parameters are required for an action from WPViewPDF - so you can copy&paste the following code to your projects and use it there with a minimum of changes.

 

private void doExecuteWPViewAction(object sender, EventArgs e)

{

    int param, paramkind, res;

    string actionname, actionparam;

   

    int ac = 0; // This is the action identifyer

 

    if (sender is System.Windows.Forms.ToolStripMenuItem)

 ac = (int)(sender as System.Windows.Forms.ToolStripMenuItem).Tag;

    else if (sender is System.Windows.Forms.ToolStripButton)

 ac = (int)(sender as System.Windows.Forms.ToolStripButton).Tag;      

   

    param     = pdfViewer1.Command(commands.COMPDF_ACTION_READ, "param", ac );

    paramkind = pdfViewer1.Command(commands.COMPDF_ACTION_READ, "paramkind", ac );

    actionname= pdfViewer1.CommandGetStr(commands.COMPDF_ACTION_READ, "name", ac );

    actionparam="";

   

    if (paramkind==50)

    {

  string s = pdfViewer1.CommandGetStr(commands.COMPDF_ACTION_READ, "hint", ac )+"?";

  if(MessageBox.Show(s,"",MessageBoxButtons.OKCancel)==DialogResult.Cancel) return;

    }

 

    // Bit 2 is set, we need a string parameter!

    if ((param & 2) == 2)

    {

/*     // 0: Pagenr as Int or string

 // 1: Fontname as string

 // 2: Color as Int or string

 // 3: PDF filename as string OPEN

 // 4: PDF filename as string SAVE

 // 5: text filename as string OPEN

 // 6: text filename as string SAVE

 // 7: image file name as string  OPEN

 // 8: JPEG file name as string   SAVE

 // 9: type @ options_comma_list

 // 10: options_comma_list

 // 11: options_for_DrawObjects

 // 12: Zoom Value as Int

 // 13: JPEG image file name as string to OPEN passed as "file=...",... + other params

 // 14: some text as string  passed as "contents=...",... + other params

 // 15: some multiline text as string  passed as "contents=...",... + other params

 // 16: Boolean  on/off 1/0

 

 // 50: Ask $hint$ yes/now

 // 51: Ask $hint$ yes/no/cancel

  */

 

if ( (paramkind == 3) || (paramkind == 5) || (paramkind == 6) || (paramkind == 13) )

     {

if (paramkind==3)   openFileDialog1.Filter = "PDF Files (*.PDF)|*.PDF";

else if (paramkind==5)  openFileDialog1.Filter = "Text Files (*.TXT)|*.TXT,*.*";

else if ((paramkind==3) || (paramkind==13))  

                                         openFileDialog1.Filter = "Image Files (*.JPG)|*.JPG;*.JPEG";

 

if (openFileDialog1.ShowDialog()==DialogResult.Cancel) return;

else actionparam = openFileDialog1.FileName;

 

// This parameter is used for JPEG Draw Objects

if (paramkind==13)

   actionparam = "\"file=" + actionparam + "\""; // + Color params   color= background-color

 

     }

    else if ( (paramkind == 4) || (paramkind == 6) || (paramkind == 8)  )

     {

if (paramkind == 4) saveFileDialog1.Filter = "PDF Files (*.PDF)|*.PDF";

else if (paramkind == 6)  saveFileDialog1.Filter = "Text Files (*.TXT)|*.TXT,*.*";

else if ((paramkind == 8) || (paramkind==13)) saveFileDialog1.Filter = "Image Files (*.JPG)|*.JPG;*.JPEG";

if (saveFileDialog1.ShowDialog() == DialogResult.Cancel) return;

else actionparam = saveFileDialog1.FileName;

}

else if ((paramkind == 14)||(paramkind == 0))   // A string

{

  InputForm dlg = new InputForm();

  dlg.label1.Text = pdfViewer1.CommandGetStr(commands.COMPDF_ACTION_READ, "hint", ac );

  if (dlg.ShowDialog(this)==DialogResult.Cancel) return;

 

  actionparam = (paramkind == 0) ? dlg.textBox1.Text :

    "\"contents=" + dlg.textBox1.Text + "\"";

  dlg.Dispose();

     }

    else if (paramkind == 15)   // A multiline string

     {

  InputForm dlg = new InputForm();

  dlg.label1.Text = pdfViewer1.CommandGetStr(commands.COMPDF_ACTION_READ, "hint", ac);

  dlg.textBox1.Multiline = true;

  dlg.Height = dlg.Height * 2;

  if (dlg.ShowDialog(this) == DialogResult.Cancel) return;

  actionparam = "\"contents=" +  dlg.textBox1.Text + "\"";

  dlg.Dispose();

}

    }

 

    pdfViewer1.CommandStrEx(commands.COMPDF_ACTIONNR, actionparam, ac);

 }

 

 

At the start we check if the event was used by a menu item or toolbar item. Then we get the "ac", the integer value which is identifying an action. We request information about the action from WPViewPDF to check if any parameters are required, and if they are, open dialogs to request the information from the user.

We use the open and save dialog here and also a simple InputForm which has been implemented in the .NET example.

 

Using "commands.COMPDF_ACTIONNR" the action is processed.