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

时间:2016-05-27 15:23:03

标签: vb.net pdf itextsharp filestream

我正在使用我在此处找到的功能将两个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

现在我需要删除旧文件&#34; pdfFiles()&#34;但是我收到了错误&#34;该过程无法访问该文件&#C; ....... pdf&#39;因为它正被另一个进程使用。&#34;这只发生在第一个文件中,我对另一个文件没有问题

OP Here with the function

由于

2 个答案:

答案 0 :(得分:1)

我知道这是在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();
}

希望这在某种程度上有所帮助。

答案 1 :(得分:0)

标记正确的代码在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

End Sub