วิธีแปลง Text file เป็น PDF File
ต้องใช้ iTextSharp.dll
แหล่งอ้างอิง
http://www.codeproject.com/KB/aspnet/Creating_PDF_documents_in.aspx
http://www.mikesdotnetting.com/Article/81/iTextSharp-Working-with-Fonts
http://stackoverflow.com/questions/1374563/itextsharp-changing-font-size-of-pdfptable
Imports System
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.Diagnostics.Process
Public Class frmConvertTextToPDF
Dim TextPath As String ' path of text file
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
OpenText(TextPath) ' find text file
Dim sReader As IO.StreamReader = New IO.StreamReader(TextPath)
TextBox1.AppendText(sReader.ReadToEnd)
ConvertToPDF() 'Process for convert file
'openPDF()
Me.Dispose()
End
End Sub
Sub ConvertToPDF()
Dim doc As Document = New Document
Dim PDFPath As String ' path of pdf file
PDFPath = Replace(TextPath, ".txt", ".pdf")
'set font
Dim formatFont As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, False)
Dim getfont As New Font(formatFont, 10, Font.Italic)
PdfWriter.GetInstance(doc, New FileStream(PDFPath, FileMode.Create))
doc.Open()
doc.Add(New Paragraph(TextBox1.Text, getfont))
doc.Close()
OpenFileDialog1.FileName = PDFPath
Start(OpenFileDialog1.FileName)
End Sub
Sub OpenText(Optional ByRef strPath As String = "")
Dim Response As DialogResult
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "TEXT Files(*.txt)|*.txt|All Files(*.*)|*.*"
Response = OpenFileDialog1.ShowDialog()
If Response <> Windows.Forms.DialogResult.Cancel Then
If OpenFileDialog1.FileName <> "" Then
Start(OpenFileDialog1.FileName)
strPath = OpenFileDialog1.FileName
End If
End If
End Sub
Sub openPDF()
Dim Response As DialogResult
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "PDF Files(*.pdf)|*.pdf|All Files(*.*)|*.*"
Response = OpenFileDialog1.ShowDialog()
If Response <> Windows.Forms.DialogResult.Cancel Then
If OpenFileDialog1.FileName <> "" Then
'WebBrowser1.Navigate(OpenFileDialog1.FileName)
Start(OpenFileDialog1.FileName)
End If
End If
End Sub
End Class
ไม่มีความคิดเห็น:
แสดงความคิดเห็น