【问题标题】:Way to default the name of the generated XPS file?如何默认生成的 XPS 文件的名称?
【发布时间】:2010-11-14 19:56:09
【问题描述】:

如果用户打印报告,并且他们恰好使用 Microsoft XPS 打印机,我希望将默认文件名设置为有意义的名称。

我原以为 XPS 打印机会采用打印作业的名称,并将其用作默认文件名 - 但事实并非如此。

当我打印到该打印机时,是否有其他一些程序化的方式来默认生成的 XPS 文件的名称?我在想可能是这样的:

  • 一个注册表项
  • 全局共享内存
  • API 调用,如 SetDefaultXPSFilename()
  • 打印作业的扩展属性

示例

自动化 Excel 以创建电子表格:

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

现在如果用户的默认打印机是Microsoft XPS Document Writer,那么用户将得到:

我想要一种将 File name 默认设置为有用的方法,例如:

20110729 - Chip Bank Settlement Sheet.xps

用户将接受默认文件名,文件将自动组织,而不是用户输入:

asdfadf.xps

参考文献

凹凸:20110729(12 个月后)

【问题讨论】:

    标签: printing xps


    【解决方案1】:

    嗯, 这是一个简单的方法(至少在我的情况下):

    (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
    

    我还没有找到方法来确定我选择的打印机是否要打印到文件中,因此需要对打印机名称进行测试。

    除了上述之外,还有一段我觉得很有用的代码:

    假设我的默认打印机不是 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
    


    有时会很方便。

    【讨论】:

    • @PathTheFrog 知道如何在 C# 和 WPF 中做到这一点吗?
    • 不,对不起。我只使用VB。
    【解决方案2】:

    如果打印的应用程序在DOCINFO 中设置 lpszOutput,Microsoft XPS 文档编写器 (MXDW) 将生成输出文件路径,而不提示用户。

    如果您无权访问应用程序的代码,那么另一个选择是构建一个 XPS 驱动程序,该驱动程序即使在未设置 lpszOutput 时也会生成文件路径。 Windows 驱动程序工具包 (WDK) 是开始的地方。

    更多详情和链接见this post

    【讨论】:

      【解决方案3】:

      Win2PDF 7 可以保存为 XPS,并且默认为打印作业的名称。如果您不想将打印作业用作“文件保存”对话框中显示的名称,可以通过设置名为“PDFTitle”的注册表值来更改默认文件名。

      您还可以使用DOCINFO 的lpszOutput 字段或通过设置名为“PDFFileName”的注册表设置(如Win2PDF 文档中所述)来设置输出文件而无需提示。如果文件名包含 .xps 扩展名,则会以 XPS 格式创建文件。

      【讨论】:

      • 只有当我能说服微软在 Windows 上发布 Win2PDF 时。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      • 1970-01-01
      相关资源
      最近更新 更多