VB.net将PDF合并到一个共享类似文件名的目录中

问题描述 投票:3回答:1

我目前有一个目录,我将具有多个标题/条形码的PDF分成以下类别:

文件#Header Sheet#所以它看起来像这样:

ZTEST01封面1

ZTEST01封面2

ZTEST01投诉3

ZTEST01投诉4

ZTEST01图表5

ZTEST01图表6

ZTEST01召唤8

ZTEST01召唤9

我的目标是让代码遍历此目录并将所有具有相同标题名称的文件合并在一起:

ZTEST01封面纸1 + ZTEST01封面纸2 = ZTEST01封面纸

这是我的下面的代码(我和PDFsharp和Bytescout来回走动,因此我现在只剩下进口,直到我找出最有效的方法):

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 & "\Merge\" & Path & "Complaint" & ".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

我现在尝试使用像“投诉”这样的文件名来查看它是否有效,但到目前为止它只是显示一个空白的cmd提示符。

我想这样做

“封面”

“投诉”

“展示”

和“召唤”

任何建议将不胜感激。

vb.net pdf merge directory enumeration
1个回答
1
投票

解:

    Imports System.IO
    Imports System.IO.Path
    Imports PdfSharp.Pdf
    Imports PdfSharp.Pdf.IO

    Module Module1
        Private inputdir As String = "G:\Word\Department Folders\Pre-Suit\Drafts-IL\2-IL_AttyReview\2018-09\Reviewed\unmerged\

"


    Public Sub Main()

        MergeFiles("Cover Sheet", inputdir)
        MergeFiles("Complaint", inputdir)
        MergeFiles("Exhibit", inputdir)
        MergeFiles("Military", inputdir)
        MergeFiles("Summons", inputdir)
    End Sub

    Public Sub MergeFiles(ByVal name As String, inputdir As String)
        Dim OutputFile As String
        Dim OutputDir As String = inputdir & "\Merge\"
        Dim OutputDocument As PdfDocument

        If Not Directory.Exists(OutputDir) Then Directory.CreateDirectory(OutputDir)

        For Each files As String In Directory.GetFiles(inputdir, "*" & name & "*.pdf")
            OutputFile = GetFileNameWithoutExtension(files).Substring(0, 7) & " " & name & ".pdf"

            If File.Exists(OutputDir & OutputFile) Then
                OutputDocument = PdfReader.Open(OutputDir & OutputFile)
            Else
                OutputDocument = New PdfDocument()
            End If
            Console.WriteLine("Merging: {0}...", GetFileName(files))
            Using InputDocument As PdfDocument = PdfReader.Open(files, PdfDocumentOpenMode.Import)
                For Each page As PdfPage In InputDocument.Pages
                    OutputDocument.AddPage(page)
                Next
            End Using

            OutputDocument.Save(OutputDir & OutputFile)
            OutputDocument.Dispose()
        Next

    End Sub
End Module
© www.soinside.com 2019 - 2024. All rights reserved.