Initialize the toolbar

<< Click to Display Table of Contents >>

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

Initialize the toolbar

To initialize the toolbar we use an array of strings which contains the name of each action we want to list in the toolbar.

 

private string[] _ActionButtons = new string[23] {

"FileOpen", "FileAppend", "FileSaveAsPDF", "SelectStd", "SelectObjects",

"ZoomToRect", "SelectText", "SelectFillForm", "DrawFieldEdit", "DrawFieldCheck",

"DrawAnnotFrame", "DrawAnnotHighlight", "DrawAnnotFreetext", "DrawAnnotSymbol", "DrawAnnotSquiggly",

"DrawAnnotHighlightText", "DrawAnnotBlackText", "DrawTextline", "DrawRect", "DrawImage",

"DrawHighlight", "DrawCircle", "About" };

 

 

We also add PNG images for the buttons to the resources. All images use the build mode "Embedded Resource":

 

clip0021

 

 

In this case the name of the image is the same as the action in the array _ActionButtons. The images are stored in a smaller and in a larger "2x" resolution.

This are the symbols in the order of the actions in the array:

 

wpviewpdf_action_buttons

 

 

private void InitToolbar( ToolStrip toolStrip, string[] Actions )

{

    toolStrip.Height = 40;

    bool highdpi;

 

    // Enable the large buttons if >120dpi!

    Graphics g = Graphics.FromHwnd(new IntPtr(0));

    highdpi = (g.DpiX>120);

 

    // Create the toolbar

    for (int i = 0; i < Actions.Length; i++)

    {

string pngname =

"PDFViewNET.Resources." + Actions[i] + ((highdpi) ? "@2x.png" : ".png");

 

 System.Reflection.Assembly thisExe;

 

// use this to check resource names in debugger!

// string[] db = GetType().Assembly.GetManifestResourceNames();

 

 thisExe = System.Reflection.Assembly.GetExecutingAssembly();

 System.IO.Stream imagestream =  thisExe.GetManifestResourceStream(pngname);

// If you get an exception here

// a) check name of resource

// b) check if resource Buildmoude was set to "Embedded"

 

Image img = Image.FromStream(imagestream);  

   

// Create a new button

ToolStripButton ActionBtn = new ToolStripButton("", img, null, "");

 ActionBtn.ImageScaling = ToolStripItemImageScaling.None;

 

 

// and get the correct id

 ActionBtn.Tag = pdfViewer1.CommandStr(

   commands.COMPDF_ACTION_READ, "?" + Actions[i] );

 ActionBtn.Click += new System.EventHandler(doExecuteWPViewAction);

 

 toolStrip.Items.Add(ActionBtn);

    }

}

 

This method is called at the end of the initialization

 

public Form1()

{

     ...

 

     // Load the menu from the embedded actions

     pdfViewer1.InitMainMenu(menuStrip1, ...

 

    // Initialize the toolbar

    InitToolbar(toolStrip1, _ActionButtons );      

 

}

 

All buttons call the event handler doExecuteWPViewAction which is also used by the menu items.