[C#.NET][VB.NET] TXT 轉 PDF

[C#.NET][VB.NET] TXT 轉 PDF

1.加入Crystal Report項目

1

2.加入參數欄位,並命名PDF

1

3.將欄位拖拉至”細目”,把框框拉大,產生出來的報表可以裝比較多東西

1

4.貼下以下程式碼即可

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class Form1
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Setting Approprite parametes
        With OpenFileDialog1
            .CheckFileExists = True
            .FileName = ""
 
            'Making Only Text Files to Appear
            '.Filter = "Text files|*.txt"
            .FilterIndex = 1
 
            'Disabling the Multiple Selection of File 
            .Multiselect = False
            .RestoreDirectory = True
        End With
 
        If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
            txtFrom.Text = OpenFileDialog1.FileName
        End If
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If txtFrom.Text = "" Then
            MsgBox("Please Select a Text File")
            Exit Sub
        End If
 
        Dim PdfName As String = txtFrom.Text.TrimEnd.Replace(".txt", ".pdf")
        'Creating an Object for the Crystal Report
        Dim ObjReport As New CrystalReport1
 
        Try
            'File To String
            Dim fileContents As String
            fileContents = My.Computer.FileSystem.ReadAllText(txtFrom.Text.TrimEnd)
 
            'Passes the String to the Crystal Report
            ObjReport.SetParameterValue("PDF", fileContents.Trim)
 
            'Finally Using the Export Option from the Crystal Report,
            'the File has been Converted To PDF
            ObjReport.ExportToDisk(ExportFormatType.PortableDocFormat, PdfName)
 
        Catch ex As Exception
            MsgBox("Not Converted", MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, ex.Message)
        Finally
            'Destructing the Objects
            ObjReport.Close()
            ObjReport = Nothing
        End Try
 
        MsgBox(PdfName, MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly, "Successfully Converted")
    End Sub
End Class

 

 

 

 

 

 

TXT檔若是有中文字,記得存成Unicode

最近工作忙到沒什麼時間寫細部說明,連C#的用法都省了,今天先到這邊改天再補寫C#用法;看範例使用上應該不會很難,這報表還真的蠻好玩的。

範例下載:VB.NET TXT2PDF.rar

==============================================================================

今天也把C#的的用法用出來了

範例下載:C#.NET TXT2PDF.rar


若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo