1

I'm trying to modify a print ticket since many days. :(

Here is the code :

    using Microsoft.Win32;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Printing;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Drawing.Printing;
    using System.Drawing;
    using System.Xml;

    // ....

    // Get the ticket
    var printQueue = new PrintServer("\\\\NetworkNameHere").GetPrintQueue("PrinterNameHere");
    PrintTicket userPrintTicket = printQueue.UserPrintTicket;

    // Modify the ticket to print in landscape (or any other option)
    var xmlDoc = new XmlDocument();
    xmlDoc.Load(userPrintTicket.GetXmlStream());
    var manager = new XmlNamespaceManager(xmlDoc.NameTable);
    manager.AddNamespace(xmlDoc.DocumentElement.Prefix, xmlDoc.DocumentElement.NamespaceURI);

    string xpath = string.Format("//psf:Feature[@name='{0}']/psf:Option", "psk:PageOrientation");
    XmlNode node = xmlDoc.SelectSingleNode(xpath, manager);
    node.Attributes["name"].Value = "psk:Landscape";

    PrintTicket modifiedPrintTicket = null;
    using (var stream = new MemoryStream())
    {
        xmlDoc.Save(stream);
        stream.Position = 0;
        modifiedPrintTicket = new PrintTicket(stream);
    }

    System.Printing.ValidationResult result = printQueue.MergeAndValidatePrintTicket(printQueue.UserPrintTicket, modifiedPrintTicket);
   printQueue.UserPrintTicket = result.ValidatedPrintTicket;

           MessageBox.Show(result.ValidatedPrintTicket.PageOrientation.Value.ToString());

   printQueue.Commit();

   Byte[] myByteBuffer = UnicodeEncoding.Unicode.GetBytes("This is a test string for the print job stream.");
   using (var job = printQueue.AddJob())
        using (var stream = job.JobStream)
        {
            stream.Write(myByteBuffer, 0, myByteBuffer.Length);
            stream.Close();
        }

The problem : Look like the ticket is correctly modified, but for some reasons it never use the information specified in the ticket for any option. HELP!!!!

Note that I'm using .Net 4.0 but I get the same result with the new PrintTicket parameter in addJob (4.5) : printQueue.AddJob("xx", result.ValidatedPrintTicket)

Thanks!

leppie
  • 115,091
  • 17
  • 196
  • 297
michelqa
  • 137
  • 1
  • 2
  • 14
  • Wow, this one is a hard one. First of all you can mutate userPrintTicket without having to deal with xml, just by changing its properties. Regarding the issue itself, I have been checking this for a very long time. The closest clue I got, is that we are changing the UserPrintTicket, but the job is received by the remote printer as "local user". If you send a test page from you printer's settings page, (changing UserPrintTicket actually changes your default printer settings too), it will print landscape, and the remote printer will receive the job as your user. – omerts Jun 13 '16 at 20:49
  • Not sure about what you are trying to explain.... First my goal is to change the outputbin (not the orientation like in my example) so my only option is to change the xml. So how to change the printTicket correctly ?? any ideas? Thanks – michelqa Jun 14 '16 at 14:24
  • Just to add more detail : The information in the printTicket is totally ignored... Even the example from microsoft (https://msdn.microsoft.com/library/ms552927(v=vs.110).aspx) is not working also if you try to print it.... What is wrong with this damn print ticket ?????? – michelqa Jun 14 '16 at 16:53
  • I really need to move forward on this... Any idea for a work-around??.... my only need is to print programmatically a PDF (a stream) to a specific tray (not the default one) After so many days on this issue I'm still at the same point. – michelqa Jun 14 '16 at 19:56
  • Could you try connecting the printer locally, and then trying again? What I tried to say before is that from my check it seems like the printer received the job under "local user", and maybe the UserPrintTicket is connected to your user name. If you changed the setting once from your code, and then send a test page to print (not through your code), you should see your settings honored by the printer. It looks like there is some problem with the job owner received by the printer, when sending the job through the .net printing assembly. – omerts Jun 15 '16 at 08:17
  • Did you ever find a solution for this? I'm struggling with this problem now too! – Markinson Jan 13 '17 at 08:56
  • The output bin is not a standard setting on PrintTickets, even not on DEVMODE for Win32-Printing. So actually you can't set it directly. I have this issue, too, while migrating an old program from GDI to WPF. The GDI print routine does honor the settings of the output bin on the printer, the WPF routines does not. WPF Printing really sucks. My advice: write a Win32-GDI-Printing program and let that do the job. Like https://www.sumatrapdfreader.org/ – Lars Sep 21 '18 at 10:06

0 Answers0