zoom webBrowser when embedded by pdf - c#

Is there a way to zoom webbrowser when it loaded by pdf file.
I tried some code but it didn't work.
Like:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.Body.Style = "zoom:300%";
}
Also:
int zoomFactor = 300;
((SHDocVw.WebBrowser)webBrowser1.ActiveXInstance).ExecWB(SHDocVw.OLECMDID.OLECMDID_OPTICAL_ZOOM,
SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, zoomFactor, IntPtr.Zero);
This works when you open link like google.com etc. But in my case my web browser display PDF file.

Related

Entering text in website textbox using c#

I am trying to automate fill the textbox of a website in c# and i used:
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.WebBrowser webBrowser = new WebBrowser();
HtmlDocument document = null;
document=webBrowser.Document;
System.Diagnostics.Process.Start("http://www.google.co.in");
document.GetElementById("lst-ib").SetAttribute("value", "ss");
}
The webpage is opening but the text box is not filled with the specified value. I have also tried innertext instead of setAttribute. I am using windows forms.
You are expecting that your webBrowser will load the page at specified address, but actually your code will start default browser (pointing at "http://www.google.co.in"), while webBrowser.Document will remain null.
try to replace the Process.Start with
webBrowser.Navigate(yourUrl);
Eliminate the Process.Start() statement (as suggested by Gian Paolo) because it starts a WebBrowser as an external process.
The problem with your code is that you want to manipulate the value of your element too fast. Wait for the website to be loaded completely:
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.WebBrowser webBrowser = new WebBrowser();
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
webBrowser.Navigate("http://www.google.co.in");
}
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser.document.GetElementById("lst-ib").SetAttribute("value", "ss");
}
Please note that using a instance of a WebBrowser is not often the best solution for a problem. It uses a lot of RAM and has some overhead you could avoid.

Load a local webpage into the webbrowser control

I am trying to simply add a webbrowser control to a window and then have it open up a page. I tried a web URL as well as a local HTML file to no avail. Here is my code:
namespace qTab1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
FileStream source = new FileStream("index.html", FileMode.Open, FileAccess.Read);
webBrowser1.DocumentStream = source;
//// When the form loads, open this web page.
//webBrowser1.Navigate("www.google.com");
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
// Set text while the page has not yet loaded.
this.Text = "Navigating";
}
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
// Better use the e parameter to get the url.
// ... This makes the method more generic and reusable.
this.Text = e.Url.ToString() + " loaded";
}
}
}
This is my project at the moment:
What am I doing wrong?
The reason this happens is that when you press Debug or build your project any other way the root directory is the directory of the executable (so it would be - ./bin/Debug), not the directory of the project.
To fix this, you can do the following:
Right click the html file, click Properties and set the variable "Copy to output directory" to Copy always. That way, the html file will get copied with your executable.
Now you have to load the local file into the WebBrowser control. The following should work :
webBrowser1.Url = new Uri("file:///" + Directory.GetCurrentDirectory() + "/index.html");

WebBrowser PrintPreviewDialog Size

I have a WebBrowser
private WebBrowser wb = new WebBrowser();
and when I click on a Button, it should open a PrintPreviewDialog and show me the WebBrowser.
So when the user clicks on a button, this code is being executed:
private void PrintPreviewCommandExecute()
{
wb.DocumentText = ReportPage;
wb.ClientSize = new System.Drawing.Size(450000, 750000);
wb.MinimumSize = new System.Drawing.Size(450000, 750000);
wb.Size = new System.Drawing.Size(450000, 750000);
wb.DocumentCompleted += wb_PreviewDocumentCompleted;
}
void wb_PreviewDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
wb.ShowPrintPreviewDialog();
}
ReportPage contains a HTML string. As you can see, I'm trying to set the Dialog size somehow (with ridiculous big numbers...), but the window size is not being changed.
So when the Dialog is being shown, it looks like this:
http://i.imagebanana.com/img/s474xiyk/Unbenannt.png
How can I change the size of the PreviewDialog (in the top left corner)?

C# WebBrowser.ShowPrintDialog() not showing

I have this peculiar problem while wanting to print a html-report. The file itself is a normal local html file, located on my hard drive.
To do this, I have tried the following:
public static void PrintReport(string path)
{
WebBrowser wb = new WebBrowser();
wb.Navigate(path);
wb.ShowPrintDialog()
}
And I have this form with a button with the click event:
private void button1_Click(object sender, EventArgs e)
{
string path = #"D:\MyReport.html";
PrintReport(path);
}
This does absolutely nothing. Which is kind of strange... but things get stranger...
When editing the print function to do the following:
public static void PrintReport(string path)
{
WebBrowser wb = new WebBrowser();
wb.Navigate(path);
MessageBox.Show("TEST");
wb.ShowPrintDialog()
}
It works. Yes, only adding a MessageBox. The MessageBox is showing and after it comes the print dialog. I have also tried with Thread.Sleep(1000) instead, which doesn't work. Can anyone explain to me what's going on here? Why would a messagebox make any difference?
Can it be some kind of permission problem? I've reproduced this on both Windows 7 and 8, same thing. I made this small application with only the above code to isolate the problem. I am quite sure it works on windows XP though, since an older version of the application I'm working on runs on it. When trying to do this directly with the mshtml-dll instead I also get problems.
Any input or clarification is greatly appreciated!
The problem is that the browser is not ready to print yet. You will want to add an event handler WebBrowserDocumentCompletedEventHandler to the WebBrowser Object. Sample code below.
public static void PrintReport(string path)
{
WebBrowser wb = new WebBrowser();
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
wb.Navigate(path);
}
public static void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = (WebBrowser)sender;
if (wb.ReadyState.Equals(WebBrowserReadyState.Complete))
wb.ShowPrintDialog();
}

Using custom image buttons to display an image in RichTextBox

I am creating a text editor much like notepad/word and i have buttons on my form with custom images attached. The images have been loaded into the resource folder. Now when i click the button i want it to display that same image in the RichTextBox, the images being displayed will be various emoticons. I only found ways to create picture viewers and not this.
private void button11_Click(object sender, EventArgs e)
{
Image.FromFile("../../Resources/sad-icon.png");
}
This will be used to place a sad emoticon in the RichTextBox. This is what i have so far i am pretty new to C#.
I have been trying various methods and i found one that works but only with certain emoticons and not the emoticons that i need to be able to use.
void InsertEmoticon(string ImageName)
{
int StringLength = 0;
StringLength = txtPAD.Text.Length;
Image img = Image.FromFile("../../Resources/Emoticons/" + ImageName + ".png");
Clipboard.SetImage(img);
txtPAD.SelectionStart = (StringLength + 1);
txtPAD.Paste();
txtPAD.Clear();
}
I will then use:
private void button10_Click(object sender, EventArgs e)
{
InsertEmoticon("glad");
}
Am i missing something here? The images have been loaded into the resource folder and are inside the folder Emoticons.

Categories