vb.net 无需另存为对话框即可打印到 XPS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21918040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:51:02  来源:igfitidea点击:

Print to XPS without a Save As dialog

vb.netprintingxps

提问by Bernoulli Lizard

How can I save an xps file by printing to a virtual printer without using the Save File As dialog? When I call the print method, a dialog automatically pops up asking the user to specify the file name and path. This only works when creating brand new files; it throws an error 'you do not have permission to write to that file...' if I attempt to overwrite an existing file. Anyways, I want the user to be able to specify the file name in my own dialog, not the one that is autamatically called by the printDocument's Print method.

如何在不使用“文件另存为”对话框的情况下通过打印到虚拟打印机来保存 xps 文件?当我调用print方法时,会自动弹出一个对话框,要求用户指定文件名和路径。这仅在创建全新文件时有效;如果我尝试覆盖现有文件,它会引发错误“您无权写入该文件...”。无论如何,我希望用户能够在我自己的对话框中指定文件名,而不是由 printDocument 的 Print 方法自动调用的那个。

Public Event PrintPage As System.Drawing.Printing.PrintPageEventHandler
Private WithEvents Doc As New Printing.PrintDocument

Public Sub SaveXPSFile()
    Doc.PrinterSettings.PrinterName = "Microsoft XPS Document Writer"
    Doc.PrinterSettings.PrintFileName = "C:\Users\POConnell\Documents\t.xps"
    Doc.Print()
    Doc.Dispose()
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Doc.PrintPage
   'drawing commands go here
End sub

回答by Matt Anderson

It's a couple of months late, but here we go.

晚了几个月,但我们走了。

2 questions, two answers.

2个问题,两个答案。

Question 1: How can I save an xps file by printing to a virtual printer without using the Save File As dialog?

问题 1:如何在不使用“文件另存为”对话框的情况下通过打印到虚拟打印机来保存 xps 文件?

Answer 1: You were close. I think you're looking for

答案 1:你很接近。我想你正在寻找

Doc.DefaultPageSettings.PrinterSettings.PrintToFile = True Doc.DefaultPageSettings.PrinterSettings.PrintFileName = "C:\Users\POConnell\Documents\t.xps"

Doc.DefaultPageSettings.PrinterSettings.PrintToFile = True Doc.DefaultPageSettings.PrinterSettings.PrintFileName = "C:\Users\POConnell\Documents\t.xps"

Here is my implementation: (Legal paper size, landscape mode)

这是我的实现:(合法纸张尺寸,横向模式)

        Using prn As New PrintDocument
            With prn
                .PrinterSettings.PrinterName = "Microsoft XPS Document Writer"
                AddHandler .PrintPage, _
                   AddressOf Me.PrintPageHandler
                .DefaultPageSettings.Landscape = landscape
                .DefaultPageSettings.PaperSize = New PaperSize("Legal", 850, 1400)
                If My.Computer.FileSystem.FileExists("C:\temp\Log.oxps") Then My.Computer.FileSystem.DeleteFile("C:\temp\Log.oxps")
                .DefaultPageSettings.PrinterSettings.PrintToFile = True
                .DefaultPageSettings.PrinterSettings.PrintFileName = "C:\temp\Log.oxps"
                .Print()
                RemoveHandler .PrintPage, _
                   AddressOf Me.PrintPageHandler
            End With
        End Using

As you can see, I use the oxps file format, but it should still work just the same for you.

正如您所看到的,我使用 oxps 文件格式,但它对您来说仍然可以正常工作。

Question 2: it throws an error 'you do not have permission to write to that file...' if I attempt to overwrite an existing file.

问题 2:如果我尝试覆盖现有文件,它会抛出错误“您无权写入该文件...”。

Answer 2: Check if the file already exists prior to printing the file, and delete it if it does. Of course it will fail attempting to create a file that already exists.

回答2:在打印文件之前检查文件是否已经存在,如果存在则将其删除。当然,尝试创建一个已经存在的文件会失败。

For some reason using My.Computer.FileSystem.DeleteFile is faster than the traditional Kill() and System.IO.File.Delete, which both require the thread to sleep for ~1-200ms prior to recreating the file, or else a different access denied error will occur.

出于某种原因,使用 My.Computer.FileSystem.DeleteFile 比传统的 Kill() 和 System.IO.File.Delete 更快,它们都需要线程在重新创建文件之前休眠约 1-200 毫秒,或者不同将发生访问被拒绝错误。

Hopefully this helps someone in the future!

希望这对将来的人有所帮助!