How to merge PDF files by their similar file name in a directory?

I currently have a directory where i split a PDF that had multiple headers/barcodes into the following categories:

File# Header Sheet# so it looks like this:

ZTEST01 Cover Sheet 1

ZTEST01 Cover Sheet 2

ZTEST01 Complaint 3

ZTEST01 Complaint 4

ZTEST01 Exhibit 5

ZTEST01 Exhibit 6

ZTEST01 Summons 8

ZTEST01 Summons 9

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:

ZTEST01 Cover Sheet 1 + ZTEST01 Cover Sheet 2 = ZTEST01 Cover Sheet

This is the following code i have which mergers a PDF file number with its corresponding filenumber that has an “_” and 1

So like TP031337 and TP031337_1 merge together as one file.

using PdfSharp.Pdf; using PdfSharp.Pdf.IO; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { private const string dir = @"C:UserstblockDesktopIMAGES"; static void Main(string[] args) { //files in files folder and named like: TP031041 TP031041 TP031337 TP031337_1 File.SetAttributes(dir, FileAttributes.Normal); string[] files = Directory.GetFiles(dir, "*.pdf"); IEnumerable<IGrouping<string, string>> groups = files.GroupBy(n => n.Split('.')[0].Split('_')[0]); foreach (var items in groups) { Console.WriteLine(items.Key); PdfDocument outputPDFDocument = new PdfDocument(); foreach (var pdfFile in items) { Merge(outputPDFDocument, pdfFile); } outputPDFDocument.Save(Path.GetDirectoryName(items.Key) + @"Merge" + Path.GetFileNameWithoutExtension(items.Key) + ".pdf"); } Console.ReadKey(); } private static void Merge(PdfDocument outputPDFDocument, string pdfFile) { PdfDocument inputPDFDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import); outputPDFDocument.Version = inputPDFDocument.Version; foreach (PdfPage page in inputPDFDocument.Pages) { outputPDFDocument.AddPage(page); } } } } 

So how would i change this so it finds all pdf files that contain “Cover Sheet” and merge them together?

I’ve been trying to change the above using

 Dim groups As IEnumerable(Of IGrouping(Of String, String)) = files.GroupBy(Function(n) n.Contains("Complaint")) 

But to no avail….

submitted by /u/Pr0x1mo
[link] [comments]

Leave a Reply