2013-07-23 146 views
0

這裏我使用vs 2010並在系統中安裝Adobe Reader 11.0。我試過類似的東西 -如何通過VB.net創建PDF文件

Private Sub OpenButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenButton.Click 

    Dim open1 As FileDialog 
    Dim inFileName As String 
    Dim outFileName As String 
    Dim i As Long 
    open1.Filter = "All Files (*.*)|*.*" 
    """ 
    "'""" 
    ""  
End Sub 
+1

使用Itextsharp庫創建Pdf。 –

+1

我不認爲* Adob​​e Reader *允許您*生成* PDF文件。這是一個**讀者**的原因。 – Neolisk

+1

我沒有看到你實際嘗試過的部分。 – jsedano

回答

0

答案取決於您實際上想要打印的內容;對於Excel(舊版本)我通常會打印到postscript打印機驅動程序;然後使用GhostScript命令行將PS轉換爲PDF。

您需要擁有GhostScript文件。這裏是代碼:

Option Explicit 
    Dim PSPrinterSetupName As String 
    Dim svPsFileName As String 
    Dim svPDFName As String 


Sub testRun() 
    'set up a printer using driver "HP C LaserJet 4500-PS" or something similar 
    'name it "PostScript Writer" 
    'set it to "print to file" 

    'set the name of the printer to use 
    PSPrinterSetupName = "PostScript Writer" 

    'set the postscript file name (must be 8.3 notation; 
    'no long names; no spaces; you can move and rename the file later) 
    svPsFileName = "C:\Temp\input1.ps" 

    'set the PDF name (must be 8.3 notation; 
    'no long names; no spaces; you can move and rename the file later) 
    svPDFName = "C:\Temp\Output1.PDF" 

    Call printToPS 
    Call ConvertToPDF 
End Sub 

Sub printToPS() 
    Dim PrinterInUse As String 
    PrinterInUse = Application.ActivePrinter 
    Worksheets("Sheet1").PrintOut ActivePrinter:=PSPrinterSetupName, PrintToFile:=True, PrToFileName:=svPsFileName 
    Application.ActivePrinter = PrinterInUse 
End Sub 

Sub ConvertToPDF() 
    Dim fso 
    Dim lcCmd As String 

    Set fso = CreateObject("Scripting.FileSystemObject") 

    lcCmd = "C:\Progra~1\GS\Misc\GhostS~1\GSWIN32C.EXE " & _ 
    "-q -dNOPAUSE -IC:\Progra~1\GS\Misc\GhostS~1\lib;./fonts " & _ 
    "-sFONTPATH=./fonts -sFONTMAP=C:\Progra~1\GS\Misc\GhostS~1\lib\FONTMAP.GS " & _ 
    "-sDEVICE=pdfwrite -sOUTPUTFILE=" & svPDFName & " -dBATCH " & svPsFileName 
    Shell (lcCmd) 

End Sub