Sruthi Suresh
Sruthi Suresh

Reputation: 697

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:

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:

"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.

Upvotes: 1

Views: 610

Answers (1)

Isma
Isma

Reputation: 15209

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

Upvotes: 1

Related Questions