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

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

[VB.NET] การ Merge หรือรวมหน้าเอกสารไฟล์ PDF ด้วยของฟรีจากค่าย Syncfusion

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

308

กระทู้

498

โพสต์

5971

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
5971



การ Merge (เมิร์จ) หรือการรวมหน้าเอกสารไฟล์ PDF ในโค้ดตัวอย่างชุดนี้ จะสามารถเลือกจากหน้าเอกสาร PDF เพียง 1 ไฟล์ เพื่อมาทำการบันทึกเป็นไฟล์ใหม่ก็ได้ หรือเลือกหน้าจาก 2 ไฟล์ เพื่อมาทำการรวมกันเข้าไป และสุดท้ายเป็นการ Merge รวมไฟล์ทั้ง 2 ชุดเข้าหากันได้ทั้งหมดทุกหน้า ...

Add References ... ย้ำว่า Syncfusion Community จะต้องใช้ Net FrameWork ตั้งแต่เวอร์ชั่น 4.0 ขึ้นไป  เท่านั้น (Client จะใช้งานไม่ได้)


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

  4. Public Class frmMergePDF

  5.     Dim GetPath As String = MyPath(Application.StartupPath)
  6.     '// พาธและชื่อไฟล์ที่เป็นเอ้าพุท
  7.     Dim PdfFileOutput As String = GetPath & "Sample.pdf"

  8.     Private Sub btnMergePage_Click(sender As System.Object, e As System.EventArgs) Handles btnMergePage.Click
  9.         '/ Loads a document
  10.         Dim FirstDocument As New PdfLoadedDocument(GetPath & "FirstPDF.pdf")
  11.         Dim SecondDocument As New PdfLoadedDocument(GetPath & "SecondPDF.pdf")
  12.         '/ Create a new document
  13.         Dim Document As New PdfDocument()
  14.         Try
  15.             '// โหลดไฟล์ PDF ตัวแรก แล้ววนรอบตามจำนวนหน้าที่ต้องการ
  16.             '// เป็นการเลือกหมายเลขหน้า มาทำการจัดเรียง หรือสร้างในไฟล์ PDF ตัวใหม่
  17.             For i As Integer = 0 To 0 'FirstDocument.Pages.Count - 1
  18.                 '/ Imports loaded document page to the new document
  19.                 Document.ImportPage(FirstDocument, i)
  20.                 '/ Save the document
  21.                 Document.Save(PdfFileOutput)
  22.             Next
  23.             '// หากไม่ต้องการเอกสารไฟล์ PDF ชุดที่ 2 ก็ตัดชุดนี้ทิ้งไป
  24.             With Document
  25.                 '/ ดึงไฟล์ PDF ตัวที่ 2 เข้ามา ตัวอย่างคือเลือกแค่หน้าแรกมาต่อท้ายเข้าไป
  26.                 .ImportPage(SecondDocument, 0)  '<-- เพิ่มหน้าแรกสุด
  27.                 '.ImportPage(SecondDocument, 2) '<-- เพิ่มหน้าที่ 3 (Index = 2)
  28.                 '/ บันทึก Document
  29.                 .Save(PdfFileOutput)
  30.                 '/ Closes the documents
  31.                 .Close(True)
  32.             End With
  33.             FirstDocument.Close(True)
  34.             SecondDocument.Close(True)
  35.             '/ Display new PDF
  36.             Me.PdfViewerControl1.Load(PdfFileOutput, "")
  37.             '//
  38.         Catch ex As Exception
  39.             MessageBox.Show(ex.Message)
  40.         End Try
  41.         Document.Dispose()
  42.         FirstDocument.Dispose()
  43.         SecondDocument.Dispose()
  44.     End Sub

  45.     Private Sub btnMergeAll_Click(sender As System.Object, e As System.EventArgs) Handles btnMergeAll.Click
  46.         Try
  47.             Dim FinalDoc As New PdfDocument()
  48.             ' Creates a string array of source files to be merged.
  49.             '/ Loads a document
  50.             Dim Source As String() = {GetPath & "FirstPDF.pdf", GetPath & "SecondPDF.pdf"}
  51.             '/ Merges PDFDocument.
  52.             PdfDocument.Merge(FinalDoc, Source)
  53.             '/ Saves the final document
  54.             FinalDoc.Save("Sample.pdf")
  55.             '/ Saves the final document
  56.             FinalDoc.Save(PdfFileOutput)
  57.             '/ Closes the document
  58.             FinalDoc.Close(True)
  59.             '/ Display new PDF
  60.             Me.PdfViewerControl1.Load(PdfFileOutput, "")
  61.             '//
  62.             FinalDoc.Dispose()
  63.         Catch ex As Exception
  64.             MessageBox.Show(ex.Message)
  65.         End Try
  66.     End Sub

  67.     Function MyPath(ByVal AppPath As String) As String
  68.         '/ MessageBox.Show(AppPath);
  69.         AppPath = AppPath.ToLower()
  70.         '/ Return Value
  71.         MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "").Replace("\bin\x86\release", "")
  72.         '// If not found folder then put the \ (BackSlash) at the end.
  73.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  74.     End Function

  75.     Private Sub frmMergePDF_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  76.         Me.Dispose()
  77.         GC.SuppressFinalize(Me)
  78.         Application.Exit()
  79.     End Sub

  80. End Class
คัดลอกไปที่คลิปบอร์ด

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

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

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

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

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

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

GMT+7, 2024-3-28 23:34 , Processed in 0.126382 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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