title: VB.net Merge PDF's in a directory that share a similar Filename
body: <p>I currently have a directory where i split a PDF that had multiple headers/barcodes into the following categories:</p>
  
  <p>File# Header Sheet# so it looks like this:</p>
  
  <p>ZTEST01 Cover Sheet 1</p>
  
  <p>ZTEST01 Cover Sheet 2</p>
  
  <p>ZTEST01 Complaint 3</p>
  
  <p>ZTEST01 Complaint 4</p>
  
  <p>ZTEST01 Exhibit 5</p>
  
  <p>ZTEST01 Exhibit 6</p>
  
  <p>ZTEST01 Summons 8</p>
  
  <p>ZTEST01 Summons 9</p>
  
  <p>My goal is to have the code iterate through this directory and merge all the files that have the same header name in the middle together:</p>
  
  <p>ZTEST01 Cover Sheet 1 + ZTEST01 Cover Sheet 2 = ZTEST01 Cover Sheet</p>
  
  <p>This is the following code i have (i was going back and forth with PDFsharp and Bytescout hence i'm leaving the imports alone for now till i figure out what works best):</p>
  
  <pre><code>Imports Bytescout.PDFExtractor
  Imports System.Diagnostics
  Imports System
  Imports System.Collections.Generic
  Imports System.IO
  Imports System.IO.Path
  Imports System.Linq
  Imports System.Text
  Imports System.Threading.Tasks
  Imports PdfSharp.Pdf
  Imports PdfSharp.Pdf.IO
  
  Module Module2
  
  
  
  
      Public Sub Main(ByVal args As String())
          Dim Dir As String = "G:\Word\Department Folders\Pre-Suit\Drafts-IL\2-IL_AttyReview\2018-09\Reviewed\unmerged"
          Dim name As String = "Complaint"
  
          Dim supportedfiles As New List(Of String)()
          For Each files As String In Directory.GetFiles(Dir, "*.pdf")
              Dim filename As String = GetFileName(files).ToLower()
  
              If filename Like name Then
                  supportedfiles.Add(files)
              End If
          Next files
  
  
  
          Dim outputPdfDocument As PdfDocument = New PdfDocument()
  
  
          For Each files As String In supportedfiles
              Merge(outputPdfDocument, files)
  
              Dim Path As String = IO.Path.GetFileNameWithoutExtension(files)
  
              outputPdfDocument.Save(Dir &amp; "\Merge\" &amp; Path &amp; "Complaint" &amp; ".pdf")
          Next
  
          Console.ReadKey()
  
  
      End Sub
  
      Public Sub Merge(ByVal outputPDFDocument As PdfDocument, ByVal pdfFile As String)
              Dim inputPDFDocument As PdfDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import)
              outputPDFDocument.Version = inputPDFDocument.Version
  
              For Each page As PdfPage In inputPDFDocument.Pages
                  outputPDFDocument.AddPage(page)
  
              Next
  
          End Sub
  
  
  End Module
  </code></pre>
  
  <p>I tried using the filename like "Complaint" for now to see if it works but so far it just brings up a blank cmd prompt.</p>
  
  <p>I'd like to do this for </p>
  
  <p>"Cover Sheet"</p>
  
  <p>"Complaint"</p>
  
  <p>"Exhibit"</p>
  
  <p>and "Summons"</p>
  
  <p>Any suggestions would be greatly appreciated.</p>