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

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

[VB.NET] การแยกหน้าเอกสาร PDF ด้วย Syncfusion Community License

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

311

กระทู้

502

โพสต์

6052

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6052








หลังจากที่เคยแจกโค้ดการรวมหน้าเอกสาร (Merge) ไฟล์ PDF หลายๆไฟล์เข้าด้วยกัน โดยใช้ Syncfusion Community License Free (ของฟรีที่โลกลืม) ออกไปแล้ว คราวนี้เป็นการแยกหน้าเอกสาร หรือ Split กันบ้างครับ โดยแอดมินทำตัวอย่างไว้ให้ดูอยู่ 2 แบบ คือ แยกหน้าเอกสาร PDF ทุกๆหน้า ซึ่งจะแยกเป็นไฟล์ให้อัตโนมัติ เช่น จาก Document-0 โดยจะมีเลขตามหลังตามจำนวนหน้าไปเรื่อยๆ อีกแบบคือ แยกหน้าเอกสารจากการเลือกหน้า เช่น หน้า 2 - 4 แล้วทำการตั้งชื่อไฟล์ขึ้นมาใหม่เพื่อทำการบันทึก ...


## Register & Download Syncfusion Community License Free.
ลิ้งค์สมัคร Syncfusion เพื่อขอรหัสลงทะเบียนด้วย LinkedIn: (แนะนำ)
คลิปวิดีโอสอนการสมัคร Syncfusion เพื่อขอรหัสลงทะเบียนด้วย LinkedIn:
ลิ้งค์การลงทะเบียนด้วย eMail:


มาดูโค้ดฉบับเต็มกันเถอะ ...
  1. Imports Syncfusion.Pdf
  2. Imports Syncfusion.Pdf.Parsing
  3. Imports Syncfusion.Windows.Forms

  4. Public Class frmSplitPdf

  5.     '// PDF Path
  6.     Private strPathPDF As String = ""
  7.     Private strPathOutput As String = ""

  8.     ' / --------------------------------------------------------------------------------
  9.     ' / Get my project path
  10.     ' / AppPath = C:\My Project\bin\debug
  11.     ' / Replace "\bin\debug" with ""
  12.     ' / Return : C:\My Project\
  13.     Function MyPath(ByVal AppPath As String) As String
  14.         '/ Return Value
  15.         MyPath = AppPath.ToLower.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
  16.         '// If not found folder then put the \ (BackSlash) at the end.
  17.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  18.     End Function

  19.     ' / --------------------------------------------------------------------------------
  20.     ' / S T A R T ... H E R E
  21.     ' / --------------------------------------------------------------------------------
  22.     Private Sub frmSplitPdf_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  23.         '// If folders doesn't exist? then create it.
  24.         '// Original PDF Folder.
  25.         If Not System.IO.Directory.Exists(strPathPDF) Then
  26.             System.IO.Directory.CreateDirectory(MyPath(Application.StartupPath) & "PDF")
  27.             strPathPDF = MyPath(Application.StartupPath & "PDF")
  28.         End If
  29.         '// Output PDF Folder.
  30.         If Not System.IO.Directory.Exists(strPathOutput) Then
  31.             System.IO.Directory.CreateDirectory(MyPath(Application.StartupPath) & "Output")
  32.             strPathOutput = MyPath(Application.StartupPath & "Output")
  33.         End If
  34.         '// Disable Open/Save toolbar PdfViewerControl of Syncfusion.
  35.         DirectCast(Me.PdfViewerControl1.Controls(0).Controls(0).Controls(0), ToolStrip).Items(0).Visible = False
  36.         DirectCast(Me.PdfViewerControl1.Controls(0).Controls(0).Controls(0), ToolStrip).Items(1).Visible = False
  37.     End Sub

  38.     ' / --------------------------------------------------------------------------------
  39.     ' / Browse PDF File and Load it into PdfViewerControl.
  40.     ' / --------------------------------------------------------------------------------
  41.     Private Sub btnBrowse_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse.Click
  42.         '/ Declare Open File Dialog @ Run Time.
  43.         Dim dlgOpenFile As OpenFileDialog = New OpenFileDialog()
  44.         Try
  45.             ' / Initialize Open File Dialog.
  46.             With dlgOpenFile
  47.                 .InitialDirectory = strPathPDF
  48.                 .Title = "Select PDF File"
  49.                 .Filter = "PDF Files (*.pdf)|*.pdf"
  50.                 .RestoreDirectory = True
  51.             End With
  52.             '/ If the OK button is selected.
  53.             If dlgOpenFile.ShowDialog() = DialogResult.OK Then
  54.                 txtFileName.Text = dlgOpenFile.FileName
  55.                 '/ Show PDF on PDFViewerControl of Syncfusion.
  56.                 Me.PdfViewerControl1.Load(dlgOpenFile.FileName, "")
  57.                 '/ Load document.
  58.                 Dim LoadDocument As PdfLoadedDocument = New PdfLoadedDocument(txtFileName.Text)
  59.                 '// Go To Sub Program Count Pages.
  60.                 Call CountPage(LoadDocument)
  61.                 LoadDocument.Close(True)
  62.             End If

  63.         Catch ex As Exception
  64.             MessageBoxAdv.Show(ex.Message, "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Warning)
  65.         End Try
  66.     End Sub

  67.     ' / --------------------------------------------------------------------------------
  68.     ' / Split PDF All Pages.
  69.     ' / --------------------------------------------------------------------------------
  70.     Private Sub btnSplitAllPage_Click(sender As System.Object, e As System.EventArgs) Handles btnSplitAllPage.Click
  71.         If txtFileName.Text.Length = 0 Then Return
  72.         Try
  73.             '// Load PDF document ... Imports Syncfusion.Pdf.Parsing
  74.             Dim LoadDocument As PdfLoadedDocument = New PdfLoadedDocument(txtFileName.Text)
  75.             '// Split PDF document with pattern and save each page.
  76.             LoadDocument.Split(strPathOutput & "Document-{0}.pdf")
  77.             '// Close the document
  78.             LoadDocument.Close(True)
  79.             MessageBoxAdv.Show("Split PDF Successfully.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  80.             '//
  81.             Dim dlgOpenFile As OpenFileDialog = New OpenFileDialog()
  82.             ' / Initialize Open File Dialog.
  83.             With dlgOpenFile
  84.                 .InitialDirectory = strPathOutput
  85.                 .Title = "Open PDF File"
  86.                 .Filter = "PDF Files (*.pdf)|*.pdf"
  87.                 .RestoreDirectory = True
  88.             End With
  89.             '/ If the OK button is selected.
  90.             If dlgOpenFile.ShowDialog() = DialogResult.OK Then
  91.                 '/ Show PDF on PDFViewerControl of Syncfusion.
  92.                 Me.PdfViewerControl1.Load(dlgOpenFile.FileName, "")
  93.                 txtFileName.Text = dlgOpenFile.FileName
  94.                 LoadDocument = New PdfLoadedDocument(txtFileName.Text)
  95.                 '// Go To Sub Program Count Pages.
  96.                 Call CountPage(LoadDocument)
  97.             End If
  98.             LoadDocument.Close(True)
  99.             '//
  100.         Catch ex As Exception
  101.             MessageBoxAdv.Show(ex.Message, "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  102.         End Try
  103.     End Sub

  104.     ' / --------------------------------------------------------------------------------
  105.     ' / Split Range Pages.
  106.     ' / --------------------------------------------------------------------------------
  107.     Private Sub btnSplitPage_Click(sender As System.Object, e As System.EventArgs) Handles btnSplitPage.Click
  108.         If txtFileName.Text.Length = 0 Then Return
  109.         Try
  110.             '// Load PDF document ... Imports Syncfusion.Pdf.Parsing
  111.             Dim LoadDocument As PdfLoadedDocument = New PdfLoadedDocument(txtFileName.Text)
  112.             '// Declare Save File Dialog Control @Run Time
  113.             Dim dlgSaveFile As SaveFileDialog = New SaveFileDialog
  114.             '// Initialize Save File Dialog
  115.             With dlgSaveFile
  116.                 .InitialDirectory = strPathOutput
  117.                 .Title = "Save PDF File"
  118.                 .Filter = "Save PDF File |*.pdf"
  119.                 .RestoreDirectory = True
  120.             End With
  121.             '// Save the new PDF document.
  122.             If dlgSaveFile.ShowDialog() = DialogResult.OK Then
  123.                 If txtFileName.Text = dlgSaveFile.FileName Then
  124.                     MessageBoxAdv.Show("This file is already opened. Please close it before saving.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  125.                     Return
  126.                 End If
  127.                 '// Create new PDF document.
  128.                 Dim document As PdfDocument = New PdfDocument()
  129.                 Dim PageFrom As Integer = cmbPageFrom.SelectedIndex
  130.                 Dim PageTo As Integer = cmbPageTo.SelectedIndex
  131.                 '// Import the range of pages from the existing PDF
  132.                 '// If the first page is greater than the last page have to swap values.
  133.                 If PageFrom < PageTo Then
  134.                     document.ImportPageRange(LoadDocument, PageFrom, PageTo)
  135.                 Else
  136.                     document.ImportPageRange(LoadDocument, PageTo, PageFrom)
  137.                 End If
  138.                 '// Save PDF.
  139.                 document.Save(dlgSaveFile.FileName)
  140.                 txtFileName.Text = dlgSaveFile.FileName
  141.                 LoadDocument = New PdfLoadedDocument(txtFileName.Text)
  142.                 Me.PdfViewerControl1.Load(txtFileName.Text, "")
  143.                 Call CountPage(LoadDocument)
  144.                 document.Close(True)
  145.             End If
  146.             '// Close the PDF document.
  147.             LoadDocument.Close(True)
  148.             '//
  149.         Catch ex As Exception
  150.             MessageBoxAdv.Show(ex.Message, "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  151.         End Try
  152.     End Sub

  153.     ' / --------------------------------------------------------------------------------
  154.     ' / Count Pages.
  155.     ' / --------------------------------------------------------------------------------
  156.     Sub CountPage(ByRef doc As PdfLoadedDocument)
  157.         '/ Show PDF on PDFViewerControl of Syncfusion.
  158.         Dim count As Integer = doc.Pages.Count
  159.         cmbPageFrom.Items.Clear()
  160.         cmbPageTo.Items.Clear()
  161.         For pg As Integer = 1 To count
  162.             cmbPageTo.Items.Add(pg)
  163.             cmbPageFrom.Items.Add(pg)
  164.         Next
  165.         cmbPageTo.SelectedIndex = 0
  166.         cmbPageFrom.SelectedIndex = 0
  167.     End Sub

  168.     Private Sub txtFileName_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtFileName.KeyPress
  169.         '/ Protect key press on TextBox Control.
  170.         e.Handled = True
  171.     End Sub

  172.     Private Sub frmSplitPdf_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  173.         Me.Dispose()
  174.         GC.SuppressFinalize(Me)
  175.         Application.Exit()
  176.     End Sub

  177.     Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
  178.         Me.Close()
  179.     End Sub
  180. End Class
คัดลอกไปที่คลิปบอร์ด


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

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

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

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

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

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

GMT+7, 2024-4-26 22:14 , Processed in 0.201931 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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