<< Click to Display Table of Contents >> Navigation: Create a PDF Editor > Delphi Example > Create "OnClick" |
The action OnClick handler is used to interact with the PDF viewer.
But first add a TOpenDialog and a TSaveDialog to the form - both are used by the code below.
Then fill the event WPViewPDFWizard1ActionClick with the following code. It was developed to work as OnClick handler for TAction, TMenuItem and any TControl class, such as TButton. The command ID is always encoded into "Tag".
The procedure fist uses the static TWPViewPDF instance to get more information about the special command. It reads if the command requires a parameter and of which kind the parameter should be. Some parameterkinds require a dialog, such as a file open or file save dialog. You will see in the code below, how this all works. You can copy&paste that code into your own applications, due to its universal nature.
procedure TForm2.WPViewPDFWizard1ActionClick(Sender: TObject);
var ac, param, paramkind, res : Integer;
actionname, s : string;
begin
// This is a universal Action handler ...
if Sender is TAction then ac := TAction(Sender).Tag
else if Sender is TMenuItem then ac := TMenuItem(Sender).Tag
else if Sender is TControl then ac := TControl(Sender).Tag
else exit;
param := WPViewPDF1.Command(COMPDF_ACTION_READ, 'param', ac );
(* param: bit 2 --> need string *)
paramkind := WPViewPDF1.Command(COMPDF_ACTION_READ, 'paramkind', ac );
(* // paramkind (used for string parameters)
// 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/no
// 51: Ask $hint$ yes/no/cancel
*)
actionname:= WPViewPDF1.CommandGetStr(COMPDF_ACTION_READ, 'name', ac );
s := '';
if paramkind=50 then
begin
if MessageDlg(pdf.CommandGetStr(COMPDF_ACTION_READ, 'hint', ac )
+'?', mtConfirmation, [mbYes,mbNo], 0)=IDNO then exit;
end
else if paramkind=51 then
begin
// Ask: YES, NO CANCEL
iparam := MessageDlg(pdf.CommandGetStr(COMPDF_ACTION_READ, 'hint', ac )+'?',
mtConfirmation, [mbYes,mbNo,mbCancel], 0);
if iparam=IDCANCEL then exit;
if iparam = IDYES then
s := 'true';
end;
// bit 2 is set, we need a string parameter!
if (param and 2) = 2 then
begin
if paramkind in [3,5,6,13] then
begin
if paramkind=3 then
OpenDialog1.Filter := 'PDF Files (*.PDF)|*.PDF'
else if paramkind=5 then
OpenDialog1.Filter := 'Text Files (*.TXT)|*.TXT,*.*'
else if (paramkind=3) or (paramkind=13) then
OpenDialog1.Filter := 'Image Files (*.JPG)|*.JPG;*.JPEG';
if not OpenDialog1.Execute then exit
else s := OpenDialog1.FileName;
// This parameter is used for JPEG Draw Objects
if paramkind=13 then
s := '"file=' + s + '"'; // + Color params color= background-color
end
else if paramkind in [4,6,8] then
begin
if paramkind=4 then
SaveDialog1.Filter := 'PDF Files (*.PDF)|*.PDF'
else if paramkind=6 then
SaveDialog1.Filter := 'Text Files (*.TXT)|*.TXT,*.*'
else if (paramkind=8) or (paramkind=13) then
SaveDialog1.Filter := 'Image Files (*.JPG)|*.JPG;*.JPEG';
if not SaveDialog1.Execute then exit
else s := SaveDialog1.FileName;
end
else if paramkind in [14] then // A string
begin
s := '';
if InputQuery(pdf.CommandGetStr(COMPDF_ACTION_READ,
'hint', ac ), '', s) then
s := '"contents=' + s + '"' // + Color params color= background-color
else exit;
end
else if paramkind in [15] then // a multiline string -> contents
begin
s := '';
if InputQuery('', pdf.CommandGetStr(COMPDF_ACTION_READ,
'hint', ac ), s) then
s := '"contents=' + s + '"' // + Color params color= background-color
else exit;
end
else if paramkind in [0] then // Just a string
begin
s := '';
if not InputQuery(pdf.CommandGetStr(COMPDF_ACTION_READ,
'hint', ac ), '', s) then exit;
end;
end;
// We can react on special actions
if actionname='FileOpen' then
begin
NewPDFDocument(s);
exit;
end
else if actionname='FileClose' then
begin
if PageControl1.ActivePage<>nil then
PageControl1.ActivePage.Free;
exit;
end;
// Here the Action is executed:
// Note: The number "ac" is no fixed. It may change.
// We read it from the "Tag" of the action
// But you can check actionname
// Example:
// if actionname='DrawAnnotHighlight' then
// s := 'color=blue';
if (pdf=nil) and ((WPViewPDF1.Command(
COMPDF_ACTION_READFLAGS, ac ) and 16)=16) then
res := WPViewPDF1.CommandStrEx( COMPDF_ACTIONNR, s, ac)
else
begin
if (pdf=nil) then exit;
res := pdf.CommandStrEx( COMPDF_ACTIONNR, s, ac);
end;
end;