Reputation: 159
I'm using the following code. When I print from notepad it get printed. But when I print from MS Word it get printed without words containing symbols. I think I have to enter doc format in code. How can I do this?
String content="";
private void btnUpload_Click(object sender, EventArgs e)
{
string fileName;
// Show the dialog and get result.
OpenFileDialog ofd = new OpenFileDialog();
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
fileName = ofd.FileName;
var application = new Microsoft.Office.Interop.Word.Application();
//var document = application.Documents.Open(@"D:\ICT.docx");
//read all text into content
content=System.IO.File.ReadAllText(fileName);
//var document = application.Documents.Open(@fileName);
}
}
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDialog printDlg = new PrintDialog();
PrintDocument printDoc = new PrintDocument();
printDoc.DocumentName = "fileName";
printDlg.Document = printDoc;
printDlg.AllowSelection = true;
printDlg.AllowSomePages = true;
//Call ShowDialog
if (printDlg.ShowDialog() == DialogResult.OK)
{
printDoc.PrintPage += new PrintPageEventHandler(pd_PrintPage);
printDoc.Print();
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
ev.Graphics.DrawString(content,printFont , Brushes.Black,
ev.MarginBounds.Left, 0, new StringFormat());
}
Upvotes: 1
Views: 13705
Reputation: 12557
As far as I know there are no basic functions which support reading the word format and / or printing it with the default Print Functionality in .net .
IF you just want to print the document without any further information you can start a basic windows print process by using the Start method of the Process Class with the PrintTo Verb
s. MSDN Forum Print Word Document in c# Example form the linkes post:
using (PrintDialog pd = new PrintDialog())
{
pd.ShowDialog();
ProcessStartInfo info = new ProcessStartInfo(@"D:\documents\filetoprint.doc");
info.Verb = "PrintTo";
info.Arguments = pd.PrinterSettings.PrinterName;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(info);
}
If you need to do more (layout, other data ...) you could write your own doc / docx parser or use something like the aspose tools
s. http://www.aspose.com/.net/word-component.aspx
perhaps infragistics / devexpress may also components to read word documents, convert them to HTML or furthermore supporting direct printing of the word.
For all tools trial versions should be aviable
Upvotes: 4