I'm trying to get a selected line of a pdf document which i've displayed in Webbrowser control (c# winform)
This is my code
IHTMLDocument2 htmlDocument = webBrowser1.Document.DomDocument as IHTMLDocument2;
IHTMLSelectionObject currentSelection = htmlDocument.selection;
if (currentSelection != null)
{
IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
if (range != null)
{
MessageBox.Show(range.text);
}
}
i get the following output
It doesn't seem to work as i'm getting an empty message box. I've got to know online that in order to edit/highlight text, i might have to use an SDK!
I'd really appreciate any kind of help/guidence here!
Related
I'm trying to implement a find and highlight function using webkitbrowser (instead of default c# webbrowser).
Basically I need to do something like the Ctrl + F functionality.
I had something working in default webbrowser (example below), but this doesn't work in webKitBrowser.
IHTMLDocument2 doc = (IHTMLDocument2)this.webBrowser1.Document;
IHTMLSelectionObject sel = (IHTMLSelectionObject)doc.selection;
sel.empty();
IHTMLTxtRange rng = (IHTMLTxtRange)sel.createRange();
if (rng.findText(text, 1000000000, 0))
{
rng.select();
return true;
}
return false;
Thanks all in advance!!
I am trying to white an application that gets a url and find out all html5 canvas advertisement (i found out that iframes is the best HTMLElement that I can get) and take a video screenshot of the element.
I have to render the page (because it uses javascript calls to create the final page) using an in memory webbrowser control and then I find all iframes from document of the Webbrowser control (objDoc in the sample).
var aColl = from HtmlElement p in objDoc.Body.All
where Configuration.lstCanvas.Contains(p.TagName.ToUpper())
select p;
For each element I have to capture a screenshot and a video of the content of the iframe or even an html file that can represent the video later.
In order to capture the image I use the following code (if i pass null to HTMLelement I get a full screen capture) as an extended metod of the webbrowser control.
public static Image GetElementImage(this WebBrowser web, HtmlElement elm = null)
{
try
{
IHTMLDocument2 idoc2 = web.Document.DomDocument as IHTMLDocument2;
if (idoc2 == null)
{
return null;
}
IHTMLElementCollection elementCollection = idoc2.all;
if (elementCollection == null)
{
return null;
}
IHTMLElementRender iHTMLElementRender;
if (elm != null)
{
iHTMLElementRender = (IHTMLElementRender)elm.DomElement;
if (iHTMLElementRender == null)
{
return null;
}
}
else {
iHTMLElementRender = idoc2.body as IHTMLElementRender;
if (iHTMLElementRender == null)
{
return null;
}
}
// get the location and size of the element and create a bitmap
// need a different interface
IHTMLElement element = iHTMLElementRender as IHTMLElement;
if (element == null)
{
return null;
}
int elementWidth = element.offsetWidth;
int elementHeight = element.offsetHeight;
// Create a bitmap and render the element to it.
Bitmap memoryBitmap = new Bitmap(elementWidth, elementHeight, PixelFormat.Format24bppRgb);
Graphics memGraphics = Graphics.FromImage(memoryBitmap);
IntPtr memDC = memGraphics.GetHdc();
iHTMLElementRender.DrawToDC(memDC);
memGraphics.ReleaseHdc(memDC);
Image r = (Image)memoryBitmap.Clone();
memGraphics.Dispose();
memoryBitmap.Dispose();
return r;
}
catch // (Exception ex)
{
return null;
}
}
I haven't found a way to take a video capture of the element.
I try to grab the content of the iframe as html (in order to save it and use it for latter) but I come out with some issues:
The url is not always an attribute of the iframe (it changes from the javascript)
Cross Domain scripting is not letting me to gram the html
The file that I download is not allways reproduces the content of the iframe
Do you have any suggestion either on getting the html code or the video?
Thanks in advance
I am adding hyperlink to a RichTextEditor (link). But now I want to get the Hyperlink back when user selects the hyperlink. And here I meant I want the Hyperlink element not just the text.
What I'm trying to do is to create a pop-up like google docs for hyperlink. I can add the hyperlink just fine. But now I need to modify the existing link. Getting the display text is easy but to get the address of the link is tricky. And I don't want the whole paragraph and then look for the link because there can be multiple links in one paragraph.
I tried richtextbox.Selection.Start.GetAdjacentElement(Forward/Backward) but it's not always returning the hyperlink. Is there any way to get just the selected element/hyperlink?
Maybe, some helps are here ^^
http://blogs.msdn.com/b/prajakta/archive/2006/10/17/autp-detecting-hyperlinks-in-richtextbox-part-i.aspx
Based on reference, I try to find NavigateUri
public static Hyperlink GetHyberLink(this TextPointer pointer)
{
if (pointer == null)
{
return null;
}
Inline parent = pointer.Parent as Inline;
while (parent != null && !(parent is Hyperlink))
{
parent = parent.Parent as Inline;
}
return parent == null ? null : (Hyperlink)parent;
}
Using:
Hyperlink hyperlink = RichTextBox.Selection.Start.GetHyberLink();
TextRange textRange = new TextRange(hyperlink.ElementStart, hyperlink.ElementEnd);
textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
Result:
The NavigateUri will be marked as red.
So this is what I end up doing to get the hyperlink.
Hyperlink GetHyperlinkAtSelection()
{
var selectedPointer = rtb.Selection.GetNextInsertionPosition(forward)
if(sp == null)
return;
var para = sp.Paragraph;
var hyperlink = para.Inlines.FirstOrDefault(x =>
x.ContentStart.CompareTo(sp) == -1 && x.ContentEnd.CompareTo(sp) == 1);
return hyperlink as Hyperlink;
}
to get hyperlink at caret position just change the first line of the method.
Here is my web browser:
<WebBrowser viewmodel:BrowserBehavior.Html="{Binding SelectedNode.ContentData.FileName, Converter={StaticResource converter}, Mode=OneWay}" />
In fact, text in my WebBrowser Control can be Selected.
I wonder if I can retrieve the Selected portion in a string object?
PS: When right-clicking the Selected portion , I'd noticed that user can copy selected text. So my second question will be "Can we get the copied text?" I mean the copied portion should be saved somewhere in some environment variable, can we get it in c#?
You cannot bind selected text from WebBrowser but you can get it manually like so:
var doc = webBrowser.Document as mshtml.HTMLDocument;
if (doc != null)
{
var currentSelection = doc.selection;
if (currentSelection != null)
{
var selectionRange = currentSelection.createRange();
if (selectionRange != null)
{
var selectionText = selectionRange.Text;
//do something with selected text
}
}
}
where webBroswer is your browser control
<WebBrowser x:Name="webBroswer" ... />
but you'll need to add reference to Microsoft.mshtml and to answer your second question you can get copied text from clipboard with Clipboard class
var copiedText = Clipboard.GetText();
I made MVVM application using DevExpress and WPF. To show data NamesView.xaml View will be called in Document Tab Provided by DevExpress. Code is:
void ShowDocument(Int32? key)
{
if(DocumentManagerService == null)
return;
IDocument document = (key != null && key.HasValue) ?
GetDocumentById(key.Value) : null;
if(document == null)
{
document = DocumentManagerService.CreateDocument("NamesView", key, this);
}
document.Show();
}
There is a PanelB in NamesCollectionView.xaml file.How to Show "NamesView.xaml" in Panel(named as PanelB defined in NamesCollectionView.xaml file rather than showing in Document Tab?. When user will Click on Edit or new button, NamesView file will get open in response.I want that this file to show in PanelB.
Note: I f my Question is not clear or if you need some more code to figure it out. Please ask me, i will provide you more detail.