Next code should split a multipage pdf file into one file each page. It works for first page, but when closing destination pdfDocument for second page it throws iText.Kernel.PdfException: 'Document has no pages.' Tried with different source files with same result. Tried also to first get the pagecount and open the reader for each page with same result
Shared Function splitFileIntoPages(sourceFilename As String) As Boolean
If System.IO.File.Exists(sourceFilename) Then
Using reader As New PdfReader(sourceFilename)
Using pdfSource As New PdfDocument(reader)
For iPage = 1 To pdfSource.GetNumberOfPages
Dim destFilename = sourceFilename.Replace(".pdf", String.Format(".Page {0}.pdf", iPage))
Using writer As New PdfWriter(destFilename)
Using pdfDest As New PdfDocument(writer)
pdfSource.CopyPagesTo(iPage, 1, pdfDest)
End Using
End Using
Next
End Using
End Using
End If
Return True
End Function