How to change Font in Word by C#
-
Hello Every Body!
I just try to text analysis program by C# .In the program i uses RichTextEdit . I want to load word file, text file , pdf to Richtext Edit Box. Now i can load these file to RichText Edit . But i can't change font these text. Just i used Microsoft.Office.Interop.Word; . Check my code below. Thanks for your kindness!public void ReadMsWord()
{
// variable to store file path
string filePath=null;
// open dialog box to select file
OpenFileDialog file= new OpenFileDialog();
// dilog box title name
file.Title="Word File";
// set initial directory of computer system
file.InitialDirectory= "c:\\";
// set restore directory
file.RestoreDirectory= true;
// execute if block when dialog result box click ok button
if (file.ShowDialog() ==DialogResult.OK)
{
// store selected file path
filePath=file.FileName.ToString();
}
try
{
// create word application
Microsoft.Office.Interop.Word.Application word =new Microsoft.Office.Interop.Word.Application();
// create object of missing value
object miss= System.Reflection.Missing.Value;
// create object of selected file path
object path=filePath;
// set file path mode
object readOnly= false;
//Microsoft.Office.Interop.Word.Font f = new Microsoft.Office.Interop.Word.Font();
//f.Name = "Zawgyi-One, 8.25pt";
//object fo = f;
// open document
Microsoft.Office.Interop.Word.Document docs=word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss,ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
// select whole data from active window document
docs.ActiveWindow.Selection.WholeStory();
// handover the data to cllipboard
docs.ActiveWindow.Selection.Copy();
// clipboard create reference of idataobject interface which transfer the data
IDataObject data= Clipboard.GetDataObject();
//Microsoft.Office.Interop.Word.Range rng = docs.Paragraphs[1].Range;
//rng.Font.Name = "Zawgyi-One";
//set data into richtextbox control in text format
richTextBox1.Text=data.GetData(DataFormats.Text).ToString();
//word.Selection.Font.Name = "Zawgyi-One";
// Microsoft.Office.Interop.Word.Document document = new Microsoft.Office.Interop.Word.Document();
// this.richTextBox1.SelectAll();
//System.Drawing.Font currentFont = richTextBox1.SelectionFont;
// richTextBox1.SelectionFont = new System.Drawing.Font(currentFont.FontFamily,currentFont.Size,newFontStyle);
// read bitmap image from clipboard with help of iddataobject interface
Image img= (Image)data.GetData(DataFor -
Hello Every Body!
I just try to text analysis program by C# .In the program i uses RichTextEdit . I want to load word file, text file , pdf to Richtext Edit Box. Now i can load these file to RichText Edit . But i can't change font these text. Just i used Microsoft.Office.Interop.Word; . Check my code below. Thanks for your kindness!public void ReadMsWord()
{
// variable to store file path
string filePath=null;
// open dialog box to select file
OpenFileDialog file= new OpenFileDialog();
// dilog box title name
file.Title="Word File";
// set initial directory of computer system
file.InitialDirectory= "c:\\";
// set restore directory
file.RestoreDirectory= true;
// execute if block when dialog result box click ok button
if (file.ShowDialog() ==DialogResult.OK)
{
// store selected file path
filePath=file.FileName.ToString();
}
try
{
// create word application
Microsoft.Office.Interop.Word.Application word =new Microsoft.Office.Interop.Word.Application();
// create object of missing value
object miss= System.Reflection.Missing.Value;
// create object of selected file path
object path=filePath;
// set file path mode
object readOnly= false;
//Microsoft.Office.Interop.Word.Font f = new Microsoft.Office.Interop.Word.Font();
//f.Name = "Zawgyi-One, 8.25pt";
//object fo = f;
// open document
Microsoft.Office.Interop.Word.Document docs=word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss,ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
// select whole data from active window document
docs.ActiveWindow.Selection.WholeStory();
// handover the data to cllipboard
docs.ActiveWindow.Selection.Copy();
// clipboard create reference of idataobject interface which transfer the data
IDataObject data= Clipboard.GetDataObject();
//Microsoft.Office.Interop.Word.Range rng = docs.Paragraphs[1].Range;
//rng.Font.Name = "Zawgyi-One";
//set data into richtextbox control in text format
richTextBox1.Text=data.GetData(DataFormats.Text).ToString();
//word.Selection.Font.Name = "Zawgyi-One";
// Microsoft.Office.Interop.Word.Document document = new Microsoft.Office.Interop.Word.Document();
// this.richTextBox1.SelectAll();
//System.Drawing.Font currentFont = richTextBox1.SelectionFont;
// richTextBox1.SelectionFont = new System.Drawing.Font(currentFont.FontFamily,currentFont.Size,newFontStyle);
// read bitmap image from clipboard with help of iddataobject interface
Image img= (Image)data.GetData(DataFornaylynn wrote:
Now i can load these file to RichText Edit
That is not entirely what the code does; it copies it to the clipboard, and pastes that in the RichTextBox. That means that it is a copy, in RTF format, and will no longer support everything that Word does. You can change the font, it's style and color, by programmatically setting a selection (like SelectAll would do) and setting its SelectionFont. If you want to do it using Word-interop, then you will have to make the changes before copying the data to the clipboard. After that, it becomes RTF.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
-
naylynn wrote:
Now i can load these file to RichText Edit
That is not entirely what the code does; it copies it to the clipboard, and pastes that in the RichTextBox. That means that it is a copy, in RTF format, and will no longer support everything that Word does. You can change the font, it's style and color, by programmatically setting a selection (like SelectAll would do) and setting its SelectionFont. If you want to do it using Word-interop, then you will have to make the changes before copying the data to the clipboard. After that, it becomes RTF.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)