Loading a website inside WPF - c#

I've been playing with loading a website (specifically my blog) inside a WebBrowser in WPF. My code is pretty much just one line:
WebBrowser.Source = new Uri("http://www.example.net");
When I run this I get scripting error, like this:
I get similar errors when running other web sites, although not always related to Google Ads.
My assumption is that there is an issue with sites that run JS. Is this correct and, if so, why? Is there a way around it?

The problem here is that the WPF WebBrowser did not implement this property as in the 2.0 control. But with reflection you can get to the activexinstance of the webbrowser control to put the browser in silent mode:
dynamic activeX = this.WB.GetType().InvokeMember("ActiveXInstance",
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, this.WB, new object[] { });
activeX.Silent = true;

Related

SAP Gui Scripting | .NET | Start transaction with parameters

I have the following script, which allows me to to control the SAP Gui with .NET (C#)
SapROTWr.CSapROTWrapper sapROT = new SapROTWr.CSapROTWrapper();
object objSapGui = sapROT.GetROTEntry("SAPGUI");
object objEngine = objSapGui.GetType().InvokeMember("GetScriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, objSapGui, null);
GuiConnection connection = (GuiConnection)(objEngine as GuiApplication).Connections.Item(0);
SapSession = (GuiSession)connection.Sessions.Item(0);
SapSession.SendCommand("/nN204");
As i figured out, i can start a transaction within the SAP Gui with GuiSession.StartTransaction("N204") or with GuiSession.SendCommand("/nN204")
Now I want to start the transaction with a parameter structure to go directly into a specific for example document at this point. How can I achieve this?
My first thought was the GuiApplication.CreateGuiCollection() but as I do not found any examples, I do not now what to do with it.
It would be nice if someone could share a approach to achieve my goal.

C# Download a complete weired HTML page

I'm sorry if this question has allready been answered , but I litterally spent more than two weeks searching the Internet for a solution to my issue.
Now , I definitly do not perform the best google searches , and it might seem that my question has several effective answers on the Internet. but I really tried every single solution that I found , without any positive results.
What i'm trying to do is simple , and I did it successfully on many websites :
Navigating to a website using WebBrowser (1).
Waiting for everything to load properly (document completed event).
Download the page using DocumentText property (1).
(1) : I also use WebClient from time to time.
And there it is , I get the html page , and I can exploit it anyway I like. The issue is with a particular website that I cannot obtain the full content inspite of using all the different solutions that I found. I suspected the fact that this page might need to load several scripts before getting the full content. Yet again, I read that WebBrowser does run all the necessarry scripts before triggering the " completed " event, so , apparently , that's not the issue. The page that i'm inquiring about is : http://www.coolmod#com/tarjetas-graficas-nvidia-pci-express
I tried , after that the WebBrowser loads the entire page , looking for random elements using GetElementByID property and checking if I get a null result. It appears that when I try getting an element that does not belong to the products list , i'm successful. But , whenever I try to get an element that belongs to the list it self , I always get a null. Which means , the list it self does not load. and I really don't know why. By the way , I do not prevent the WebBrowser. Navigate () from delivering multiple responses , I allow it to give as many feedbacks as possible , and still , the product list does not load , even when I pass the cookies. I Even tried copying all the content of the document and pasting it through the clipboard. Here is a simple example of what I try to do :
private void catalogueDownload()
{
System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();
wb.ScriptErrorsSuppressed = true;
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(Catalogue_DocumentCompleted);
wb.Navigate("http://www.coolmod.com/tarjetas-graficas-nvidia-pci-express");
}
public void Catalogue_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var wb = sender as System.Windows.Forms.WebBrowser;
string output = wb.DocumentText;
File.WriteAllText("testing.html", output);
}
Thanks for giving up your time to read all this.
System.Windows.Forms.WebBrowser is a bit outdated, If I were you, I would consider using an external library for that, Selenium would be my 1st choice, given it has all the necessary integrations with .NET Framework (and a lot of other languages)

Enabling annotation in Adobe AxAcroPDFLib

I embedded a PDF viewer in a C# Winform using AxAcroPDFLib.
However, the annotation buttons in the toolbar (comments...) are disabled.
I searched and found that they are disabled by default, but some reported enabling them using Javascript:
Collab.showAnnotToolsWhenNoCollab = True
Is there a way to do this here?
Edit: Is it possible to use the browser plugin in a WebBrowser Control? If so, how can this be done?
Update - The first section is relevant only to Acrobat Reader. For information on when using full versions of Acrobat, see the second section.
Acrobat Reader
I'll preface all of this by stating this is probably not the answer you're looking for, but I felt this warranted more of an explanation than just a comment.
A similar, self-answered question was asked on SO (here), where the OP came to the conclusion that this behavior is by design and nothing cannot be done about it, which I agree with, almost.
While I'm sure you've seen that Reader itself can add annotations, the only straightforward means of accomplishing this using the Reader Plugin (AcroPDFLib) is for the document being loaded to be "Reader Enabled," at which point annotations become available just as they are in Reader. If you have control of the documents you wish the plugin to load, this may be a solution for you.
To your question about possibly setting Collab.showAnnotToolsWhenNoCollab = True as a workaround, my searches only showed this being a viable workaround for those using a full version of Acrobat, not Reader. More specifically, on an Adobe forum (here), an Adobe staff commented on the use of this property directly:
No, it is not [about allowing commenting in Adobe Reader]. It is
about enabling commenting in a browser for Acrobat Standard or
Professional. If you wish to enable commenting in Reader, then you
need to "Reader Enable" the PDFs themselves using Acrobat professional
or Adobe Livecycle Reader Extension Server.
Granted, this comment was in reference to Acrobat 9, it appears to still be valid for Acrobat XI.
One last bit. I don't know the scope of your application, so this may be completely irrelevant, but if this is a commercial application, even if do you find a functional workaround, I'd be hesitant to use it, as it might violation the Adobe Reader license agreement (here); specifically section 4.3.3, Disabled Features. The short version is, as with most companies, they don't want you circumventing their protections.
Full versions of Acrobat
The following code will create a PDF viewer (using the Form's window for drawing), open a PDF, then set collab.showAnnotToolsWhenNoCollab = true to allow annotations on the open PDF. This requires a reference to the Acrobat type library.
void CreatePdfViewerAndOpenFile(string pdfFile)
{
short AV_DOC_VIEW = 2;
short PDUseBookmarks = 3;
short AVZoomFitWidth = 2;
Type AcroExch_AVDoc = Type.GetTypeFromProgID("AcroExch.AVDoc");
_acroExchAVDoc = (Acrobat.CAcroAVDoc)Activator.CreateInstance(AcroExch_AVDoc);
bool ok = _acroExchAVDoc.OpenInWindowEx(pdfFile, this.Handle.ToInt32(), AV_DOC_VIEW, -1, 0, PDUseBookmarks, AVZoomFitWidth, 0, 0, 0);
if (ok)
{
CAcroPDDoc pdDoc = (CAcroPDDoc)_acroExchAVDoc.GetPDDoc();
object jsObj = pdDoc.GetJSObject();
Type jsObjType = jsObj.GetType();
object collab = jsObjType.InvokeMember("collab",
BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance,
null, jsObj, null);
jsObjType.InvokeMember("showAnnotToolsWhenNoCollab",
BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance,
null, collab, new object[] { true });
}
}
Call this method from wherever you want to display the PDF. When finished, be sure to call the Close method or the PDF file will remain open in the Acrobat process in the background.
_acroExchAVDoc.Close(-1);
Bear in mind that a lot of "normal" functionality is left out of this example, like form resize handling, etc., but it should get you started. Because resizing isn't handled by this example, you'll probably want to maximize the form before invoking the method, so the viewer is big enough to be useful. For more information on how to use the viewer in this fashion, download the Acrobat SDK (here) and look at the ActiveViewVB sample project, which is what I used to build some of this example. For reference, I used the Acrobat XI SDK.

GeckoFX - implement search function - how to use nsIWebBrowserFind interface

Could anyone perhaps tell me how should I use the nsIWebBrowserFind interface in GeckoFX to find strings on a webpage?
I tried the following code, but this throws me an ArgumentNullException - parameter cannot be null (pUnk).
I have no idea what this means, I have never used interfaces before.
GeckoWebBrowser browser = getCurrentBrowser();
nsIWebBrowserFind finder = browser.GetInterface<nsIWebBrowserFind>();
finder.SetSearchStringAttribute(searchBox1.Text);
finder.FindNext();
I have also tried
nsIWebBrowserFind finder = Gecko.Xpcom.GetInterface<nsIWebBrowserFind>(browser);
With the same results:(
Please help:)
Thanks!
This works in GeckoFX 29.0:
var field = typeof(GeckoWebBrowser).GetField("WebBrowser", BindingFlags.Instance | BindingFlags.NonPublic);
nsIWebBrowser browser = (nsIWebBrowser)field.GetValue(webBrowser1);
var browserFind = Xpcom.QueryInterface<nsIWebBrowserFind>(browser);
browserFind.SetSearchStringAttribute(search);
try
{
browserFind.SetWrapFindAttribute(true);
browserFind.FindNext();
}
catch { }
Some websites with frames throws an exception after the last result however, this seems to be a bug in GeckoFX. We did a workaround by falling back to a javascript find (javascript:window.find) when the exception was thrown.

JavaScript to upload to server

I am writing an ASP.NET script to capture a signature, I want to save the file on the server side.
The following code is JavaScript for saving the file. But it does not allow me to save from browser disable by safety measure:
flags = 0x2481000; //SigObj.renderRelative | SigObj.outputFilename | SigObj.color32BPP | SigObj.encodeData
rc = sigCtl.Signature.RenderBitmap(filename, -160, -160, "image/bmp", 0.5, 0xff0000, 0xffffff, 0.0, 0.0, flags );
How can I upload via JavaScript or C#?
If you do not find it to be overkill, for your uses, i suggest trying out https://github.com/blueimp/jQuery-File-Upload, i do think it is possible to adapt for use with C#. There is quite an extensive documentation for the plugin.
It is based on jQuery UI.
Cheers Kalle

Categories

Resources