0

I'm merging two pdf into one i'm using a function that I found here

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

Now I need to delete the old files "pdfFiles()" but I'm getting the error "The process cannot access the file 'C:.......pdf' because it is being used by another process." this is only happening with the first file, I have no problem with the other one

OP Here with the function

Thanks

Community
  • 1
  • 1
Carlos Anez
  • 61
  • 1
  • 11
  • A quick glance shows that at least `writer` isn't being close or disposed of, I'd start there. – Chris Haas May 27 '16 at 18:22
  • I'm also not really clear why there's some work done outside of the loop and some inside. This would be a really good place for a `for each` instead IMHO. – Chris Haas May 27 '16 at 18:31
  • I Close the writer and still not luck, I think I'll find another way to do it, and I just found this Function, Copy, Paste then I tested and worked fine, I'm sure there should be a better way to do it – Carlos Anez May 27 '16 at 18:55
  • @Chris *`writer` isn't being close or disposed of* - `writer` is implicitly closed when `pdfDoc` is closed. – mkl May 28 '16 at 09:11

2 Answers2

1

I know this is in C# but this is what i use to merge files together.

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();
}

Hope this helps in some way.

Eric
  • 122
  • 8
0

The code that was marked correct is in C# here is the vb.net version:

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