从C#应用程序打印文档 [英] Print a document from C# application

查看:99
本文介绍了从C#应用程序打印文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哎。我希望一切都会好的。

实际上我想做的是,我想使用文件对话框工具选择一个文件,然后使用printdocument.print()函数打印它。

now print()不带任何参数。我如何告诉打印机要打印哪个文件??

我想知道,print()方法背后究竟发生了什么。

我们不给任何文件文件路径然后按下OK按钮后要打印什么。

plz帮我获得print(file_path + name)功能。

这是我的努力!!! br />

hey. i hope all will be fine.
actually what i want to do is that, i want to select a file using file dialog facility, and then print it using printdocument.print() function.
now print() does not take any parameter. how i can tell the printer to which document to print??
and i want to know, what actually happens behind the print() method.
we are not giving any file path then what is going to print after hitting OK button.
plz help me to get print(file_path+name) functionality.
here is my effort !!!

using System;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;


namespace InstalledAndDefaultPrinters
{
class Program
{


static void Main(string[] args)
{
   string filename="";
   foreach (string printer in PrinterSettings.InstalledPrinters)
       Console.WriteLine(printer);
   var printerSettings = new PrinterSettings();
   Console.WriteLine(string.Format("The default printer is: {0}", printerSettings.PrinterName));

   Console.WriteLine(printerSettings.PrintFileName);
   OpenFileDialog fdlg = new OpenFileDialog();
   fdlg.Title = "Open File Dialog";
   fdlg.InitialDirectory = @"C:\ ";
   fdlg.RestoreDirectory = true;
   fdlg.ShowDialog();
   Console.WriteLine(fdlg.Title);
   if (fdlg.ShowDialog() == DialogResult.OK)
   {
       filename = String.Copy(fdlg.FileName);
   }
   Console.WriteLine(filename);

   PrintDialog printdg = new PrintDialog();
   PrintDocument pd_doc = new PrintDocument();
   printdg.ShowDialog();
   if (printdg.ShowDialog() == DialogResult.OK)
   pd_doc.Print();
   }
   }
   {

推荐答案

尝试将文档设置为<您的 PrintDialog 实例上的code>文档属性。



Try setting your document as the Document property on your PrintDialog instance.

PrintDialog printdg = new PrintDialog();
PrintDocument pd_doc = new PrintDocument();
printdg.Document = pd_doc;
...





您显然还必须确保 PrintDocument 表示您要打印的文件。



希望这会有所帮助,

Fredrik



You obviously also have to make sure your PrintDocument represents the document you want to print.

Hope this helps,
Fredrik


请查看下面的MSDN文章..除了现有的打印文件代码之外,还需要有一个StreamReader实例。



http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.print.aspx [ ^ ]
Please have a look into the below MSDN article.. In addition to your existing code to print a file you need to have a StreamReader instance.

http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.print.aspx[^]


这篇关于从C#应用程序打印文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆