vb.net - VB.net在共享相似文件名的目录中合并PDF

标签 vb.net pdf merge directory enumeration

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

File#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提示符。

我想这样做

“封面”

“投诉”

“展示”

和“召唤”

任何建议将不胜感激。

最佳答案

解:

    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

关于vb.net - VB.net在共享相似文件名的目录中合并PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52169914/

相关文章:

excel - 在事件工作簿目录中从 PDF 转换为 Word

python - .doc 到 pdf 使用 python

在市集合并两个结帐

vb.net - 在声明中初始化类成员实例?

asp.net - System.IO.FileNotFoundException : Could not load file or assembly 'file:///C:\Windows\Microsoft. NET\Framework\v4.0.30319\-i

asp.net - 在asp.net中获取用户ID

vb.net - "Return Not (s Is Nothing)"返回什么

jquery - 使用 jQuery $.Get 打开/保存 PDF

php - 需要在一个 JSON 中合并多行

python - 基于Python中列的部分匹配匹配和合并2个数据帧