如果用户打印报告,并且他们碰巧使用的是Microsoft XPS打印机,我希望默认文件名是有意义的.
我原本以为XPS打印机将采用打印作业的名称,并将其用作默认文件名 - 但事实并非如此.
当我打印到该打印机时,是否有其他一些编程方法来默认生成的XPS文件的名称?我在想可能有类似的东西:
自动化Excel以创建电子表格:
Excel xl = new ExcelApplication();
Workbook wb = xl.Workbooks.Add();
GenerateReport(wb);
wb.PrintOut();
Run Code Online (Sandbox Code Playgroud)
现在,如果用户的默认打印机是Microsoft XPS Document Writer,则用户将获得:
我希望有一种方法File name可以默认为有用的东西,例如:
20110729 - Chip Bank Settlement Sheet.xps
Run Code Online (Sandbox Code Playgroud)
用户将接受默认文件名,文件将自动组织,而不是用户键入:
asdfadf.xps
Run Code Online (Sandbox Code Playgroud)
凹凸:20110729(12个月后)
嗯,这是一个简单的方法(至少在我的情况下):
(myPrintPage继承自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
Run Code Online (Sandbox Code Playgroud)
我还没有找到办法确定我选择的打印机是否要打印到文件中,因此测试打印机的名称.
除此之外,这里有一段我觉得有用的代码:
假设我的默认打印机不是XPS Document Writer.我的代码需要自动存档一些数据,以XPS打印报告,然后让用户在默认打印机上打印报告.在第二步中,我需要更改myPrintPage的PrinterSettings.
方法如下:
'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
Run Code Online (Sandbox Code Playgroud)
它有时候很方便.
小智 2
如果打印的应用程序在DOCINFO中设置 lpszOutput,则 Microsoft XPS Document Writer (MXDW) 将生成输出文件路径,而不提示用户。
如果您无权访问应用程序的代码,那么另一个选择是构建一个 XPS 驱动程序,即使尚未设置 lpszOutput,该驱动程序也会生成文件路径。Windows 驱动程序工具包 (WDK) 是起点。
有关更多详细信息和链接,请参阅此帖子。