1626928 - How to use PrintToPrinter API to directly print the report to "Microsoft XPS Document Writer" in ASP.NET web application without being prompted for XPS file path and name?

SAP Knowledge Base Article - Public

1626928 - How to use PrintToPrinter API to directly print the report to "Microsoft XPS Document Writer" in ASP.NET web application without being prompted for XPS file path and name?

Symptom

  • In Windows .NET application, you will get the popup dialog asking for providing XPS file path and name when printing the report to "Microsoft XPS Document Writer"
  • In ASP.NET application, as business requirement, you need to print the reports to "Microsoft XPS Document Writer" at Server side. Is it possible to achieve this without user intervention (providing XPS file path and name)?

Environment

  • Crystal Reports 2008
  • Crystal Reports XI R2
  • Crystal Report SDK

Resolution

Use the below sample code:

Dim rpt As New ReportDocument
rpt.Load(Server.MapPath("MyCrystalReport.rpt"))

'CrystalReportViewer1.ReportSource = rpt

Dim printerSettings As New System.Drawing.Printing.PrinterSettings
Dim pageSettings As New System.Drawing.Printing.PageSettings

rpt.PrintOptions.CopyTo(printerSettings, pageSettings)
printerSettings.PrinterName = "Microsoft XPS Document Writer"
printerSettings.PrintFileName = "c:\folder\myrpt.xps"
printerSettings.PrintToFile = True

rpt.PrintToPrinter(printerSettings, pageSettings, False)

 

Note:

If you do not specify the folder path in PrintFileName property ( eg: printerSettings.PrintFileName = "myrpt.xps") , XPS file will be generated at C:\WINDOWS\system32\inetsrv folder by default. Thus, you may get "Access is Denied" error due to the lack of permission of writing the file to this folder.  You can use either of following solutions to solve the issue:

  • Specify the folder path in PrinterName property. Also ensure the default build-in Network Service Account associated with the application's default worker process has "write" permission for the folder.
  • Have your ASP.NET run under local system account.  Please refer to the solution section of SAP KBA 1216640 as to how to change the worker process from the default "Network Service" to "Local System"

 

Keywords

xps, print, file, form , KBA , BI-DEV-NET , BI Software Development Kits (SDKs) - .NET or Other , How To

Product

Crystal Reports 2008 V1 ; SAP Crystal Reports 2011, feature pack 03 ; SAP Crystal Reports XI R2