1

我有一个程序可以抓取 PDF 文件并将它们合并,然后将原始未合并的文件删除到回收站。

我一直在重新设计程序以利用 Syncfusion PDF 工具,并且大部分时间都很顺利,直到我尝试删除文件。

警告弹出窗口告诉我是我的程序打开了文件,但我不确定它在程序中的哪个位置仍然打开或如何找出。

这是功能:

Function MergePDFSync(ByVal Path As String, ByVal SavePath As String, ByVal outFileName As String, ByVal DeleteOriginal As Boolean) As String
    On Error GoTo sError

    Dim CreateDate As Date = Now
    Dim finalFileName As String = ""
    Dim dInfo As New DirectoryInfo(Path)

    If dInfo.GetFiles("*.pdf").Length > 0 Then
        Dim doc As New Syncfusion.Pdf.PdfDocument
        Dim ldoc As Syncfusion.Pdf.Parsing.PdfLoadedDocument
        Dim file As String

        For Each f As FileInfo In dInfo.GetFiles("*.pdf")
            ldoc = New Syncfusion.Pdf.Parsing.PdfLoadedDocument(f.OpenRead)
            Syncfusion.Pdf.PdfDocument.Merge(doc, ldoc)
            ldoc.Close()
            doc.DisposeOnClose(ldoc)
        Next
        ldoc = Nothing

        finalFileName = Format(CreateDate, "M-d-yy-HHmmss-") & outFileName

        doc.Save(Path & "\" & finalFileName)
        doc.Close()
        doc = Nothing

        dInfo = Nothing

        If DeleteOriginal Then ' delete origional files
            dInfo = New DirectoryInfo(Path)
            For Each f As FileInfo In dInfo.GetFiles("*.pdf") 'For i As Integer = 0 To strFiles.Length - 1 ' run through all the files in the directory
                Console.WriteLine("MergePDF2 10.1 : " & f.Name & " = " & finalFileName)
                If Not f.Name = finalFileName Then
                    Console.WriteLine("MergePDF2 10.2 : Delete " & f.FullName)
                    My.Computer.FileSystem.DeleteFile(f.FullName, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin)
                End If
            Next
        End If
    End If

    Return finalFileName
    Exit Function
sError:
    ReportError("MergePDF2 " & Path & " " & Err.Description)
    Console.WriteLine("MergePDF2 " & Path & " " & ErrorToString())
End Function
4

1 回答 1

1

我知道他们的合并功能有问题,我不确定它是否已修复或您使用的是哪个版本。您可能想尝试替代合并。

Dim docTarget As PdfLoadedDocument = New PdfLoadedDocument(targetFile)
Dim docAppend As PdfLoadedDocument = New PdfLoadedDocument(appendFile)

docTarget.Append(docAppend)
docTarget.Save()
docTarget.Close(True)
docAppend.Close(True)

http://www.syncfusion.com/support/forums/pdf-windows/79124

或一页一页地添加

doc.Pages.Add(ldoc.Pages[i]);

http://www.syncfusion.com/support/kb/876/How%20to%20merge%20two%20PDF%20documents

于 2013-08-14T20:54:41.260 回答