Way to default the name of the generated XPS file?

Ian Boyd picture Ian Boyd · Jul 28, 2009 · Viewed 20.4k times · Source

If a user prints a report, and they happen to be using the Microsoft XPS printer, i would like the default the filename to something meaningful.

i would have thought that the XPS printer would take the name of the print job, and use that as the default filename - but it doesn't.

Is there some other, programatic, way to default the name of the generated XPS file when i print to that printer? i was thinking there might be something like:

  • a registry key
  • global shared memory
  • API call like SetDefaultXPSFilename()
  • extended attributes about a print job

Example

Automate Excel to create a spreadsheet:

Excel xl = new ExcelApplication();
Workbook wb = xl.Workbooks.Add();
GenerateReport(wb);
wb.PrintOut();

Now if the user's default printer is the Microsoft XPS Document Writer, then the user will get:

enter image description here

i would like a way for that File name to be defaulted to something useful, such as:

20110729 - Chip Bank Settlement Sheet.xps

The user will accept the default filename, and files will organized automatically, rather than the user typing:

asdfadf.xps

References

Bump: 20110729 (12 months later)

Answer

PatTheFrog picture PatTheFrog · Sep 1, 2010

Well, here is a simple way (at least in my case):

(myPrintPage inherits from System.Drawing.Printing.PrintDocument)

    With myPrintPage
        With .PrinterSettings
            If .PrinterName = "Microsoft XPS Document Writer" Then
            .PrintToFile = True
            .PrintFileName = "c:\test.pdf"
            End If
        End With
        .Print()
    End With

I haven't found a way, yet, to determine whether or not the printer I have chosen is going to print into a file, hence the test on the printer's name.

In addition to above, here is a piece of code I found useful:

Let's say that my default printer is NOT the XPS Document Writer. My code needs to archive automatically some data, print a report in XPS, then offer the user to print the report on the default printer. In the second step, I need to change the PrinterSettings of myPrintPage.
Here is how:

  'save xps results
    'is the XPS printer installed?
    Dim myXPSfound As Boolean = False
    For Each s As String In System.Drawing.Printing.PrinterSettings.InstalledPrinters
        If s.Contains("XPS") Then
            myXPSfound = True
            Exit For
        End If
    Next
    If myXPSfound Then
        'Manual settings of the XPS printerSettings
        Dim myXPSPrinterSettings As New Drawing.Printing.PrinterSettings
        myXPSPrinterSettings.Collate = False
        myXPSPrinterSettings.Copies = 1
        myXPSPrinterSettings.Duplex = Drawing.Printing.Duplex.Simplex
        myXPSPrinterSettings.FromPage = 0
        myXPSPrinterSettings.MaximumPage = 9999
        myXPSPrinterSettings.MinimumPage = 0
        myXPSPrinterSettings.PrinterName = "Microsoft XPS Document Writer"
        myXPSPrinterSettings.PrintRange = Drawing.Printing.PrintRange.AllPages
        myXPSPrinterSettings.PrintToFile = True
        myXPSPrinterSettings.ToPage = 1

        myPrintPage.PrinterSettings = myXPSPrinterSettings
        myPrintPage.PrinterSettings.PrintToFile = True
        myPrintPage.PrinterSettings.PrintFileName = mytargetFileName & ".xps"
        Try
            myPrintPage.Print()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Error Printing the XPS File")
        End Try
    Else
        MsgBox("The Microsoft XPS Writer was no found on this computer", MsgBoxStyle.Information, "Error Printing the XPS File")
    End If


It can be handy sometimes.