[C#] XPSDocument naar printerTray sturen

Pagina: 1
Acties:

Vraag


  • tha_crazy
  • Registratie: Maart 2007
  • Laatst online: 19:18
Ik ben bezig met een applicatie welke XPS documenten merged en daarna afvuurt naar de printer.
Echter krijg ik het niet voor elkaar om deze naar een specifieke Tray te sturen.

De manier zoals ik hem nu heb stuurt die zelfs helemaal niks meer naar de printer, maar krijg je een hele mooie savefile dialog 8)7

Mijn initiële printopdracht verstuurde ik als volgt:
C#:
1
2
3
4
5
6
7
8
9
10
                    using (XpsDocument xpsDoc = new XpsDocument(_directory+"\\PrintsXXX.xps", System.IO.FileAccess.Read))
                    {
                        PrintDialog dlg = new PrintDialog();
                        
                        dlg.PrintTicket.Duplexing = Duplexing.OneSided;

                        dlg.PrintDocument(xpsDoc.GetFixedDocumentSequence().DocumentPaginator, "Prints XXX");

                        //LogData("Print OK\r\n");
                    }


Dit werkt op zich al aardig, maar doet nog niet precies wat ik wilde.
Tijdens het zoeken kwam ik wel dit C# voorbeeld tegen, welke ik ben gaan integreren in mijn code.
http://www.wittersworld.c...n-printing-xps-documents/

Waardoor het er daarna als volgt uit zag:
C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
                    using (XpsDocument xpsDoc = new XpsDocument(_directory+"\\PrintsXXX.xps", System.IO.FileAccess.Read))
                    {
                        PrintDialog dlg = new PrintDialog();

                        PrintTicket defaultPrintTicket = dlg.PrintTicket;

                        string nameSpaceURI = string.Empty;
                        string selectedBin = XpsPrinterUtils.GetInputBinName(printerName, intSelectedXXXTray, out nameSpaceURI);
                        PrintTicket myPrintTicket = XpsPrinterUtils.ModifyPrintTicket(defaultPrintTicket, "psk:JobInputBin", selectedBin, nameSpaceURI);

                        dlg.PrintTicket = myPrintTicket;

                        dlg.PrintTicket.Duplexing = Duplexing.OneSided;

                        dlg.PrintDocument(xpsDoc.GetFixedDocumentSequence().DocumentPaginator, "Prints XXX");

                        //LogData("Print OK\r\n");
                    }


en de class inclusief een tweak voor netwerkprinters.
C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    public class XpsPrinterUtils
    {

        public static string GetInputBinName(string printerName, int binIndex, out string nameSpaceURI)
        {
            string binName = string.Empty;

            //GetServer
            string printServerDNS = "\\\\" + Dns.GetHostEntry(printerName.TrimStart('\\').Remove((printerName.LastIndexOf('\\')-2))).HostName;

            // get PrintQueue of Printer from the PrintServer
            PrintServer printServer = new PrintServer(printServerDNS);
            PrintQueue printQueue = printServer.GetPrintQueue(printerName);

            // get PrintCapabilities of the printer
            MemoryStream printerCapXmlStream = printQueue.GetPrintCapabilitiesAsXml();

            // read the JobInputBins out of the PrintCapabilities
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(printerCapXmlStream);

            // create NamespaceManager and add PrintSchemaFrameWork-Namespace (should be on DocumentElement of the PrintTicket)
            // Prefix: psf  NameSpace: xmlDoc.DocumentElement.NamespaceURI = "http://schemas.microsoft.com/windows/2003/08/printing/printschemaframework"
            XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
            manager.AddNamespace(xmlDoc.DocumentElement.Prefix, xmlDoc.DocumentElement.NamespaceURI);
            nameSpaceURI = xmlDoc.ChildNodes[1].GetNamespaceOfPrefix("ns0000");

            // and select all nodes of the bins
            XmlNodeList nodeList = xmlDoc.SelectNodes("//psf:Feature[@name='psk:JobInputBin']/psf:Option", manager);

            // fill Dictionary with the bin-names and values
            if (nodeList.Count > binIndex)
            {
                binName = nodeList[binIndex].Attributes["name"].Value;
            }
            return binName;
        }

        public static PrintTicket ModifyPrintTicket(PrintTicket ticket, string featureName, string newValue, string nameSpaceURI)
        {
            if (ticket == null)
            {
                throw new ArgumentNullException("ticket");
            }
            // read Xml of the PrintTicket
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(ticket.GetXmlStream());

            // create NamespaceManager and add PrintSchemaFrameWork-Namespace (should be on DocumentElement of the PrintTicket)
            // Prefix: psf  NameSpace: xmlDoc.DocumentElement.NamespaceURI = "http://schemas.microsoft.com/windows/2003/08/printing/printschemaframework"
            XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
            manager.AddNamespace(xmlDoc.DocumentElement.Prefix, xmlDoc.DocumentElement.NamespaceURI);

            // search node with desired feature we're looking for and set newValue for it
            string xpath = string.Format("//psf:Feature[@name='{0}']/psf:Option", featureName);
            XmlNode node = xmlDoc.SelectSingleNode(xpath, manager);
            if (node != null)
            {
                if (newValue.StartsWith("ns0000"))
                {
                    // add namespace to xml doc
                    XmlAttribute namespaceAttribute = xmlDoc.CreateAttribute("xmlns:ns0000");
                    namespaceAttribute.Value = nameSpaceURI;
                    xmlDoc.DocumentElement.Attributes.Append(namespaceAttribute);
                }
                node.Attributes["name"].Value = newValue;
            }

            // create a new PrintTicket out of the XML
            MemoryStream printTicketStream = new MemoryStream();

            xmlDoc.Save(printTicketStream);
            printTicketStream.Position = 0;

            PrintTicket modifiedPrintTicket = new PrintTicket(printTicketStream);
            
            return modifiedPrintTicket;
        }
    }


Wat me sowieso opvalt:

C#:
1
2
3
4
            if (nodeList.Count > binIndex)
            {
                binName = nodeList[binIndex].Attributes["name"].Value;
            }

De count is altijd 0.
Verder kom ik er niet echt uit, en het merendeel wat ik vind zijn voorbeelden in VB.net wat ik wel een beetje maar niet compleet kan lezen.
Of XPS converten naar PDF 8)7
of gewoon het printen naar een XPS bestand.

Iemand die mij hier een duwtje in de goede richting kan geven?