将打印文档保存到XPS时出现异常

时间:2017-12-26 11:08:19

标签: c# printing

使用代码

将我的打印文档保存为我的应用程序启动路径中的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();*

这在某些PC中工作正常但在某些情况下会发生以下错误

“尝试读取或写入受保护的内存。这通常表示其他内存已损坏。”

当我将默认打印机设置为XPS时,代码适用于所有系统。但是当我将其更改为网络打印机时,错误再次发生。

1 个答案:

答案 0 :(得分:0)

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