thongkorn โพสต์ 2023-9-23 15:53:22

[VB.NET] โค้ดการแปลงไฟล์ PDF ให้เป็นไฟล์ภาพ (Images) ด้วย Syncfusion

โค้ดการแปลงไฟล์ PDF ให้เป็นไฟล์ภาพ (Images) ด้วย Syncfusion ...

http://www.g2gsoft.com/webboard/images/VBNet/pdftoimage.png


http://www.g2gsoft.com/webboard/images/VBNet/pdftoimageref.png
Add References ... เพื่อไว้เปรียบเทียบหรืออ้างอิง กรณีที่ Syncfusion มีเวอร์ชั่นไม่ตรงกันกับแอดมิน ...

มาดูโค้ดฉบับเต็มกันเถอะ ...
Imports Syncfusion.Pdf.Parsing
Imports Syncfusion.Windows.Forms.PdfViewer
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports Syncfusion.Pdf

Public Class frmPdfToImageSf

    Private strFileName As String
    Private strPathImages As String = MyPath(Application.StartupPath) & "Images\"

    Private Sub btnWordDoc_Click(sender As System.Object, e As System.EventArgs) Handles btnWordDoc.Click
      Dim dlgOpenFile As OpenFileDialog = New OpenFileDialog()
      ' / Open File Dialog
      With dlgOpenFile
            .Filter = "PDF|*.pdf"
            .Title = "Select PDF File"
            .DefaultExt = "pdf"
            .InitialDirectory = MyPath(Application.StartupPath) & "PDF"
            .RestoreDirectory = True
      End With
      '/ Select OK after Browse ...
      If dlgOpenFile.ShowDialog() = DialogResult.OK Then
            txtFileName.Text = dlgOpenFile.FileName
            strFileName = Path.GetFileNameWithoutExtension(dlgOpenFile.FileName)
            Dim pdfFile As String = dlgOpenFile.FileName
            '/ Load PDF into PDFViewerControl.
            Me.PdfViewerControl1.Load(pdfFile, "")
      End If
    End Sub

    ' / --------------------------------------------------------------------------------
    ' / Convert PDF to Images.
    ' / --------------------------------------------------------------------------------
    Private Sub btnConvert_Click(sender As System.Object, e As System.EventArgs) Handles btnConvert.Click
      If txtFileName.Text.Trim.Length = 0 Then Return
      Dim dlgSaveFile As New SaveFileDialog
      With dlgSaveFile
            .Title = "Select images"
            .Filter = "JPEG Image (.jpg)|*.jpg|Png Image (.png)|*.png|Bitmap Image (.bmp)|*.bmp;"
            .RestoreDirectory = True
            .InitialDirectory = strPathImages
            .FileName = strFileName
      End With
      '//
      If dlgSaveFile.ShowDialog() = DialogResult.OK Then
            Try
                Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument(txtFileName.Text)
                Dim image As Bitmap() = PdfViewerControl1.ExportAsImage(0, loadedDocument.Pages.Count - 1)
                '// Export all the pages as images at the specific page range.
                For i As Integer = 0 To image.Length - 1
                  '// Saves the Image in the appropriate ImageFormat based upon the
                  '// file type selected in the dialog box.
                  Select Case dlgSaveFile.FilterIndex
                        Case 1
                            image(i).Save(strPathImages & strFileName + "-" + i.ToString() + ".jpg", ImageFormat.Jpeg)
                        Case 2
                            image(i).Save(strPathImages & strFileName + "-" + i.ToString() + ".png", ImageFormat.Png)
                        Case 3
                            image(i).Save(strPathImages & strFileName + "-" + i.ToString() + ".bmp", ImageFormat.Bmp)
                  End Select
                Next
                Process.Start(strPathImages)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
      End If
    End Sub

    Private Sub txtFileName_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtFileName.KeyPress
      ' / Prevents pressing any keys.
      e.Handled = True
    End Sub

    Private Sub frmDoc2Pdf_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
      Me.Dispose()
      GC.SuppressFinalize(Me)
      Application.Exit()
    End Sub

#Region "FUNCTION"
    Function MyPath(ByVal AppPath As String) As String
      '/ Return Value
      MyPath = AppPath.ToLower.Replace("\bin\debug", "\").Replace("\bin\release", "\").Replace("\bin\x86\debug", "\").Replace("\bin\x86\release", "\")
      '// If not found folder then put the \ (BackSlash) at the end.
      If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
    End Function
#End Region
End Class
ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...


หน้า: [1]
ดูในรูปแบบกติ: [VB.NET] โค้ดการแปลงไฟล์ PDF ให้เป็นไฟล์ภาพ (Images) ด้วย Syncfusion