logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

New Topic Post Reply
Options
Go to last post Go to first unread
Alex  
#1 Posted : Sunday, March 21, 2021 5:10:36 PM(UTC)
Quote
Alex

Rank: Newbie

Groups: Registered
Joined: 3/21/2021(UTC)
Posts: 7
Australia
Location: Sydney

Thanks: 2 times
Hope someone could point me in the right direction.

I'm using PdfViewer to display a document with acroforms. My task is to provide the normal Cut/Copy/Paste functionality for text fields on the document via a context menu. I followed the suggestion in https://forum.patagames....xt-menu-in-WPF-PdfViewer to create the context menu, however, every time I right-click on a field, the menu comes up with all three items disabled (see the image).


context-menu.png


I know that the corresponding keyboard shortcuts Ctrl+X, Ctrl+C, Ctrl+P work just fine, but I need the same functionality available via a context menu. Is this possible?

Edited by user Sunday, March 21, 2021 5:12:04 PM(UTC)  | Reason: Not specified

Paul Rayman  
#2 Posted : Sunday, March 21, 2021 10:37:58 PM(UTC)
Quote
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Hello,

WinForms Example
Code:

private void pdfViewer1_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        MenuItem[] items = {
            new MenuItem("Cut", (s, args)=> Pdfium.FORM_OnChar(pdfViewer1.FillForms.Handle, pdfViewer1.CurrentPage.Handle, 24, KeyboardModifiers.ControlKey)),
            new MenuItem("Copy", (s, args)=> Pdfium.FORM_OnChar(pdfViewer1.FillForms.Handle, pdfViewer1.CurrentPage.Handle, 3, KeyboardModifiers.ControlKey)),
            new MenuItem("Paste", (s, args)=> Pdfium.FORM_OnChar(pdfViewer1.FillForms.Handle, pdfViewer1.CurrentPage.Handle, 22, KeyboardModifiers.ControlKey))
        };
        var menu = new ContextMenu(items);
        menu.Popup += (s, args) =>
        {
            items[0].Enabled = items[1].Enabled = pdfViewer1.FillForms.SelectedText != "";
            items[2].Enabled = pdfViewer1.FillForms.InterForm.Controls.GetFocused() != null;
        };
        menu.Show(pdfViewer1, e.Location);
    }
}


WPF Example:
Code:

private void pdfViewer1_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.RightButton == MouseButtonState.Pressed)
    {
        MenuItem[] items = {
                new MenuItem() { Header = "Cut", Command = new ClipboardCmd(pdfViewer1, "X") },
                new MenuItem() { Header = "Copy", Command = new ClipboardCmd(pdfViewer1, "C") },
                new MenuItem() { Header = "Paste", Command = new ClipboardCmd(pdfViewer1, "V") },
            };
        var menu = new ContextMenu();
        menu.ItemsSource = items;
        menu.IsOpen = true;
    }
}


Code:

class ClipboardCmd : ICommand
{
    private PdfViewer _viewer;
    private string _mode;
    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        if(_mode=="C" || _mode =="X")
            return _viewer.FillForms.SelectedText != "";
        if(_mode =="V")
            return _viewer.FillForms.InterForm.Controls.GetFocused() != null;
        return false;
    }

    public void Execute(object parameter)
    {
        switch (_mode)
        {
            case "X":
                Pdfium.FORM_OnChar(_viewer.FillForms.Handle, _viewer.CurrentPage.Handle, 24, KeyboardModifiers.ControlKey);
                break;
            case "C":
                Pdfium.FORM_OnChar(_viewer.FillForms.Handle, _viewer.CurrentPage.Handle, 3, KeyboardModifiers.ControlKey);
                break;
            case "V":
                Pdfium.FORM_OnChar(_viewer.FillForms.Handle, _viewer.CurrentPage.Handle, 22, KeyboardModifiers.ControlKey);
                break;
        }
    }

    public ClipboardCmd(PdfViewer viewer, string mode)
    {
        _viewer = viewer;
        _mode = mode;
    }
}

Edited by user Sunday, March 21, 2021 11:10:59 PM(UTC)  | Reason: Not specified

Quick Reply Show Quick Reply
Users browsing this topic
Guest
New Topic Post Reply
Forum Jump  
You can post new topics in this forum.
You can reply to topics in this forum.
You can delete your posts in this forum.
You can edit your posts in this forum.
You cannot create polls in this forum.
You can vote in polls in this forum.