Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Word webkit
I try to show the clicked link of the browser in a label. Every time the user click on a link, the link is shown in the label, exept when i click on a *.doc, the value of the label dont change and nothing happen! I know It will be much easy with the default browser, i changed to web kit because it was ver laggy and it freeze the whole application when the user is surfing. can someone help me with this please??
 
private void webKitBrowser1_Navigated(object sender, WebKitBrowserNavigatedEventArgs e)
    {
        label1.Text = Convert.ToString(webKitBrowser1.Url);
    }
 
it seems that when i click on a .doc link nothin happen, like if i didn't click anywhere.. like if the .doc click is disabled in webKitBrowser and we should enable it
Posted 10-May-13 12:26pm
Nnorss521
Edited 10-May-13 13:09pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This is because you don't really navigate to *.DOC. You need to start using the debugger, by the way.
 
—SA
  Permalink  
Comments
Naourass Derouichi at 10-May-13 17:34pm
   
so i navigate to where? how can i use debugger?
Naourass Derouichi at 10-May-13 17:40pm
   
thanks for informing me about the debugger! but where do i navigate?
Naourass Derouichi at 10-May-13 18:09pm
   
i placed a stop point... it seems that when i click on a .doc link nothin happen, like if i didn't click anywhere.. like if the .doc click is disabled in webKitBrowser and we should enable it
Naourass Derouichi at 10-May-13 18:33pm
   
5 stars but it's not a solution :p
Sergey Alexandrovich Kryukov at 10-May-13 19:26pm
   
Thank you, I know... :-)
At least I explain you the problem. You should come back to your previous post. Also, there is a possibility to bite a bulled and develop presentation of Word document as HTML, instead of Google...
—SA
Naourass Derouichi at 10-May-13 19:35pm
   
i think you hate Google :D
Sergey Alexandrovich Kryukov at 10-May-13 19:45pm
   
Huh?
Naourass Derouichi at 10-May-13 20:21pm
   
it's just a joke :s
Sergey Alexandrovich Kryukov at 10-May-13 20:35pm
   
Ah... Good for Google. :-)
—SA
Naourass Derouichi at 10-May-13 19:43pm
   
here is the code to convert from doc to html:
using OpenXmlPowerTools;
using DocumentFormat.OpenXml.Wordprocessing;
 
byte[] byteArray = File.ReadAllBytes(DocxFilePath);
using (MemoryStream memoryStream = new MemoryStream())
{
memoryStream.Write(byteArray, 0, byteArray.Length);
using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true))
{
HtmlConverterSettings settings = new HtmlConverterSettings()
{
PageTitle = "My Page Title"
};
XElement html = HtmlConverter.ConvertToHtml(doc, settings);
 
File.WriteAllText(HTMLFilePath, html.ToStringNewLineOnAttributes());
}
}
 
but how can i retrieve the target link by clickin on it? what is the evenement of the click on a hyperlink in webbrowser?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

OK, I took some time and got the complete solution for you. It was easy and worked from the first attempt!
 
You just ignored some of my words. Did I tell you to use another event and cancel redirection to "href="https://docs.google.com/viewer?url=..."? Also, pay attention: this URL is HTTPS.
 
Now, I'll show how. This is a complete solution, all in code, without the use of the designer, to show it all in one file:
using System.Windows.Forms;
 
//...

public class MyForm : Form {
 
    const string viewer = "https://docs.google.com/viewer";
    const string viewerUrl = viewer + "?url={0}";
    const string docSample = "https://docs.google.com/viewer?url=http://www.gotw.ca/publications/C%2B%2BCLIRationale.pdf";
 
    WebBrowser browser = new WebBrowser(); // you probably do it in designer, but I want to show all in 1 file

    static bool IsDocumentToRedirect(string url) {
        return url.ToLower().EndsWith(".pdf");
        // you can also use .doc and .docx, I did PDF for my testing
    } //IsDocumentToRedirect

    public MyForm() {
        browser.Dock = DockStyle.Fill; // would be done in designer
        browser.Parent = this; // would be done in designer
        // commented out, to show that the designer is not used, 3 lines of code replace it:
        // InitializeComponent()
        browser.Navigating += (sender, eventArgs) => {
            string urlString = eventArgs.ToString();
            if (IsDocumentToRedirect(urlString) && !urlString.ToLower().StartsWith(viewer)) {
                eventArgs.Cancel = true;
                browser.Navigate(string.Format("{0},{1}", eventArgs.Url));
            } //if
        }; //browser.Navigating
    } //MyForm

    protected override void OnShown(EventArgs e) {
        // this is done just to simulate the click on anchor and test
        // redirection:
        this.browser.Navigate(docSample);
    } //OnShown

} //class MyForm
 
Please note that I uses System.Windows.Forms.WebBrowser. I simplified down the code, to show only one test, on form shown.
I hope you got the idea. The key it to handle the event when the navigation is only started and can be cancelled.
 
I hope you got the idea and can see what was missing from your code (your previous question).
 
Good luck,
—SA
  Permalink  
v2
Comments
Naourass Derouichi at 11-May-13 12:39pm
   
this work perfectly with webKitBrowser and .doc! infinite thanks and big respect!
Naourass Derouichi at 11-May-13 15:43pm
   
another thing to do is getting the right url, for exemple when i search on google for a doc filetype, it gives me an url lick that: {https://www.google.com/url?q=http://www.website.com/TRAQUEOS.doc&sa=U&ei=_Ye3yAfBQ&ved=0CBgAA&usg=AFXq9jj-5OX}, and unfortunatelly this type is not included in the toRedirect bool!..and also sometime there is empty spaces inside the link...
Sergey Alexandrovich Kryukov at 11-May-13 23:02pm
   
I described it in my answer to your previous question; it's really easy. It's like have an array { ".doc", ".docx" } (low-case) and iterate to check if there is one match with the actual URL...
—SA
Sergey Alexandrovich Kryukov at 11-May-13 23:04pm
   
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 11 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100