Vb.net 以编程方式打印到pdf打印机

Vb.net 以编程方式打印到pdf打印机,vb.net,pdf,Vb.net,Pdf,我试图在VisualBasic2008中以编程方式将现有文件打印为PDF 我们的流动相关资产为: Visual Studio 2008专业版 Adobe Acrobat Professional 8.0 我曾考虑过要一个像ITextSharp这样的sdk,但我现在所做的似乎有些过头了,特别是因为我们有完整版本的Adobe 是否有一个相对简单的代码可以打印到PDF打印机(当然也可以将其分配到特定位置),或者是否需要使用另一个库才能打印到PDF 我想将以前创建的文档打印为pdf文件。在本例中,它是

我试图在VisualBasic2008中以编程方式将现有文件打印为PDF

我们的流动相关资产为: Visual Studio 2008专业版 Adobe Acrobat Professional 8.0

我曾考虑过要一个像ITextSharp这样的sdk,但我现在所做的似乎有些过头了,特别是因为我们有完整版本的Adobe

是否有一个相对简单的代码可以打印到PDF打印机(当然也可以将其分配到特定位置),或者是否需要使用另一个库才能打印到PDF


我想将以前创建的文档打印为pdf文件。在本例中,它是一个.snp文件,我希望将其转换为.pdf文件,但我认为对于任何文件类型,逻辑都是相同的


我刚刚尝试了上面的shell execute,但它不会按我希望的方式执行。因为它提示我要打印到哪里,但仍然没有打印到我想要打印的地方(多个位置),这一点至关重要,因为我们创建了许多相同命名的PDF文件(PDF中有不同的数据,并放置在相应的客户端文件夹中)


目前的进程是:

  • 转到\\报表服务器\client1
  • 手动创建文件夹中所有snp文档的pdf文件
  • 将pdf复制到\\website reports\client1
  • 然后对所有100多个客户端重复此操作,大约需要两个小时来完成和验证

我知道这可以做得更好,但我在这里才三个月,还有其他更紧迫的问题。我也没想到这么琐碎的东西会那么难编码。

你想做的是找到一个好的免费PDF打印机驱动程序。这些命令作为打印机安装,但不是打印到物理设备,而是将打印机命令呈现为PDF格式。然后,您可以如上所述执行ShellExecute,也可以使用内置的.net打印文档,按名称引用PDF“打印机”。我很快就找到了一些,包括和的产品

看起来SNP文件是Microsoft Access快照。您必须查找Access或Snapshot Viewer的命令行界面,以便指定打印机目标


我还看到SnapshotViewer下载中包含一个ActiveX控件。您可以尝试在程序中使用它来加载snp文件,然后告诉它打印到哪里,如果它支持该功能。

尝试将ShellExecute与打印谓词一起使用

这是我在谷歌找到的一个博客

提供PDFCreator。它将从任何能够打印的程序中创建PDF,即使是现有的程序。请注意,它基于GhostScript,因此可能不适合您的Acrobat许可证


你调查过了吗?您可以使用任何打印机驱动程序生成PostScript文件,并将其转换为PDF格式。(实际上,PDFCreator也做了类似的事情。)

如果您试图手动生成PDF(使用和SDK或PDF打印机驱动程序),这并不容易。PDF格式参考可从Adobe获得

问题在于,该文件是ASCII和表的混合体,这些表在文件中具有引用对象的二进制偏移量。这是一种有趣的格式,并且非常可扩展,但是编写一个简单的文件很困难


如果你需要的话,这是可行的。我看了AdobePDF参考资料中的示例,手工输入并反复研究,直到可以根据需要使用它们。如果您经常这样做,那么它可能是值得的,否则请查看SDK。

这就是我在VBScript中所做的。可能对您没有多大帮助,但可能会让您开始。您需要一个名为“adobe PDF”的PDF maker(adobe acrobat)作为打印机

'PDF_通配符=“*.PDF”
'PrnName=“Adobe PDF”
子PrintToPDF(ReportName为字符串,TempPath为字符串_
OutputName作为字符串,OutputDir作为字符串_
可选参数(如整数=1)
Dim rpt作为报告
Dim NewFileName作为字符串,TempFileName作为字符串
---打印机设置---
DoCmd.OpenReport报告名,视图:=acViewPreview,窗口模式:=acHidden
设置rpt=报告(报告名称)
设置rpt.Printer=Application.Printers(PrnName)
"定位",
如果RPT方向=1,则
rpt.Printer.Orientation=acPRORPortrait
其他的
rpt.Printer.Orientation=acPRORLandscape
如果结束
“---打印---
'打印(打开)并关闭实际报告,而不保存更改
DoCmd.OpenReport报告名,视图:=acViewNormal,窗口模式:=acHidden
'等待文件完全创建
调用waitForFile(临时路径、报告名和PDF_-EXT)
'DoCmd.Close acReport,ReportName,acSaveNo
DoCmd.Close acReport,ReportName
TempFileName=TempPath&ReportName&PDF_EXT'默认PDF文件名
NewFileName=OutputDir&OutputName&PDF\u EXT'新文件名
'由COM接口引起的陷阱错误
错误时转到错误文件
FileCopy TempFileName,NewFileName
'删除临时路径中的所有PDF
(这就是为什么您应该将其分配到pdf目录)
错误时转到错误文件
清除临时路径和PDF_通配符
退出pdfTest:
设置rpt=Nothing
出口接头
Err_File:'复制文件时的错误处理例程
选择案例错误编号“评估错误编号”。
案例53,70'“权限被拒绝”和“未找到文件”msgs
“等3秒钟。
Debug.Print“Error”&Err.Number&“:”&Err.Description&vbCr&“请稍等几秒钟,然后单击“确定”,vbInformation,“复制文件命令”
呼叫睡眠(2,错误)
简历
其他情况
MsgBox错误号&“:”&错误说明
恢复退出\u pdfTest
结束选择
简历
端接头
子waitForFile(ByVal路径名作为字符串,ByVal tempfile作为字符串)
使用Application.FileSearch
.新闻研究
.LookIn=路径名
.SearchSubFolders=True
.filename=tempfile
.MatchTextExactly=True
'.FileType=msoFileTypeAllFiles
以
'PDF_WILDCARD = "*.pdf"
'PrnName = "Adobe PDF"
Sub PrintToPDF(ReportName As String, TempPath As String, _
               OutputName As String, OutputDir As String, _
               Optional RPTOrientation As Integer = 1)

  Dim rpt As Report
  Dim NewFileName As String, TempFileName As String

  '--- Printer Set Up ---
  DoCmd.OpenReport ReportName, View:=acViewPreview, WindowMode:=acHidden
  Set rpt = Reports(ReportName)
  Set rpt.Printer = Application.Printers(PrnName)

  'Set up orientation
  If RPTOrientation = 1 Then
    rpt.Printer.Orientation = acPRORPortrait
  Else
    rpt.Printer.Orientation = acPRORLandscape
  End If

  '--- Print ---
  'Print (open) and close the actual report without saving changes
  DoCmd.OpenReport ReportName, View:=acViewNormal, WindowMode:=acHidden

  ' Wait until file is fully created
  Call waitForFile(TempPath, ReportName & PDF_EXT)

  'DoCmd.Close acReport, ReportName, acSaveNo
  DoCmd.Close acReport, ReportName

  TempFileName = TempPath & ReportName & PDF_EXT 'default pdf file name
  NewFileName = OutputDir & OutputName & PDF_EXT 'new file name

  'Trap errors caused by COM interface
  On Error GoTo Err_File
  FileCopy TempFileName, NewFileName

  'Delete all PDFs in the TempPath
  '(which is why you should assign it to a pdf directory)
  On Error GoTo Err_File
  Kill TempPath & PDF_WILDCARD

Exit_pdfTest:
  Set rpt = Nothing
  Exit Sub

Err_File:    ' Error-handling routine while copying file
  Select Case Err.Number    ' Evaluate error number.
      Case 53, 70   ' "Permission denied" and "File Not Found" msgs
          ' Wait 3 seconds.
          Debug.Print "Error " & Err.Number & ": " & Err.Description & vbCr & "Please wait a few seconds and click OK", vbInformation, "Copy File Command"
          Call sleep(2, False)
          Resume
      Case Else
          MsgBox Err.Number & ": " & Err.Description
          Resume Exit_pdfTest
  End Select

  Resume

End Sub



Sub waitForFile(ByVal pathName As String, ByVal tempfile As String)
    With Application.FileSearch
        .NewSearch
        .LookIn = pathName
        .SearchSubFolders = True
        .filename = tempfile
        .MatchTextExactly = True
        '.FileType = msoFileTypeAllFiles
    End With
    Do While True
       With Application.FileSearch
           If .Execute() > 0 Then
               Exit Do
           End If
       End With
    Loop
End Sub



Public Sub sleep(seconds As Single, EventEnable As Boolean)
    On Error GoTo errSleep
    Dim oldTimer As Single

    oldTimer = Timer
    Do While (Timer - oldTimer) < seconds
       If EventEnable Then DoEvents
    Loop

errSleep:
       Err.Clear
End Sub
Imports System.Drawing.Printing
Imports System.Reflection
Imports System.Runtime.InteropServices
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim pkInstalledPrinters As String

    ' Find all printers installed
    For Each pkInstalledPrinters In _
        PrinterSettings.InstalledPrinters
        printList.Items.Add(pkInstalledPrinters)
    Next pkInstalledPrinters

    ' Set the combo to the first printer in the list
    If printList.Items.Count > 0 Then
        printList.SelectedItem = 0
    End If
    End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try

        Dim pathToExecutable As String = "AcroRd32.exe"
        Dim sReport = " " 'pdf file that you want to print
        'Dim SPrinter = "HP9F77AW (HP Officejet 7610 series)" 'Name Of printer
        Dim SPrinter As String
        SPrinter = printList.SelectedItem
        'MessageBox.Show(SPrinter)
        Dim starter As New ProcessStartInfo(pathToExecutable, "/t """ + sReport + """ """ + SPrinter + """")
        Dim Process As New Process()
        Process.StartInfo = starter
        Process.Start()
        Process.WaitForExit(10000)
        Process.Kill()
        Process.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message) 'just in case if something goes wrong then we can suppress the programm and investigate
    End Try
End Sub
End Class
    Dim psi As New ProcessStartInfo
    psi.FileName = "C:\Users\User\file_to_print.pdf"
    psi.Verb = "print"
    Process.Start(psi)