Load local html file in webbrowser control in editable mode - c#

I want to load a local html file in webbrowser control in editable mode. My code for loading in html file in webBrowser is:
private void LoadFile(string filename)
{
using (StreamReader reader = File.OpenText(filename))
{
webBrowser1.DocumentText = reader.ReadToEnd();
reader.Close();
Text = webBrowser1.DocumentTitle;
}
}
But it is not editable.

Please check this snippet.
webBrowser1.AllowWebBrowserDrop = false;
webBrowser1.Url = new Uri(#"D:\TEMP\sample.htm");

Related

MigraDoc: Rendering the same document twice

I want to render a "Document"-object twice, but on the second generation the PDF-File shows an error:
Error on this page. Maybe this page couldn't appear correctly. Please talk to the document-creator.
Sorry, it is german...
This is my example-code:
using System;
using System.Windows.Forms;
using MigraDoc.Rendering;
using MigraDoc.DocumentObjectModel;
using PdfSharp.Pdf;
using System.Diagnostics;
namespace Temp
{
public partial class Form1 : Form
{
Document document; //using the same document- and renderer-object ..
PdfDocumentRenderer renderer; //..creates the error
public Form1()
{
InitializeComponent();
document = new Document();
renderer = new PdfDocumentRenderer(false, PdfFontEmbedding.Always);
}
private void button1_Click(object sender, EventArgs e)
{
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
paragraph.AddFormattedText("Hello World", TextFormat.Bold);
SaveFile(renderer);
}
private void SaveFile(PdfDocumentRenderer renderer)
{
renderer.Document = document;
renderer.RenderDocument();
string pdfFilename = string.Format("Rekla-{0:dd.MM.yyyy_hh-mm-ss}.pdf", DateTime.Now);
renderer.PdfDocument.Save(pdfFilename);
Process.Start(pdfFilename);
}
}
}
Of course i could always create a new "Document"-object:
private void button1_Click(object sender, EventArgs e)
{
Document document = new Document(); //Creating a new document-object solves the problem..
PdfDocumentRenderer renderer = new PdfDocumentRenderer(false, PdfFontEmbedding.Always);
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
paragraph.AddFormattedText("Hi", TextFormat.Bold);
SaveFile(renderer, document);
}
This works. But i need to change the current document while performing other button-click-events. The second code-snippet doesn't solve this task.
Does someone know how to fix the first code-snippet?
Do not re-use the Renderer. Create a new Renderer in the SaveFile method and everything should be fine.
If a new Renderer is not enough, call document.Clone() and assign that to the Renderer.
A method I have found very useful for repeatedly saving a Migradoc pdf to a file is to save the document to a MemoryStream/byte array when I render it. Then when I need to save the pdf file, I just save the byte array.
Put this part in your constructor where you want to render the document:
// First we render our document and save it to a byte array
bytes[] documentBytes;
Document doc;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
PdfDocumentRenderer pdfRender = new PdfDocumentRenderer();
pdfRender.Document = doc;
pdfRender.RenderDocument();
pdfRender.Save(ms, false);
documentBytes = ms.ToArray();
}
Put this part in your button event where you want to save to a file:
// Then we save the byte array to a file when needed
string filename;
using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Create))
{
fs.Write(documentBytes, 0, documentBytes.Length);
}

XML.save doesn't update my document

I'm writing a program that will allow users to upload a photo, add a caption, and save their work. When saving, they will be updating an telerik-rotator that has a xml data source
I have the part where the user can upload a photo working. When they upload a photo, it puts the new photo in the images folder. And the telerik control works because Default.aspx has the carousel working.
The part I'm struggling with is allowing the user to update dataSource.xml
Here is my code behind.
protected void submit_Click(object sender, EventArgs e)
{
if(NameInput.Text == "")
{
generalError.Text = "All fields must be completed";
}
try
{
//create variables to replace xml
string photoString = photoDropDown.SelectedItem.Text;
string caption = NameInput.Text.ToString();
string location = HttpContext.Current.Server.MapPath("dataSource.xml");
XmlDocument xml = new XmlDocument();
xml.Load(location);
XmlElement newElement = xml.CreateElement("slide");
newElement.InnerXml = "<image>" + photoString + "</image><text>" + caption + "</text>";
xml.DocumentElement.SelectNodes("/rotator")[0].AppendChild(newElement);
xml.PreserveWhitespace = true;
xml.Save("dataSource.xml");
}
catch (Exception ex)
{
generalError.Text = ex.ToString();
generalError.Visible = true;
return;
}
generalError.Text = "It worked!";
generalError.Visible = true;
}
This throws no errors. So xml.Save is saving something. But it is not appending the new node to the xml document. What am I doing wrong?

How to display an image from file system in a PictureBox in WinForm

I have a small doubt regarding loading an image in PictureBox in WinForms.
I want to show an image file from file system in a PictureBox on my form, say form1.
I am doing Windows applications using C#.
I want to check the file type also say is it pdf/text/png/gif/jpeg.
Is it possible to programmatically open a file from file system using C#?
If anyone knows please give any idea or sample code for doing this.
Modified Code: I have done like this for opening a file in my system, but I don't know how to attach the file and attach the file.
private void button1_Click(object sender, EventArgs e)
{
string filepath = #"D:\";
openFileDialog1.Filter = "Image Files (*.jpg)|*.jpg|(*.png)|*.png|(*.gif)|*.gif|(*.jpeg)|*.jpeg|";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
try
{
}
}
}
I don't know what I have to write in try block. Can anyone help on this?
Use Image.ImageFromFile http://msdn.microsoft.com/en-us/library/system.drawing.image.fromfile.aspx method
Image img = Image.ImageFromFile(openFileDialog1.FileName);
Should work.
EDIT
If you're going to set it to PictureBox, and what to see complete inside it, use picturebox
SizeMode property.
using System.IO;
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = false; //not allow multiline selection at the file selection level
openFileDialog1.Title = "Open Data file"; //define the name of openfileDialog
openFileDialog1.InitialDirectory = #"Desktop"; //define the initial directory
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
try
{
string filename = openFileDialog1.FileName;
FileStream fs=new FileStream(filename, FileMode.Open, FileAccess.Read); //set file stream
Byte[] bindata=new byte[Convert.ToInt32(fs.Length)];
fs.Read(bindata, 0, Convert.ToInt32(fs.Length));
MemoryStream stream = new MemoryStream(bindata);//load picture
stream.Position = 0;
pictureBox1.Image = Image.FromStream(stream);
}
}

i want to display text from textfile in text box . how can i do this .. in C#

i want to display text from textfile in text box . how can i do this .. in C#
Actually i m making text to speech converter in C# .. SO i want to open text file and want show text of that file in my textbox ..
here is my code
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog O = new OpenFileDialog();
O.ShowDialog();
Loadfile(O.FileName);
}
private void Loadfile(string filename)
{
TextRange range;
FileStream fStream;
if (File.Exists(fileName))
{
range = new TextRange(textBox1.Text.TrimStart, textBox1.Text.TrimEnd);
fStream = new FileStream(filename, FileMode.Open);
range.Load(fStream, DataFormats.Text);
fStream.Close();
}
}
i got error in textBox1.Text.TrimStart, textBox1.Text.TrimEnd .. i dont want to use Richtextbox because .. for that i have to use . Document property of richtextbox cz 4 tht i bound to use WPF ...
(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd)
Please Help me on this
Cheers !
Wahib Idris
Any help will be appreciated .. Thanx in advance
Please Help
This should work:
private void Loadfile(string filename)
{
if (File.Exists(fileName))
{
textBox1.Text = File.ReadAllText(filename);
}
}
var fileText = File.ReadAllText(filePath);
textBox.Text = fileText;
You can load content of file to string simply this way:
private string Loadfile(string filePath)
{
string text = String.Empty;
if (File.Exists(filePath))
{
StreamReader streamReader = new StreamReader(filePath);
text = streamReader.ReadToEnd();
streamReader.Close();
}
return text;
}
The easiest way:
if (File.Exists(filePathString))
yourTextBox.Text = File.ReadAllText(filePathString);

Work with textBox(s) and writing in to a File and read it back from File

Can someone tell me how to;
work with textBox(s) and writing the information in it to a file and read them back from the file(.txt file)
Thanks.
ps: I want to write some text in textbox (winforms) and when i click button save all the texts in all textboxs write to a file
Daniel
That is pretty vague, but string txt = File.ReadAllText(path); and File.WriteAllText(path,txt); should handle the file part (for moderately sized files).
The .Text property of a TextBox contains the text within the text box. You can get or set this property to acquire or alter the text within the TextBox as needed. Take a look at File.WriteAllText and File.ReadAllText to read/write text from/to a file.
Write:
FileStream fs = new FileStream("test.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(txtTest.Text);
sw.Close();
Read:
FileStream fs = new FileStream("test.txt", FileMode.OpenOrCreate);
StreamReader sr = new StreamReader(fs);
string myText = string.Empty;
while (!sr.EndOfStream)
{
myText += sr.ReadLine();
}
sr.Close();
txtTest.Text = myText;
This is what you're asking for?
i got what i want here is the code (just for write):
public partial class Form1 : Form
{
FileProcess fileprocess;
public Form1()
{
InitializeComponent();
fileprocess = new FileProcess();
}
public void writeFile()
{
fileprocess.writeFile(textBox1.Text,textBox2.Text);
}
private void button1_Click(object sender, EventArgs e)
{
writeFile();
}
}
my class for work with the file :
class FileProcess
{
string path = #"c:\PhoneBook\PhoneBook.txt";
public void writeFile(string text1,string text2)
{
using (StreamWriter sw = new StreamWriter(path,true))
{
sw.WriteLine(text1);
sw.WriteLine(text2);
}
}
}
my mistake was that i try to store all textBox to a string like "info" and pass it through WriteFile() method and this is where i was stuck in it.
tnx to all.

Categories