修改 我试图重建我不再需要展示的代码。我认为这只是打印机类的一个限制,没有公开可以通过使用对话框选择的功能。看来我应该能够配置并将printerSettings对象分配给PrintDocument,然后打印PrintDocument ...... ???我不是在想这里或者??
再次编辑 我认为所有的设置者都是'printerSettings.DefaultPageSettings'。这将允许我修改打印机设置。我还没有证明,但稍后会
PrintDocument pd = new PrintDocument();
pd.DocumentName = "test.doc";
PrinterSettings printerSettings = new PrinterSettings();
printerSettings.?? <- I want to set the printer setting here e.g. DL, A4, etc
pd.PrinterSettings = printerSettings;
pd.Print();
我在c#(支票,信件,文件)中生成了单词邮件合并文档,但所有这些都需要不同的打印机设置(check =自定义设置,字母= DL Env,文档= A4)
我已保存这些设置,并且可以在加载打印机首选项对话框时访问它们,但我希望能够将其构建为代码而不是手动更改打印机设置。我环顾四周,似乎打印机设置类应该是它,但我似乎无法让它工作。
示例我正在尝试做的伪代码
//create the mail merge
IList<Letter> letters = MailMerge.Create(enum.letters)
Printer.Print(letters) //<-- in here I am trying set the printing preferences to DL Env
//create the mail merge
IList<Document> docs = MailMerge.Create(enum.documents)
Printer.Print(docs) //<-- in here I am trying set the printing preferences to A4
任何帮助表示赞赏。
感谢
答案 0 :(得分:3)
您可以使用WMI。我唯一的WMI经验是WMI的一些C#代码来检索一些打印机属性,我没有尝试设置任何打印机属性,但我认为它应该是可能的。也许这些MSDN链接和代码可以帮助您入门。
WMI Tasks: Printers and Printing显示VB脚本中的命令。 How To: Retrieve Collections of Managed Objects显示了如何使用SelectQuery和枚举。 How To: Execute a Method显示了如何执行方法: - )。
编辑:我刚注意到这个StackOverflow article: How do I programatically change printer settings ...,似乎使用WMI来更改某些打印机设置。
我的检索代码如下所示:
//using System.Management;
private void GetPrinterProperties(object sender, EventArgs e)
{
// SelectQuery from:
// http://msdn.microsoft.com/en-us/library/ms257359.aspx
// Build a query for enumeration of instances
var query = new SelectQuery("Win32_Printer");
// instantiate an object searcher
var searcher = new ManagementObjectSearcher(query);
// retrieve the collection of objects and loop through it
foreach (ManagementObject lPrinterObject in searcher.Get())
{
string lProps = GetWmiPrinterProperties(lPrinterObject);
// some logging, tracing or breakpoint here...
}
}
// log PrinterProperties for test-purposes
private string GetWmiPrinterProperties(ManagementObject printerObject)
{
// Win32_Printer properties from:
// http://msdn.microsoft.com/en-us/library/aa394363%28v=VS.85%29.aspx
return String.Join(",", new string[] {
GetWmiPropertyString(printerObject, "Caption"),
GetWmiPropertyString(printerObject, "Name"),
GetWmiPropertyString(printerObject, "DeviceID"),
GetWmiPropertyString(printerObject, "PNPDeviceID"),
GetWmiPropertyString(printerObject, "DriverName"),
GetWmiPropertyString(printerObject, "Portname"),
GetWmiPropertyString(printerObject, "CurrentPaperType"),
GetWmiPropertyString(printerObject, "PrinterState"),
GetWmiPropertyString(printerObject, "PrinterStatus"),
GetWmiPropertyString(printerObject, "Location"),
GetWmiPropertyString(printerObject, "Description"),
GetWmiPropertyString(printerObject, "Comment"),
});
}
private string GetWmiPropertyString(ManagementObject mgmtObject, string propertyName)
{
if (mgmtObject[propertyName] == null)
{
return "<no "+ propertyName + ">";
}
else
{
return mgmtObject[propertyName].ToString();
}
}
}
答案 1 :(得分:0)
private void startPrintingButton_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (DialogResult.OK == ofd.ShowDialog(this))
{
PrintDocument pdoc = new PrintDocument();
pdoc.DefaultPageSettings.PrinterSettings.PrinterName = "ZDesigner GK420d";
pdoc.DefaultPageSettings.Landscape = true;
pdoc.DefaultPageSettings.PaperSize.Height = 140;
pdoc.DefaultPageSettings.PaperSize.Width = 104;
Print(pdoc.PrinterSettings.PrinterName, ofd.FileName);
}
}
private void Print(string printerName, string fileName)
{
try
{
ProcessStartInfo gsProcessInfo;
Process gsProcess;
gsProcessInfo = new ProcessStartInfo();
gsProcessInfo.Verb = "PrintTo";
gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
gsProcessInfo.FileName = fileName;
gsProcessInfo.Arguments = "\"" + printerName + "\"";
gsProcess = Process.Start(gsProcessInfo);
if (gsProcess.HasExited == false)
{
gsProcess.Kill();
}
gsProcess.EnableRaisingEvents = true;
gsProcess.Close();
}
catch (Exception)
{
}
}