1
votes

I am working on a printing feature where GUI applications create & store XPS files to be printed in a temp directory and spawn a new C# program which picks these files & sends them to printer.

I need to show the printdialog in the GUI application as the xps file generation process needs some input like page range from user specified settings in print dialog page.

Then the GUI invokes the C# "MyPrinter" process which actually prints the xps files. I am able to transfer PrintTicket settings by serializing them to a file from GUI (through SaveTo API) and reading it from "MyPrinter" process. Sample code at MyPrinter process -

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;
}

Wanted to know -

  1. If there is way to serialize the printQueue settings to a file (need print server and printer name at bare minimum) ? Already tried using Xamlreader/XamlWriter but it seems PrintQueue class does not have a default constructor.

OR

  1. Any other way I could reliably transfer selected printer settings from one application to another ?

Thanks in advance Amit

2

2 Answers

0
votes

I suggest that you have a read of the Serialization (C# and Visual Basic) page from MSDN. In .NET, you can make practically any class serializable simply, using the SerializableAttribute Class like this:

[Serializable] // <--- This is all you need
public class YourClass
{
    ...
}

You can find several code examples describing how to save a serialized class in the Basic Serialization page on MSDN... from this linked page:

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();
0
votes

I used following way to transfer print settings & print using another process -

  1. Serialized PrintTicket class, it already has "ToXml()" method.
  2. Transferred the printername
  3. For actual printing in the "MyPrinter" process, did the following-

    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;
    }
    

Thanks