System.Printing命名空间设置打印机页面源

时间:2016-05-03 06:40:30

标签: c# asp.net printing system.printing pdftron

我正在尝试通过System.Printing命名空间

打印XPS流
    Stream stm = pdftron.PDF.Convert.ToXps(pdfdoc);

    LocalPrintServer localPrintServer = new LocalPrintServer();
    PrintQueueCollection pqc = localPrintServer.GetPrintQueues();
    PrintQueue defaultPrintQueue = localPrintServer.GetPrintQueue(_PrinterName);
    MemoryStream ms = defaultPrintQueue.GetPrintCapabilitiesAsXml();


    // Call AddJob
    System.Printing.PrintTicket printTicket = defaultPrintQueue.DefaultPrintTicket;


    PrintSystemJobInfo myPrintJob = defaultPrintQueue.AddJob();

    Stream myStream = myPrintJob.JobStream;//may be this line is wrong, can you suggest alternative?
    myStream = stm;//may be this line is wrong, can you suggest alternative?
    myStream.Close();//may be this line is wrong, can you suggest alternative?
    var inp= defaultPrintQueue.GetPrintCapabilities().InputBinCapability;
    System.Printing.PrintTicket newprintTicket = XpsPrinterUtils.ModifyPrintTicket(printTicket, "psk:JobInputBin", "NS0000:" + _PaperSource);
    System.Printing.ValidationResult vr = defaultPrintQueue.MergeAndValidatePrintTicket(printTicket, newprintTicket);// this line giving exception while changing page source merge and validate
    myPrintJob.Commit();

XpsPrinterUtils.ModifyPrintTicket from github

代码无效,我对此感到困惑。

任何人都有很好的想法或解决方案来打印(pdftron获得的Xps文件流)使用system.printing命名空间并动态更改页面源。

1 个答案:

答案 0 :(得分:0)

我没有测试,但这似乎更接近正确的解决方案。

Stream stream_source = pdftron.PDF.Convert.ToXps(pdfdoc);
Stream stream_sink = myPrintJob.JobStream;
stream_source.CopyTo(stream_sink);

您可能希望测试上述内容,方法是将stream_sink创建为磁盘上文件的流,并验证stream_source.CopyTo(stream_sink)是否按预期工作。如果是这样,那么任何剩下的问题都将是使用System.Printing API,而不是使用PDFNet。