Hi Awais,
Here I will give my thoughts and experiences about my problems with printing so others can benefit.
I’m aware of the possibilities of the Document.Print method. And the double-sided printing works as a charm.
The downside of the Document.Print method is the performance of my service on a server.
To improve the performance I started using the XpsPrint API described here:
https://docs.aspose.com/words/net/print-a-document-programmatically-or-using-dialogs/
This was a great improvement on the performance but I lost the ability to manage single-sided/double sided.
To be able to manage the simplex/duplex modes, I searched the internet and stumbled upon this:
https://social.msdn.microsoft.com/forums/en-us/eb1aab0d-f015-479e-9fe7-bfb848a4d58b/how-to-create-new-xpsfile-with-print-ticket
I’ve modified the Aspose XpsPrintHelper to create a Print Ticket in the documents that has to be printed.
The code is working, but I’ve not been able yet to test the performance of my service on a Windows Server.
The modified code is:
public static void Print(Aspose.Words.Document document, string printerName, string jobName, bool isWait, bool printDuplex)
{
if (document == null)
throw new ArgumentNullException("document");
// Create a PrintTicket to set Simplex or Duplex
var xpsInName = Path.ChangeExtension(document.OriginalFileName, "xps");
var xpsOutName = string.Format("{0}PT.xps", Path.GetFileNameWithoutExtension(xpsInName));
document.Save(xpsInName);
using (var doc = new XpsDocument(xpsInName, FileAccess.ReadWrite))
{
using (var container = Package.Open(xpsOutName, FileMode.Create))
{
using (var xpsDoc = new XpsDocument(container))
{
var xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
xpsWriter.WritingPrintTicketRequired += new WritingPrintTicketRequiredEventHandler((s, e) => WritingPrintTicketRequired(s, e, printDuplex));
xpsWriter.Write(doc.GetFixedDocumentSequence());
}
}
}
var stream = new MemoryStream();
using (var fileStream = File.OpenRead(xpsOutName))
{
fileStream.CopyTo(stream);
}
stream.Position = 0;
Print(stream, printerName, jobName, isWait);
}
private static void WritingPrintTicketRequired(Object sender, WritingPrintTicketRequiredEventArgs e, bool printDuplex)
{
if (e.CurrentPrintTicketLevel == PrintTicketLevel.FixedDocumentSequencePrintTicket)
{
var pt = new PrintTicket { Duplexing = printDuplex ? Duplexing.TwoSidedLongEdge : Duplexing.OneSided };
e.CurrentPrintTicket = pt;
}
}
I hope the performance is good and others can use this as well.
Best regards,
Michel van Vuren