PDF Viewer: Logging user actions

Posted by: lcolombo on 9 September 2017, 8:29 am EST

  • Posted 9 September 2017, 8:29 am EST

    How I can generate an audit record when a user clicks on the print button?

    Regards
  • Replied 9 September 2017, 8:29 am EST

    Hello,

    You can find the print button of PdfViewer and can manually create the log by handling its click event. Here is the code to access the print button:
     partial void PdfViewerScreen_Created()
    {
    this.ViewerDataLocation = @"C:UsersPublicTest.pdf";
    IContentItemProxy px = this.FindControl("C1PdfViewer");
    px.ControlAvailable += new EventHandler<ControlAvailableEventArgs>(px_ControlAvailable);
    }

    void px_ControlAvailable(object sender, ControlAvailableEventArgs e)
    {
    var pdfviewer = e.Control as C1PdfViewer;
    var border = VisualTreeHelper.GetChild(pdfviewer, 0);
    var toolbar = VisualTreeHelper.GetChild(((System.Windows.Controls.Border)(border)).Child, 0);

    IList<DependencyObject> list = new List<DependencyObject>();
    VTreeHelper.GetChildrenOfType(toolbar, typeof(Button), ref list);
    //index of print button is 3 so we can directly access it and can subscribe its click event
    (list[2] as Button).Click += new RoutedEventHandler(PrintButton_Click);
    }

    void PrintButton_Click(object sender, RoutedEventArgs e)
    {
    //create log manually
    }


    Hope it helps.

    Regards
  • Replied 9 September 2017, 8:29 am EST

    Ashish,
    Thanks for the reply.

    I added the following lines to compile:

    using C1.Silverlight;
    using C1.LightSwitch.PdfViewer.Presentation.Controls;
    using System.Windows.Media;
    using System.Windows;
    using System.Windows.Controls;

    But when entering the screen throws me the following error:

    "The reference is not a valid visual DependencyObject."


    Regards
  • Replied 9 September 2017, 8:29 am EST

    Hello
    I modified the following line:

    var pdfviewer = e.Control as C1PdfViewer;


    by

    var pdfviewer = e.Control as DependencyObject;


    and it worked.

    thank you very much!

    note: index of print button is 1
  • Replied 9 September 2017, 8:29 am EST

    Ok...so I'm facing the same problem as lcolombo....but my code isn't working....


    partial void PdfViewerScreen_created()
    {
    IContentItemProxy px = this.FindControl("PDFFile");
    px.ControlAvailable += new EventHandler<ControlAvailableEventArgs>(px_ControlAvailable);
    }
    void px_ControlAvailable(object sender, ControlAvailableEventArgs e)
    {
    var pdfviewer = e.Control as DependencyObject;
    // C1PdfViewer;
    var border = VisualTreeHelper.GetChild(pdfviewer, 0);
    var toolbar = VisualTreeHelper.GetChild(((System.Windows.Controls.Border)(border)).Child, 0);

    IList<DependencyObject> list = new List<DependencyObject>();
    VTreeHelper.GetChildrenOfType(toolbar, typeof(Button), ref list);
    //index of print button is 3 so we can directly access it and can subscribe its click event
    (list[1] as Button).Click += new RoutedEventHandler(PrintButton_Click);

    }

    void PrintButton_Click(object sender, RoutedEventArgs e)
    {
    var user = getUserInformation();
    var tempDataWorkSpace = new DataWorkspace();
    var itemAudit = tempDataWorkSpace.DbBussines.AuditSet.AddNew();
    itemAudit.User = user.UserName;
    itemAudit.PersonID = user.ID.ToString();
    itemAudit.Item = "Document print";
    itemAudit.AuditInformation = "DocumentPrint";
    try
    {
    tempDataWorkSpace.DbBussines.SaveChanges();
    }
    catch (Exception ex)
    {
    this.ShowMessageBox(ex.Message + " - " + ex.InnerException);
    }

    }


    I get the error:

    It is not valid to execute the operation on the current thread

    The problem is located in the SaveChanges line, but I don't know why.

    I also try calling Application.Current.Details.Dispatcher.BeginInvoke and this.Details.Dispatcher.BeginInvoke.

    Do you have any idea why I'm getting this error?

    Also, I have to DobleClick on the print button... ┬┐why? The first click does'nt do anything at all.


    Regards,

    Gabriel
  • Replied 9 September 2017, 8:29 am EST

    Hello Gabriel,

    In the PrintButton_Click event, you may try saving the details inside Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke method.

    Regards
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels