globbe

Hi All. A question on the DocumentViewer control. Is there a way to extract the currently selected text from a fixed document (like an XPS document) loaded into a DocumentViewer

Thank you!



Re: Windows Presentation Foundation (WPF) Get Selected text from Document Viewer

Benny Tops

Hi,

I don't think so, but I remember this is a new feature in 3.5 for a couple of WPF document viewers. I don't know if DocumentViewer is included.

New WPF 3.5 features:
http://msdn2.microsoft.com/en-us/library/bb613588(VS.90).aspx

Best regards,

Benny





Re: Windows Presentation Foundation (WPF) Get Selected text from Document Viewer

globbe

Thanks for the fast reply!

From the list it seems DocumentViewer is not included... Too bad.

(Can anyone from MS confirm this and the above reply )





Re: Windows Presentation Foundation (WPF) Get Selected text from Document Viewer

BIGDNF

There is a way to do this but this is by usinng trick with reflection:

Code Snippet

Type t = docViewer.GetType();

PropertyInfo inf = t.GetProperty("TextSelection", BindingFlags.Instance | BindingFlags.NonPublic);

TextSelection sel = inf.GetValue(docViewer, null) as TextSelection;

In this way you can get text but there is an error when you wont to get Start and End of Selectin becouse it seems that Selection in DocViewer is other type then TextSelection and this type is internal.

You can also try play with VisualTreeHelper and find TextSelection Smile





Re: Windows Presentation Foundation (WPF) Get Selected text from Document Viewer

globbe

Very interesting! Thank you!






Re: Windows Presentation Foundation (WPF) Get Selected text from Document Viewer

BIGDNF

I fight with this recently - maybe some know how to get Start and End of Selection Smile

I think that this is little odd that FlowDocuments have TextSelection property in 3.5 and docViewer don't - i will try to look on it with Reflecor and implement this on my own Smile





Re: Windows Presentation Foundation (WPF) Get Selected text from Document Viewer

globbe

If you have any luck. please keep me posted. I'll let you know if there is any progress on my end .





Re: Windows Presentation Foundation (WPF) Get Selected text from Document Viewer

Lakeside Developer

Has anyone found a solution to retrieve the selected content (or text) from a DocumentViewer control I was wondering if anyone has tried UI Automation to retrieve the selection





Re: Windows Presentation Foundation (WPF) Get Selected text from Document Viewer

globbe

I never tried that approach since my scenario involves a tablet Pc interface and there are known issues with using UI Automation on Tablets. However, if you do find a way, please post it as it may help others Smile.






Re: Windows Presentation Foundation (WPF) Get Selected text from Document Viewer

BIGDNF

I donĄ¯t know what exactly is your solution but I can give you couple advices wich a donĄ¯t have time to test J :

- You can get text element when mouse is over it in PreviewMouseMove of DocViewer event in e.OriginalSource with is of type Glyph

- You can search Visual tree for Selection Type Adorner or some kind of Glyph (to see entire tree use WPF Pref Tools from SDK). This function can be helpful:

Code Snippet

public static childItem FindVisualChild<childItem>(DependencyObject obj)where childItem : DependencyObject

{

for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)

{

DependencyObject child = VisualTreeHelper.GetChild(obj, i);

if (child != null && child is childItem)

{

return (childItem)child;

}

else

{

childItem childOfChild = FindVisualChild<childItem>(child);

if (childOfChild != null)

{

return childOfChild;

}

}

}

return null;

}

- You Can ask MS why they hide so important stuff Smile