2008-10-02 243 views
2

我试图在Visual Basic 2008打印到PDF打印机编程

我们目前的相关资产以编程方式打印现有文件为PDF格式是: Visual Studio 2008专业 安装Adobe Acrobat 8​​.0专业

我想到获得像ITextSharp这样的sdk,但是对于我正在尝试做的事情来说,似乎过度杀伤了,特别是因为我们有完整版的Adobe。

是否存在的代码相对简单的位打印到PDF打印机(当然它分配给打印到特定位置),还是会需要使用另一个库中打印到PDF?


我想将以前创建的文档打印到pdf文件。在这种情况下,它是一个.snp文件,我想将其制作为.pdf文件,但我认为对于任何文件类型,逻辑都是相同的。


我刚刚尝试了上面的shell执行,它不会执行我想要的方式。因为它会提示我我要打印的位置,并且仍然不打印我想要的位置(多个位置),这对我们创建大量相同的已命名PDF文件至关重要(使用PDF中的不同数据并放置相应的客户端文件夹)


目前的流程是:

  • 转到\\报表服务器\客户端1
  • 用手
  • 副本创建该文件夹中的所有文件SNP的PDF文件pdf到\\网站报告\ client1
  • 然后重复所有100 +客户端大约需要两个小时才能完成,并确认

我知道这是可以做到更好,但我只在这里已经三个月了,到处都是那名多了很多其他紧迫问题即时。我也没有料到那些看起来很微不足道的东西很难编码。

回答

3

这里最重要的一点是PDF非常难。如果您有任何事情可以避免直接创建或编辑PDF文档,我强烈建议您这样做。这听起来像你真正想要的是一个批量SNP到PDF转换器。你甚至可以用现成的产品来做到这一点,甚至根本不需要打开Visual Studio。有人提到了Adobe Distiller Server--检查你的文档是否为Acrobat,我知道它带有基本的Distiller,你也许可以设置Distiller以类似的模式运行,在那里它监视目录A并且吐出任何文件的PDF版本在目录B中显示。

另一种方法:由于您正在使用Access快照,因此最好编写一个VBA脚本,该脚本遍历目录中的所有SNP并将它们打印到安装的PDF打印机。

ETA:如果您需要指定PDF打印机的输出,可能会更困难。我建议将PDF蒸馏器配置为输出到临时目录,这样您可以打印一个,移动结果,然后打印另一个,等等。

+0

唯一的平局,回到使用PDF打印机,是你必须指定文件打印到静态路径。这成为处理大批量流程的问题。 – Koekiebox 2009-09-01 12:14:03

1

你想要做的是找到一个很好的免费的PDF打印机驱动程序。这些作为打印机安装,但不是打印到物理设备,而是将打印机命令呈现为PDF。然后,您可以如上所述使用ShellExecute,或使用内置的.net PrintDocument,通过名称引用PDF“printer”。我发现了一对夫妇free ones,包括产品PrimoBullZip (freedom limited to 10 users)相当快。

看起来SNP文件的Microsoft Access快照。您将不得不寻找一个命令行界面来访问或使用Snapshot Viewer来指定打印机目标。

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

1

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

你看过Adobe Distiller Server吗?您可以使用任何打印机驱动程序生成PostScript文件并将其转换为PDF。 (实际上,PDFCreator也有类似的功能。)

0

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

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

这是可行的,如果你需要。我查看了Adobe PDF参考中的例子,手中输入了它们,并将它们加以处理,直到我可以根据需要让它们工作。如果你会做这很多,这可能是值得的,否则看看一个SDK。

3

这就是我在VBScript中的做法。可能对你不太有用,但可能会让你开始。您需要将PDF制造商(Adobe Acrobat)作为名为“Adobe PDF”的打印机。

'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 
0

我在C#ASP.NET应用程序中遇到了类似的问题。我的解决方案是用一些生成的代码在命令行激发一个LaTeX编译器。这不是一个简单的解决方案,但它产生了一些非常漂亮的.pdf。

0

最佳的Java库iText的,但是从过去一年左右看邮件列表是不是在所有

1

我有同样的挑战,一个简单的任务。我所做的解决方案是购买一个名为PDFTron的组件。它有一个API将pdf文档从无人值守服务发送到打印机。 我在我的博客中发布了一些关于这方面的信息。看一看!

How to print a PDF file programmatically???

+0

链接不再有效 – briddums 2012-05-23 15:10:29

+0

谢谢你让我知道。我会看一看。 – Roberto 2012-08-13 18:33:20

0
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 
0

到其他的答案相似,但要简单得多。我最终将其归结为4行代码,没有外部库(尽管您必须安装Adobe Acrobat并将其配置为PDF的默认值)。

Dim psi As New ProcessStartInfo 
    psi.FileName = "C:\Users\User\file_to_print.pdf" 
    psi.Verb = "print" 
    Process.Start(psi) 

这将打开文件,使用默认设置进行打印,然后关闭。

适应from this C# answer