19-02-16, 04:54 AM
(10-02-16, 11:04 AM)عدنان الشمري كتب : تحية طيبة . . .السلام عليكم ورحمة الله وبركاته
ارجو مساعدتي في برنامج سكنر
احتاج الى برنامج يقوم بسحب الصور من الاسكنر وحفظها كملف PDF
يمكنك الإعتماد على مكتبة WIA المقدمة ضمن حزمة نظام التشغيل Windows للتكامل مع وحدات الماسح الضوئي Scanners أما بخصوص استخراج الملف على شكل صيغة PDF ففي تلك الحالة يمكنك الإعتماد على مكتبة ITextSharp وهي مكتبة مفتوحة المصدر تُمكنك من تحقيق أحلامك فيما يخص التعامل مع ملفات PDF بكل جدارة.
فالتالي فئة تُمكنك من التكامل و سحب الصور من أجهزة الماسح الضوئي:
PHP كود :
public class Scanner
{
string DeviceID;
const string wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}";
const string wiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}";
const string wiaFormatGIF = "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}";
const string wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
const string wiaFormatTIFF = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}";
class WIA_DPS_DOCUMENT_HANDLING_SELECT
{
public const uint FEEDER = 0x00000001;
public const uint FLATBED = 0x00000002;
}
class WIA_DPS_DOCUMENT_HANDLING_STATUS
{
public const uint FEED_READY = 0x00000001;
}
class WIA_PROPERTIES
{
public const uint WIA_RESERVED_FOR_NEW_PROPS = 1024;
public const uint WIA_DIP_FIRST = 2;
public const uint WIA_DPA_FIRST = WIA_DIP_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
public const uint WIA_DPC_FIRST = WIA_DPA_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
//
// Scanner only device properties (DPS)
//
public const uint WIA_DPS_FIRST = WIA_DPC_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
public const uint WIA_DPS_DOCUMENT_HANDLING_STATUS = WIA_DPS_FIRST + 13;
public const uint WIA_DPS_DOCUMENT_HANDLING_SELECT = WIA_DPS_FIRST + 14;
}
class WIA_ERRORS
{
public const uint BASE_VAL_WIA_ERROR = 0x80210000;
public const uint WIA_ERROR_PAPER_EMPTY = BASE_VAL_WIA_ERROR + 3;
}
public void ADFScan()
{
if (!Directory.Exists(Application.StartupPath + @"\\Workarea\\"))
{
Directory.CreateDirectory(Application.StartupPath + @"\\Workarea\\");
}
else
{
string[] files = Directory.GetFiles(Application.StartupPath + @"\\Workarea\\");
for (int i = 0; i < files.Length; i++)
{
File.Delete(files[i]);
}
}
//Choose Scanner
CommonDialogClass class1 = new CommonDialogClass();
Device d = class1.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, true, false);
if (d != null)
{
this.DeviceID = d.DeviceID;
}
else
{
//no scanner chosen
return;
}
WIA.CommonDialog WiaCommonDialog = new CommonDialogClass();
bool hasMorePages = true;
int x = 0;
int numPages = 0;
while (hasMorePages)
{
//Create DeviceManager
DeviceManager manager = new DeviceManagerClass();
Device WiaDev = null;
foreach (DeviceInfo info in manager.DeviceInfos)
{
if (info.DeviceID == this.DeviceID)
{
WIA.Properties infoprop = null;
infoprop = info.Properties;
//connect to scanner
WiaDev = info.Connect();
break;
}
}
//Start Scan
WIA.ImageFile img = null;
WIA.Item Item = WiaDev.Items[1] as WIA.Item;
try
{
img = (WIA.ImageFile)WiaCommonDialog.ShowTransfer(Item, wiaFormatJPEG, false);
//process image:
//one would do image processing here
//
//Save to file
string varImageFileName = @"C:\Image" + x.ToString() + ".jpg";
img.SaveFile(varImageFileName);
}
catch (Exception ex)
{
//MessageBox.Show("Error: " + ex.Message);
}
finally
{
Item = null;
//determine if there are any more pages waiting
Property documentHandlingSelect = null;
Property documentHandlingStatus = null;
foreach (Property prop in WiaDev.Properties)
{
if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
documentHandlingSelect = prop;
if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
documentHandlingStatus = prop;
}
hasMorePages = false; //assume there are no more pages
if (documentHandlingSelect != null)
//may not exist on flatbed scanner but required for feeder
{
//check for document feeder
if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) != 0)
{
hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0);
}
}
x++;
}
}
}
}
بواسطة إستعداء الإجراء ADFScan سوف تحصل على صورة او مجموعة صورة -بناءا على عدد صفحات الملف المُمرر إلى جهاز الماسح الصوئي- في المسار C:\ImageX.jpg.
وفالتالي كود يوضح كيفية إنشاء ملف PDF بإستخدام itextsharp وإضافة الصورة المراد تمريرها إلى الملف:
PHP كود :
private void CreatePDF()
{
string work_file = @"C:\mypdf.pdf";
iTextSharp.text.Document PdfDoc = new iTextSharp.text.Document();
iTextSharp.text.pdf.PdfWriter PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(PdfDoc, new FileStream(work_file, FileMode.Create));
PdfDoc.Open();
// start the loop of the images already scanned
iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(Image.FromFile(MY_IMAGE_FILENAME_HERE), System.Drawing.Imaging.ImageFormat.Jpeg);
if (pic.Height > pic.Width)
{
//Maximum height is 800 pixels.
float percentage = 0.0f;
percentage = 700 / pic.Height;
pic.ScalePercent(percentage * 100);
}
else
{
//Maximum width is 600 pixels.
float percentage = 0.0f;
percentage = 540 / pic.Width;
pic.ScalePercent(percentage * 100);
}
pic.Border = iTextSharp.text.Rectangle.BOX;
pic.BorderColor = iTextSharp.text.Color.BLACK;
pic.BorderWidth = 3f;
PdfDoc.Add(pic);
PdfDoc.NewPage();
//end of the scanned images loop
PdfDoc.Close();
PdfWriter.Close();
}
فقط إبداء بحلقة تكرارية تقوم بتحميل الصورة المتواجدة داخل مجلد سحب الصور -المسار -C:\ImageX.jpg- على سبيل المثال المُحدد داخل الفئة الخاصة بسحب الصور وعوض عن المتغير MY_IMAGE_FILENAME_HERE المتواجد بالكود السابق بمسار الصورة الحالية ضمن الحلقة وسوف يتم إنشاء ملف PDF بإسم mypdf.pdf داخل المسار C:\mypdf.pdf فور الإنتهاء من الإجراء CreatePDF.
المكتبتين بالمرفقات.
