我正在开发GUI应用程序创建的打印功能。存储要在临时目录中打印的XPS文件,并生成一个新的C#程序,该程序将选择这些文件&将它们发送到打印机。
我需要在GUI应用程序中显示printdialog,因为xps文件生成过程需要一些输入,例如打印对话框页面中用户指定设置的页面范围。
然后GUI调用C#“MyPrinter”进程,该进程实际打印xps文件。我可以通过将PrintTicket设置从GUI(通过SaveTo API)序列化为文件并从“MyPrinter”进程读取来传输PrintTicket设置。 MyPrinter流程中的示例代码 -
var server = new PrintServer(@"\\servername"); // need to get this from GUI processs
var printQueue = server.GetPrintQueue("printername"); // We can use printQueue name here
printQueue.UserPrintTicket = GetPrintTicket();
int index = 0;
foreach (var printFileName in new DirectoryInfo(printingPath).EnumerateFiles("*.xps"))
{
printQueue.AddJob(string.Format("CustomPrint-{0}", ++index), printFileName.FullName, printQueue.IsXpsDevice);
}
private PrintTicket GetPrintTicket()
{
var reader = new FileStream(@"C:\temp\printTicket.xml", FileMode.Open);
var ticket = new PrintTicket(reader);
reader.Close();
return ticket;
}
想知道 -
OR
提前致谢 阿米特
答案 0 :(得分:0)
我建议您阅读MSDN中的Serialization (C# and Visual Basic)页面。在.NET中,您可以使用SerializableAttribute
Class这样简单地使任何类可序列化:
[Serializable] // <--- This is all you need
public class YourClass
{
...
}
您可以在此链接页面找到几个代码示例,说明如何在MSDN上的Basic Serialization页面中保存序列化类...:
MyObject obj = new MyObject();
obj.n1 = 1;
obj.n2 = 24;
obj.str = "Some String";
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write,
FileShare.None);
formatter.Serialize(stream, obj);
stream.Close();
答案 1 :(得分:0)
我使用以下方式传输打印设置&amp;使用其他流程打印 -
对于“MyPrinter”过程中的实际打印,请执行以下操作 -
var server = new PrintServer(printDialog.PrintQueue.HostingPrintServer.Name);
var printQueue = server.GetPrintQueue(printDialog.PrintQueue.Name);
printQueue.UserPrintTicket = GetPrintTicket();
PrintUsingXpsDocWriter(printQueue);
}
private void PrintUsingXpsDocWriter(PrintQueue printQueue)
{
var docWriter = PrintQueue.CreateXpsDocumentWriter(printQueue);
int index = 0;
foreach (var printFileName in new DirectoryInfo(printingPath).EnumerateFiles("*.xps"))
{
}
printQueue.Dispose();
}
private PrintTicket GetPrintTicket()
{
// This file had serialized print ticket settings
var reader = new FileStream(@"C:\temp\printTicket.xml", FileMode.Open);
var ticket = new PrintTicket(reader);
reader.Close();
return ticket;
}
由于