Scan in PowerBuilder 2022R2 (WIA C#)

Ramón San Félix Ramón
CODE AUTHOR
Posts: 31
 11 months 4 weeks ago #506 by Ramón San Félix Ramón
Ramón San Félix Ramón created the code: Scan in PowerBuilder 2022R2 (WIA C#)
I would like to share an example to be able to scan images or PDF documents from a PowerBuilder 2022R2 application.
This example is a recreation of the Windows Forms application presented in the following article:

ourcodeworld.com/articles/read/382/creat...winforms-with-csharp

You can download his project at:

github.com/ourcodeworld/csharp-scanner-wia

The main difference with respect to the Windows Forms project is that I have added the possibility of scanning to PDF.
To do this, what I do is scan in PNG and then using the new PowerBuilder objects PDFdocument, PDFpage and PDFImage convert the PNG image into PDF.
To view the scan result, I have used the WebBrowser control, which allows us to load PDF and images directly.
The library that I created in .Net6 and imported with the Net Dll Importer simply has 2 methods:

any of_ListScanners()
string of_Scan(string as_scanner,string as_format,string as_outputpath,string as_filename)
When the application opens, the of_ListScanners() function is executed to fill a ListBox with the available scanners and when the Start scan button is pressed, the of_Scan() function is executed.
If the document extension is a PDF, then I use a global function I created to convert the PNG image to PDF:

string gf_imagetopdf (string as_image)
With the PDFImage Object, the properties of the image are indicated, with the PdfPage Object, the PdfImage object is inserted and finally the PdfPage Object is inserted into the PDfDocument Object that allows us to save the PDF.

The function in summary could be something like this:
String ls_pdf, ls_format
PDFdocument lpdf_doc
PDFpage lpdf_page
PDFImage lpdf_image
Integer li_FormatLen

//We put the same Name as the PDF Image.
li_FormatLen = Len(as_image) - LastPos(as_image, ".") + 1
ls_format = lower(mid(as_image, LastPos(as_image, "."), li_FormatLen))
ls_pdf = replace(as_image, pos(as_image, ls_format), li_FormatLen, ".pdf")

//If the PDF already exists, we delete it.
IF FileExists(ls_pdf) THEN FileDelete(ls_pdf)

//Create Objects
lpdf_page = Create PDFpage
lpdf_image = Create PDFImage
lpdf_doc = Create PDFdocument

//Create the Image in the PdfImage Object
lpdf_image.filename = as_image
lpdf_image.x=0
lpdf_image.y=0
lpdf_image.height=842 //A4 height
lpdf_image.width=595 //A4 Width
lpdf_image.FitMethod = PDFImageFitmethod_Meet!

//We add the Created Image to the PdfPage Object
lpdf_page.addcontent( lpdf_image)

//We import the Page with the Image to the PDFDocument Object in the first position
lpdf_doc.InsertPage(lpdf_page, 1)

//Save the PDF.
lpdf_doc.save(ls_pdf)

//Delete the Scanned Image
FileDelete(as_image)

//Destroy Objects
DESTROY lpdf_page
DESTROY lpdf_image
DESTROY lpdf_doc

//Return the full path of the Created PDF.
RETURN ls_pdf

As always, I leave you the current project link on GitHub:

github.com/rasanfe/pbScanner

And project in Visual Studio 2022:

github.com/rasanfe/ScannerWia

Attached here is the project compiled today in PowerBuilder 2022R2 Build 2828 along with the Visual Studio Project.

I always recommend going to the github links to find the latest version.

To be aware of what I publish you can follow my blog in Spanish:

rsrsystem.blogspot.com/

This message has an attachment file.
Please log in or register to see it.

Please Log in or Create an account to join the conversation.