I have a vb.net project that allow the user to click on a button to perform scan. On clicking of the button, it will display a commonDialog box that let user select a connected scanner device, thereby I will programmatically set the properties for the scan (ie: A4 size scan, greyscale etc).
Using a while loop, I allow the user to scan more than a single document. My code goes as below:
Private Sub ScanAndSaveMultipleDocuments()
Dim pdfFilePath As String = "examplePath"
Dim pdfDocument As New iText.Kernel.Pdf.PdfDocument(New PdfWriter(pdfFilePath))
Dim document As Document
Dim isMorePage As Boolean = True
Try
If Not Directory.Exists(tempPath) Then
Directory.CreateDirectory(tempPath)
End If
Dim documentIndex As Integer = 0
While isMorePage = True
SetScannerDevice()
Dim scannedImage As ImageFile = ScanImage()
If scannedImage IsNot Nothing Then
Dim scannedImageBytes As Byte() = RevieweScannedImage(scannedImage)
If scannedImageBytes IsNot Nothing Then
Dim pdfImage As New Image(iText.IO.Image.ImageDataFactory.Create(scannedImageBytes))
document = New Document(pdfDocument, New iText.Kernel.Geom.PageSize(pdfImage.GetImageWidth(), pdfImage.GetImageHeight()))
pdfImage.SetFixedPosition(documentIndex + 1, 0, 0)
document.Add(pdfImage)
documentIndex += 1
End If
If MsgBoxResult.Yes <> MsgBox("Are there more document to scan?", MsgBoxStyle.YesNo) Then isMorePage = False
Else
pdfDocument.Close()
Exit While
End If
End While
If pdfDocument IsNot Nothing AndAlso Not pdfDocument.IsClosed Then pdfDocument.Close()
If documentIndex = 0 Then Return
Dim doc As REF_InvoiceDocument = SaveNewInvoiceDocument(pdfFilePath)
If doc IsNot Nothing Then selectedInvoice.DocumentID = doc.ID : selectedInvoice.Save()
Dim newPdfDocument As PdfiumViewer.PdfDocument = PdfiumViewer.PdfDocument.Load(pdfFilePath)
If newPdfDocument IsNot Nothing Then
PdfViewer.Document = newPdfDocument
PdfViewer.BringToFront()
isViewPDF = True
End If
Catch ex As Exception
MsgBox(ex.ToString)
Finally
If device IsNot Nothing Then device = Nothing
If pdfDocument IsNot Nothing AndAlso Not pdfDocument.IsClosed Then
pdfDocument.Close()
End If
End Try
End Sub
This code allow user to scan multiple documents, review the scans and save the scanned documents into a pdf document. However, user have to select a scanner device from the commonDialog for each while loop, which is not really efficient.
Here is the code for my SetScannerDevice()
Private Sub SetScannerDevice(Optional runAgain As Boolean = False)
device = dialog.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, False, False)
Try
If device IsNot Nothing Then
For Each item As [Property] In device.Items(1).Properties
'Console.WriteLine("Property {0} : {1} = {2}", item.PropertyID, item.Name, item.Value)
Select Case item.PropertyID
Case 3088 'Document Handling Select
SetProperty(item, 1)
Case 6146 'Intent
SetProperty(item, 2)
Case 3098 'Page Width
SetProperty(item, 8268)
Case 3099 'Page Height
SetProperty(item, 11693)
End Select
Next
End If
Catch ex As Exception
End Try
End Sub
In my attempt to optimize the user experience, I have tried putting the SetScannerDevice outside the while loop, and adjusted the code to only call for dialog.ShowSelectDevice if device = nothing (device not been set). This is to ensure that for each ScanImage called within the while loop, it will scan using the same device and the user can avoid having to select a scanner device for every single scan.
This code works for the first while loop, but on the second loop it gave me the error
WIA_ERROR_PAPER_EMPTY (0x80210003) in image = CType(device.Items(1).Transfer(FormatID.wiaFormatJPEG), ImageFile)
even though
- my device is still intact
- my device.item(1) is still intact
- my scanner feeder is not empty
Anyone has any idea how I can go about fixing this? I am using Brother DS-940DW.