简体   繁体   English

无法将打印文档保存到 xps

[英]unable to save print document to xps

I'm saving my printed document as XPS in the application start up path using the code below:我正在使用以下代码在应用程序启动路径中将我的打印文档保存为 XPS:

PrintDocument pd = new PrintDocument();
pd = PreparePrintDocument();
pd.PrinterSettings.PrintFileName = Application.StartupPath+"\\backup.xps";
pd.PrinterSettings.PrintToFile = true;
pd.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";
pd.Print();
pd.Dispose();

This works fine in some PC's but in some others the following error occurs:这在某些 PC 上工作正常,但在其他一些 PC 中会出现以下错误:

"Attempted to read or write protected memory. This is often an indication that other memory is corrupt." “尝试读取或写入受保护的内存。这通常表明其他内存已损坏。”

When i set the default printer to XPS the code works in all systems but when I change it to network printer the error occurs again.当我将默认打印机设置为 XPS 时,代码在所有系统中都有效,但是当我将其更改为网络打印机时,错误再次发生。

Try with a "using" block instead of disposing the document yourself:尝试使用“使用”块而不是自己处理文档:

using(PrintDocument pd = new PrintDocument()) 
{
    pd = PreparePrintDocument();
    pd.PrinterSettings.PrintFileName = 
    Application.StartupPath+"\\backup.xps";
    pd.PrinterSettings.PrintToFile = true;
    pd.PrinterSettings.PrinterName = "XPS";
    pd.DefaultPageSettings.PrinterSettings.PrinterName ="Microsoft XPS Document Writer"        
    pd.Print();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM