在 VB.net 中打印外部 PDF 文档 [英] Printing an external PDF document in VB.net

查看:89
本文介绍了在 VB.net 中打印外部 PDF 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道以前有人问过这个问题,但我的情况有点不稳定.

I know this question has been asked before, but my situation is a bit wonky.

基本上,我正在尝试打印使用以前的 Windows 窗体生成的 PDF 文件.我可以找到文件没问题,我使用了以下代码,我在 MSDN 的帮助论坛上找到了:

Basically, I'm trying to print a PDF file that I've generated using a previous Windows Form. I can find the file no problem, and I used the following code which I found off MSDN's help forums:

Dim p As New System.Diagnostics.ProcessStartInfo()
p.Verb = "print"
p.WindowStyle = ProcessWindowStyle.Hidden
p.FileName = "C:\534679.pdf"   'This is the file name
p.UseShellExecute = True
System.Diagnostics.Process.Start(p)

到目前为止一切都很好,但每次我按下按钮运行这段代码时,它一直要求我将其另存为 PDF 文件,如下所示:

So far so good, but everytime I press the button to run this code, it keeps asking me to save it as a PDF file instead, as shown below:

我也试过在 Windows 窗体中添加 PrintDialog,让它弹出,我可以从那里选择我想要使用的打印机,但即使在选择打印机后它仍然要求我打印到 PDF 文档相反.

I've also tried adding a PrintDialog to the Windows Form, getting it to pop up, and I can select the printer I want to use from there, but even after selecting the printer it still asks me to print to PDF Document instead.

我做错了什么?

推荐答案

首先,要能够选择打印机,您必须使用 PrintDialogPrintDocument> 将要打印的图形发送到选定的打印机.

First, to be able to select a Printer, you'll have to use a PrintDialog and PrintDocument to send graphics to print to the selected printer.

Imports System.Drawing.Printing

    Private WithEvents p_Document As PrintDocument = Nothing

    Private Sub SelectPrinterThenPrint()
        Dim PrintersDialog As New PrintDialog()

        If PrintersDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
            Try
                p_Document = New PrintDocument()
                PrintersDialog.Document = p_Document
                AddHandler p_Document.PrintPage, AddressOf HandleOnPrintPage

            Catch CurrentException As Exception

            End Try
        End If
    End Sub

    Private Sub HandleOnPrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles p_Document.PrintPage
        Dim MorePagesPending As Boolean = False

        'e.Graphics.Draw...(....)
        'e.Graphics.DrawString(....)
        ' Draw everything...

        If MorePagesPending Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If
    End Sub

这就是我正在做的事情,因为我通常有要打印的自定义对象.

That's what I'm doing since I usually have custom objects to print.

但是要打印 PDF 文件,您必须了解 PDF 对 dotNet 来说绝对没有.与位图 (.bmp) 或 Ping 图像 (.png) 等常见图像不同,dotNet 似乎没有任何用于读取、显示和打印 PDF 文件的内置解析器/解码器.

But to print PDF Files, you must understand that PDF means absolutely nothing to dotNet. Unlike common images like Bitmaps (.bmp) or Ping images (.png) the dotNet doesn't seem to have any inbuilt parser/decoder for reading, displaying and printing PDF files.

因此,您必须使用第三方应用程序、第三方或您自己的自定义 PDF 解析器/布局生成器才能将要打印的页面发送到您的打印机.

So you must use a third party application, thrid party library or your own custom PDF parser/layout generator in order to be able to send pages to print to your printer.

这就是您无法使用打印"命令启动 Acrobat Reader 隐藏(不可见)进程的原因.您将无法选择打印机,而是直接选择默认打印机!

That's why you can't launch a hidden (not visible) process of Acrobat Reader with the command "print". You won't be able to select a printer but will direct to the default one instead !

然而,您可以启动 Acrobat Reader 进程来打开文件,并在 Acrobat Reader 内进行打印操作(选择打印机)(您现在不在 dotNet 编码中)

You can however launch the Acrobat Reader process just to open the file, and do the printing manipulations (select a printer) inside Acrobat Reader (you're outside dotNet coding now)

您也可以通过打开 Acrobat Reader 选择另一台默认打印机并在 实际工作打印机 上打印一个空白页的解决方法.这应该取消选择您的 FoxIt 东西,转而使用实际的打印机..

A workaround for your may aslo to select another default printer by opening Acrobat Reader, and print one blank page on an actual working printer. This should deselect your FoxIt thing in favour of an actual printer..

这篇关于在 VB.net 中打印外部 PDF 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆