PDFViewer WPF

Posted by: mayur.purandare on 28 August 2019, 8:12 pm EST

  • Posted 28 August 2019, 8:12 pm EST

    HI,

    Query 1:
    Is there any way to bind stream as input to PDFViewer. Is there any property to which i can bind my stream data to display contents.

    I know about source property but takes uri and not stream data. I don't have URI becz i am preparing data and want to view it.

    Instead of using LoadDocument method as we want to follow MVVM pattern.

    Query 2:

    I would like to show the C1PDFViewer Toolbar, but hide the save button.

    I have instantiated the viewer in XAML as below:
    <c1:C1PdfViewer Name="C1PDFViewerControl" />

    When i look to access the properties in C# code behind, all i can find is C1PDFViewerControl.ToolbarVisibility which either shows or hides the whole toolbar.

    Is there a way to show the toolbar but hide the save button in C# please?
  • Marked as Answer

    Replied 29 August 2019, 7:30 pm EST

    Hello Mayur,

    Sorry to mention that it is not possible to load stream into PDFViewer through the property. You need to use the "LoadDocument" method as given in your other query:
    https://www.grapecity.com/forums/wpf-edition/pdf-viewer_1


    >>Is there a way to show the toolbar but hide the save button in C# please?
    Yes, you can use the following code to hide the save button.

    public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
    if (depObj != null)
    {
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
    {
    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
    if (child != null && child is T)
    {
    yield return (T)child;
    }

    foreach (T childOfChild in FindVisualChildren<T>(child))
    {
    yield return childOfChild;
    }
    }
    }
    }
    int i = 0;
    private void Window_ContentRendered(object sender, EventArgs e)
    {
    foreach (StackPanel item in FindVisualChildren<StackPanel>(test))
    {
    if(i==0)
    item.Children[1].Visibility = Visibility.Collapsed;

    i++;
    }
    }


    Also, please refer to the attached sample for implementing the same.

    Thanks.

    PDFViewer.zip
  • Replied 22 September 2019, 5:57 pm EST

    Hello, Thanks for the reply.

    I have one query related to toolbar view of PDF Viewer control.

    Is there any way to bind short cut keys to buttons. For example next page, last page , previous page, etc.
  • Replied 22 September 2019, 9:16 pm EST

    Hello,

    You can use the KeyDown event of the viewer to achieve your requirement as follow:

     public MainWindow()
    {
    InitializeComponent();
    test.PreviewKeyUp += Test_PreviewKeyUp;
    }

    private void Test_PreviewKeyUp(object sender, KeyEventArgs e)
    {
    if(e.Key== Key.F7)
    {
    test.OpenCommand.Execute(test);
    }
    }

    Hope it helps.

    Thanks,
    Mohit
  • Replied 3 October 2019, 9:40 pm EST

    Hi,
    Thank you.

    Is there any sample application which I can refer to implement short cut keys.
    If yes please share it with me.
  • Replied 3 October 2019, 10:13 pm EST

    Hello,

    Please refer to the attached application.

    Thanks,
    Mohit

    PDfViewertest (2).zip
  • Replied 11 October 2019, 6:16 pm EST

    Hello,

    Thanks for the solution.
    I have implemented it successfully.

    Now I have 2 short cut keys for different function. Could you please let me know how to achieve this.

    When I pressed Alt + S then find textbox should be focused so that i can type string and search it.
    Similarly when I pressed Shift+Alt+P then PageNumber textbox should be focused so that i can type any page no and press enter.

    How to achieve this, is it possible to set focus on these textboxes.
  • Replied 14 October 2019, 4:26 pm EST

    Hello,

    Please use the following lines of code to achieve your requirement:

    public static T FindChild<T>(DependencyObject parent, string childName)
    where T : DependencyObject
    {
    // Confirm parent and childName are valid.
    if (parent == null) return null;
    T foundChild = null;
    int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < childrenCount; i++)
    {
    var child = VisualTreeHelper.GetChild(parent, i);
    // If the child is not of the request child type child
    T childType = child as T;
    if (childType == null)
    {
    // recursively drill down the tree
    foundChild = FindChild<T>(child, childName);
    // If the child is found, break so we do not overwrite the found child.
    if (foundChild != null) break;
    }
    else if (!string.IsNullOrEmpty(childName))
    {
    var frameworkElement = child as FrameworkElement;
    // If the child's name is set for search
    if (frameworkElement != null && frameworkElement.Name == childName)
    {
    // if the child's name is of the request name
    foundChild = (T)child;
    break;
    }
    }
    else
    {
    // child element found.
    foundChild = (T)child;
    break;
    }
    }
    return foundChild;
    }
    private void Test_PreviewKeyUp(object sender, KeyEventArgs e)
    {
    if ( Keyboard.IsKeyDown(Key.RightAlt) && e.SystemKey == Key.S)
    {
    C1.WPF.C1TextBoxBase item = FindChild<C1.WPF.C1TextBoxBase>(Viewer1, "Find");
    item.Focus();
    }

    else if ( Keyboard.IsKeyDown(Key.RightAlt) && e.SystemKey == Key.P)
    {

    C1.WPF.C1TextBoxBase item = FindChild<C1.WPF.C1TextBoxBase>(Viewer1, "PageNumber");
    item.Focus();


    }
    }
    }


    Thanks,
    Mohit
  • Replied 4 December 2019, 9:29 pm EST

    Dear Team,

    For C1PdfViewer, when I am keeping view mode as OnePage or FitWidth, it is getting displayed properly but One page or FitWidth buttons are not check.

    When the display setting of PDF Preview is default(Fit Width), this icon(Fit Width) is not selected.
  • Replied 8 December 2019, 5:33 pm EST

    Hello,

    Please change the ViewMode in the ContentRendered event of the MainWindow to achieve your requirements.
    private void Window_ContentRendered(object sender, EventArgs e)
    {
    pdfViewer.ViewMode = C1.WPF.PdfViewer.ViewMode.OnePage;

    }

    Thanks,
    Mohit
  • Replied 8 December 2019, 8:53 pm EST

    Thanks for the reply.
    It is working as per requirement.

    I have one more issue in this view.
    When the combo box/edit control(Search box or page no. control) of PDF Preview is selected and the short cut key is pressed, the page is moved.
    When editable controls are focused, short cut keys should not be allowed.
  • Replied 9 December 2019, 3:50 pm EST

    Hello,

    Please use the following modified method:

    private void Test_PreviewKeyUp(object sender, KeyEventArgs e)
    {
    C1.WPF.C1TextBoxBase find = FindChild<C1.WPF.C1TextBoxBase>(test, "Find");
    C1.WPF.C1TextBoxBase pageNumber = FindChild<C1.WPF.C1TextBoxBase>(test, "PageNumber");

    if (Keyboard.IsKeyDown(Key.RightAlt) && e.SystemKey == Key.S)
    {
    if(!pageNumber.IsFocused)
    find.Focus();
    }

    else if (Keyboard.IsKeyDown(Key.RightAlt) && e.SystemKey == Key.P)
    {

    if (!find.IsFocused)
    pageNumber.Focus();
    }
    }


    Hope it helps.

    Thanks,
    Mohit
Need extra support?

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

Learn More

Forum Channels