ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 529|ตอบกลับ: 0

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

[คัดลอกลิงก์]

311

กระทู้

502

โพสต์

6060

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6060

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





Add References ... เพื่อไว้เปรียบเทียบหรืออ้างอิง กรณีที่ Syncfusion มีเวอร์ชั่นไม่ตรงกันกับแอดมิน ...

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

  7. Public Class frmPdfToImageSf

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

  10.     Private Sub btnWordDoc_Click(sender As System.Object, e As System.EventArgs) Handles btnWordDoc.Click
  11.         Dim dlgOpenFile As OpenFileDialog = New OpenFileDialog()
  12.         ' / Open File Dialog
  13.         With dlgOpenFile
  14.             .Filter = "PDF|*.pdf"
  15.             .Title = "Select PDF File"
  16.             .DefaultExt = "pdf"
  17.             .InitialDirectory = MyPath(Application.StartupPath) & "PDF"
  18.             .RestoreDirectory = True
  19.         End With
  20.         '/ Select OK after Browse ...
  21.         If dlgOpenFile.ShowDialog() = DialogResult.OK Then
  22.             txtFileName.Text = dlgOpenFile.FileName
  23.             strFileName = Path.GetFileNameWithoutExtension(dlgOpenFile.FileName)
  24.             Dim pdfFile As String = dlgOpenFile.FileName
  25.             '/ Load PDF into PDFViewerControl.
  26.             Me.PdfViewerControl1.Load(pdfFile, "")
  27.         End If
  28.     End Sub

  29.     ' / --------------------------------------------------------------------------------
  30.     ' / Convert PDF to Images.
  31.     ' / --------------------------------------------------------------------------------
  32.     Private Sub btnConvert_Click(sender As System.Object, e As System.EventArgs) Handles btnConvert.Click
  33.         If txtFileName.Text.Trim.Length = 0 Then Return
  34.         Dim dlgSaveFile As New SaveFileDialog
  35.         With dlgSaveFile
  36.             .Title = "Select images"
  37.             .Filter = "JPEG Image (.jpg)|*.jpg|Png Image (.png)|*.png|Bitmap Image (.bmp)|*.bmp;"
  38.             .RestoreDirectory = True
  39.             .InitialDirectory = strPathImages
  40.             .FileName = strFileName
  41.         End With
  42.         '//
  43.         If dlgSaveFile.ShowDialog() = DialogResult.OK Then
  44.             Try
  45.                 Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument(txtFileName.Text)
  46.                 Dim image As Bitmap() = PdfViewerControl1.ExportAsImage(0, loadedDocument.Pages.Count - 1)
  47.                 '// Export all the pages as images at the specific page range.
  48.                 For i As Integer = 0 To image.Length - 1
  49.                     '// Saves the Image in the appropriate ImageFormat based upon the
  50.                     '// file type selected in the dialog box.
  51.                     Select Case dlgSaveFile.FilterIndex
  52.                         Case 1
  53.                             image(i).Save(strPathImages & strFileName + "-" + i.ToString() + ".jpg", ImageFormat.Jpeg)
  54.                         Case 2
  55.                             image(i).Save(strPathImages & strFileName + "-" + i.ToString() + ".png", ImageFormat.Png)
  56.                         Case 3
  57.                             image(i).Save(strPathImages & strFileName + "-" + i.ToString() + ".bmp", ImageFormat.Bmp)
  58.                     End Select
  59.                 Next
  60.                 Process.Start(strPathImages)
  61.             Catch ex As Exception
  62.                 MessageBox.Show(ex.Message)
  63.             End Try
  64.         End If
  65.     End Sub

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

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

  75. #Region "FUNCTION"
  76.     Function MyPath(ByVal AppPath As String) As String
  77.         '/ Return Value
  78.         MyPath = AppPath.ToLower.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "").Replace("\bin\x86\release", "")
  79.         '// If not found folder then put the \ (BackSlash) at the end.
  80.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  81.     End Function
  82. #End Region
  83. End Class
คัดลอกไปที่คลิปบอร์ด

ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...


ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-30 01:43 , Processed in 0.095729 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้