| 
 | 
 
  
 
การ Merge (เมิร์จ) หรือการรวมหน้าเอกสารไฟล์ PDF ในโค้ดตัวอย่างชุดนี้ จะสามารถเลือกจากหน้าเอกสาร PDF เพียง 1 ไฟล์ เพื่อมาทำการบันทึกเป็นไฟล์ใหม่ก็ได้ หรือเลือกหน้าจาก 2 ไฟล์ เพื่อมาทำการรวมกันเข้าไป และสุดท้ายเป็นการ Merge รวมไฟล์ทั้ง 2 ชุดเข้าหากันได้ทั้งหมดทุกหน้า ...  
 
Add References ... ย้ำว่า Syncfusion Community จะต้องใช้ Net FrameWork ตั้งแต่เวอร์ชั่น 4.0 ขึ้นไป  เท่านั้น (Client จะใช้งานไม่ได้)  
  
 
มาดูโค้ดฉบับเต็มกันเถอะ ...  
- Imports Syncfusion.Pdf.Parsing
 
 - Imports Syncfusion.Pdf
 
 - Imports System.IO
 
  
- Public Class frmMergePDF
 
  
-     Dim GetPath As String = MyPath(Application.StartupPath)
 
 -     '// พาธและชื่อไฟล์ที่เป็นเอ้าพุท
 
 -     Dim PdfFileOutput As String = GetPath & "Sample.pdf"
 
  
-     Private Sub btnMergePage_Click(sender As System.Object, e As System.EventArgs) Handles btnMergePage.Click
 
 -         '/ Loads a document
 
 -         Dim FirstDocument As New PdfLoadedDocument(GetPath & "FirstPDF.pdf")
 
 -         Dim SecondDocument As New PdfLoadedDocument(GetPath & "SecondPDF.pdf")
 
 -         '/ Create a new document
 
 -         Dim Document As New PdfDocument()
 
 -         Try
 
 -             '// โหลดไฟล์ PDF ตัวแรก แล้ววนรอบตามจำนวนหน้าที่ต้องการ 
 
 -             '// เป็นการเลือกหมายเลขหน้า มาทำการจัดเรียง หรือสร้างในไฟล์ PDF ตัวใหม่
 
 -             For i As Integer = 0 To 0 'FirstDocument.Pages.Count - 1
 
 -                 '/ Imports loaded document page to the new document
 
 -                 Document.ImportPage(FirstDocument, i)
 
 -                 '/ Save the document
 
 -                 Document.Save(PdfFileOutput)
 
 -             Next
 
 -             '// หากไม่ต้องการเอกสารไฟล์ PDF ชุดที่ 2 ก็ตัดชุดนี้ทิ้งไป
 
 -             With Document
 
 -                 '/ ดึงไฟล์ PDF ตัวที่ 2 เข้ามา ตัวอย่างคือเลือกแค่หน้าแรกมาต่อท้ายเข้าไป
 
 -                 .ImportPage(SecondDocument, 0)  '<-- เพิ่มหน้าแรกสุด
 
 -                 '.ImportPage(SecondDocument, 2) '<-- เพิ่มหน้าที่ 3 (Index = 2)
 
 -                 '/ บันทึก Document
 
 -                 .Save(PdfFileOutput)
 
 -                 '/ Closes the documents
 
 -                 .Close(True)
 
 -             End With
 
 -             FirstDocument.Close(True)
 
 -             SecondDocument.Close(True)
 
 -             '/ Display new PDF
 
 -             Me.PdfViewerControl1.Load(PdfFileOutput, "")
 
 -             '//
 
 -         Catch ex As Exception
 
 -             MessageBox.Show(ex.Message)
 
 -         End Try
 
 -         Document.Dispose()
 
 -         FirstDocument.Dispose()
 
 -         SecondDocument.Dispose()
 
 -     End Sub
 
  
-     Private Sub btnMergeAll_Click(sender As System.Object, e As System.EventArgs) Handles btnMergeAll.Click
 
 -         Try
 
 -             Dim FinalDoc As New PdfDocument()
 
 -             ' Creates a string array of source files to be merged.
 
 -             '/ Loads a document
 
 -             Dim Source As String() = {GetPath & "FirstPDF.pdf", GetPath & "SecondPDF.pdf"}
 
 -             '/ Merges PDFDocument.
 
 -             PdfDocument.Merge(FinalDoc, Source)
 
 -             '/ Saves the final document
 
 -             FinalDoc.Save("Sample.pdf")
 
 -             '/ Saves the final document
 
 -             FinalDoc.Save(PdfFileOutput)
 
 -             '/ Closes the document
 
 -             FinalDoc.Close(True)
 
 -             '/ Display new PDF
 
 -             Me.PdfViewerControl1.Load(PdfFileOutput, "")
 
 -             '//
 
 -             FinalDoc.Dispose()
 
 -         Catch ex As Exception
 
 -             MessageBox.Show(ex.Message)
 
 -         End Try
 
 -     End Sub
 
  
-     Function MyPath(ByVal AppPath As String) As String
 
 -         '/ MessageBox.Show(AppPath);
 
 -         AppPath = AppPath.ToLower()
 
 -         '/ Return Value
 
 -         MyPath = AppPath.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
 
  
-     Private Sub frmMergePDF_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
 
 -         Me.Dispose()
 
 -         GC.SuppressFinalize(Me)
 
 -         Application.Exit()
 
 -     End Sub
 
  
- End Class
 
  คัดลอกไปที่คลิปบอร์ด 
ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้จากที่นี่ ...  
 |   
ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง
คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน  
 
x
 
 
 
 
 |