使用 iTextSharp 合并 vb.net 后删除 pdf 文件

delete pdf file after merging vb.net using iTextSharp

我正在将两个 pdf 合并为一个我正在使用我在此处找到的函数

Private Sub MergePdfFiles(ByVal pdfFiles() As String, ByVal outputPath As String)
        Dim pdfCount As Integer = 0
        Dim f As Integer = 0
        Dim fileName As String
        Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
        Dim pageCount As Integer = 0
        Dim pdfDoc As iTextSharp.text.Document = Nothing
        Dim writer As PdfWriter = Nothing
        Dim cb As PdfContentByte = Nothing

        Dim page As PdfImportedPage = Nothing
        Dim rotation As Integer = 0

        Try
            pdfCount = pdfFiles.Length
            If pdfCount > 1 Then
                fileName = pdfFiles(f)
                reader = New iTextSharp.text.pdf.PdfReader(fileName)
                pageCount = reader.NumberOfPages
                pdfDoc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1), 18, 18, 18, 18)
                writer = PdfWriter.GetInstance(pdfDoc, New FileStream(outputPath, FileMode.OpenOrCreate))
                With pdfDoc
                    .Open()
                End With
                cb = writer.DirectContent
                While f < pdfCount
                    Dim i As Integer = 0
                    While i < pageCount
                        i += 1
                        pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(i))
                        pdfDoc.NewPage()
                        page = writer.GetImportedPage(reader, i)
                        rotation = reader.GetPageRotation(i)
                        If rotation = 90 Then
                            cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height)
                        ElseIf rotation = 270 Then
                            cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(i).Width + 60, -30)
                        Else
                            cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0)
                        End If
                    End While
                    f += 1
                    If f < pdfCount Then
                        fileName = pdfFiles(f)
                        reader = New iTextSharp.text.pdf.PdfReader(fileName)
                        pageCount = reader.NumberOfPages
                    End If
                End While
                pdfDoc.Close()
                reader.Close()
            End If
        Catch ex As Exception
            'err
        End Try
    End Sub

现在我需要删除旧文件 "pdfFiles()" 但我收到错误 "The process cannot access the file 'C:.......pdf' because it is being used by another process." 这只发生在第一个文件上,我对另一个文件没问题

谢谢

我知道这是在 C# 中,但这是我用来合并文件的方法。

var document = new Document();
var outFile = Path.Combine(finishedFilePath, fileName + ".pdf");
var writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create));
try
{
    document.Open();
    foreach (var fileName in filesList)
    {
         var reader = new PdfReader(Path.Combine(StartPath, fileName));

         for (var i = 1; i <= reader.NumberOfPages; i++)
         {
             var page = writer.GetImportedPage(reader, i);
             writer.AddPage(page);
         }

         reader.Close();
    }

    writer.Close();
    document.Close();
}
catch (Exception ex)
{
    //catch error             
}
finally
{
    writer.Close();
    document.Close();
}

希望这对您有所帮助。

标记正确的代码在 C# 中,这里是 vb.net 版本:

Public Sub MergePDFFiles(ByVal outPutPDF As String)

Dim StartPath As String = FileArray(0) ' this is a List Array declared Globally
Dim document = New Document()
Dim outFile = Path.Combine(outPutPDF)' The outPutPDF varable is passed from another sub this is the output path
Dim writer = New PdfCopy(document, New FileStream(outFile, FileMode.Create))

Try

    document.Open()
    For Each fileName As String In FileArray

        Dim reader = New PdfReader(Path.Combine(StartPath, fileName))

        For i As Integer = 1 To reader.NumberOfPages

            Dim page = writer.GetImportedPage(reader, i)
            writer.AddPage(page)

        Next i

        reader.Close()

    Next

    writer.Close()
    document.Close()

Catch ex As Exception
    'catch a Exception if needed

Finally

    writer.Close()
    document.Close()

End Try

结束子