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



我正在将两个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()",但我收到错误"进程无法访问文件'C:.......pdf',因为它正在被另一个进程使用。 这只发生在第一个文件上,我对另一个文件没有问题

OP 这里与函数

谢谢

我知道

这是在 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

结束子

相关内容

  • 没有找到相关文章

最新更新