【问题标题】:XPSDocumentWriter - Printing Specific Pages to Specific TraysXPSDocumentWriter - 将特定页面打印到特定托盘
【发布时间】:2014-03-18 14:15:37
【问题描述】:

我目前正在开发一个打印应用程序。此应用程序要求某些页面需要来自打印机上的特定纸盘。以下是我到目前为止的内容:

    foreach (var dto in dispensersToPrint)
    {
        var documents = FilterDocumentSections(DispenserDocumentsToPrint.RetrieveByDispenserId(dto.DispenserId));
        var groupedDocs = documents.GroupBy(t => t.DocumentTypeId);
        var queueName = Properties.Settings.Default.PrinterName;
        var queue = RawPrinterHelper.GetPrintQueue(queueName);
        var seq = new FixedDocumentSequence();
        var xpsWriter = PrintQueue.CreateXpsDocumentWriter(queue);

        foreach (var docGroup in groupedDocs)
        {
            var printTicket = queue.DefaultPrintTicket.Clone();
            var printTray = MapPrintTray((DocumentSectionType)docGroup.Key);
            if (!printTray.IsNullOrEmpty())
            {
                printTicket = RawPrinterHelper.ModifyPrintTicket(printTicket, "psk:JobInputBin", printTray);
            }
            var fixedDoc = new FixedDocument();
            fixedDoc.PrintTicket = printTicket;

            foreach (var doc in docGroup)
            {
                var pageContent = new PageContent();
                var fixedPage = new FixedPage();

                var localFileName = string.Empty;
                var unzippedFileName = string.Empty;
                //copy files locally
                localFileName = CopyFileToLocalMachine(doc.FileName);
                //unzip file
                unzippedFileName = EmfPrintingHelper.UnzipEmfFile(localFileName);
                var itemToPrint = new PrintableEmfImage
                                           {
                                               DataContext = new EmfImageViewModel { FileName = unzippedFileName }
                                           };
                fixedPage.Children.Add(itemToPrint);
                pageContent.Child = fixedPage;
                fixedDoc.Pages.Add(pageContent);
            }
            var docRef = new DocumentReference();
            docRef.SetDocument(fixedDoc);
            seq.References.Add(docRef);
        }
        xpsWriter.Write(seq);
    }

真正的高水平:

  • 对于每个分配器(工单),我需要打印;我首先按 DocumentType 分组(即,将类型 A 打印到纸盒 1)
  • 然后我创建一个新的 FixedDocumentSequence
  • 对于每个 DocumentType;然后我创建一个固定的文档。然后我修改打印票以查看相应的托盘。
  • 然后我为每种文档类型构建每个单独的页面;并将它们添加到 FixedDocument
  • FixedDocument 构建完成后;我将它附加到 DocumentSequence。
  • 然后我将 FixedDocumentSequence 发送到 xpsWriter。

但是出于某种原因;这些设置没有得到尊重。我从同一个托盘打印所有文件。

以下是我目前的一些观察结果:

  • 打印票的修改确实有效;我已经通过将修改后的 printTicket 发送到 xpsWriter 来验证这一点;但这会将设置应用于整个作业;这对我来说是不行的。
  • 查询我的打印能力时;我注意到我只有 JobInputBin。我不认为这意味着这台打印机不支持该功能。因为多托盘打印工作来自类似的 WindowsForms 应用程序(使用 PageSettings.PaperSource)

关于我接下来可以尝试什么的任何想法?以前有没有人成功地做过这样的事情?

【问题讨论】:

    标签: wpf printing xps


    【解决方案1】:

    首先我会说,我无法使用带托盘的打印机,因此很遗憾我无法测试此解决方案。话虽如此,我会将您的注意力引向 MSDN 论坛帖子 here,原发帖人在该帖子中追求相同的每页托盘行为。

    根据您发布的代码,您可能已经看到了这篇文章中的一些内容,从您发布的代码至少可以实现ModifyPrintTicket() 来判断。

    在帖子中,有几个不同的用户,每个用户都引用了针对他们特定版本问题的解决方案。但是,在这种情况下似乎最相关的一个解决方案是关于在 ModifyPrintTicket() 中未正确考虑命名空间的解决方案(由 乔0815)。我说“最相关”是因为海报提到打印托盘被忽视了。他们(wittersworld)提供了一个替代实现来纠正这个问题。在 MSDN 上的帖子中,完整源的链接已损坏,但可以找到 here。

    要点是,在ModifyPrintTicket() 上,他们添加了一个namespaceUri 参数,然后修改了这个:

    if (node != null)
    {
        node.Attributes["name"].Value = newValue;
    }
    

    到这里:

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

    允许用户指定使用的特定于打印机的命名空间。

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多