البرمجة

تحويل الصور إلى PDF وWord باستخدام C#

To achieve this functionality in C#, you can use the following approach:

  1. Convert Image to Text: Use an OCR (Optical Character Recognition) library to extract text from the image. Tesseract is a popular OCR library that you can use in your C# application.

  2. Create PDF and Word Documents: Once you have the text, you can use libraries like iTextSharp for PDF and Microsoft.Office.Interop.Word for Word documents to create the documents and add the extracted text to them.

Here’s a basic example to get you started. Note that you’ll need to install the required NuGet packages for Tesseract, iTextSharp, and Microsoft.Office.Interop.Word.

csharp
using System; using System.Drawing; using Tesseract; using iTextSharp.text; using iTextSharp.text.pdf; using Microsoft.Office.Interop.Word; namespace ImageToPDFAndWord { class Program { static void Main(string[] args) { string imagePath = "path_to_your_image.jpg"; string outputPath = "output_path.pdf"; string outputWordPath = "output_path.docx"; // Convert image to text string text = RecognizeText(imagePath); // Create PDF CreatePdf(outputPath, text); // Create Word document CreateWordDocument(outputWordPath, text); Console.WriteLine("Conversion complete."); } static string RecognizeText(string imagePath) { using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default)) { using (var image = new Bitmap(imagePath)) { using (var page = engine.Process(image)) { return page.GetText(); } } } } static void CreatePdf(string outputPath, string text) { using (var document = new Document()) { using (var writer = PdfWriter.GetInstance(document, new System.IO.FileStream(outputPath, System.IO.FileMode.Create))) { document.Open(); document.Add(new Paragraph(text)); document.Close(); } } } static void CreateWordDocument(string outputPath, string text) { var wordApp = new Application(); var doc = wordApp.Documents.Add(); var range = doc.Range(0, 0); range.Text = text; doc.SaveAs2(outputPath); doc.Close(); wordApp.Quit(); } } }

This is a basic example, and you may need to modify it based on your specific requirements and error handling. Additionally, make sure to handle file paths and resource management appropriately in your application.

المزيد من المعلومات

لتحقيق هذه الوظيفة بشكل كامل ودقيق في تطبيق C# الخاص بك، يمكنك اتباع الخطوات التالية:

  1. تحويل الصورة إلى نص: استخدم مكتبة OCR (التعرف الضوئي على الحروف) مثل Tesseract لاستخراج النص من الصورة.

  2. إنشاء مستند PDF ومستند Word: بعد الحصول على النص، استخدم مكتبات مثل iTextSharp لصياغة ملف PDF واستخدم Microsoft.Office.Interop.Word لإنشاء ملف Word وإضافة النص إليه.

  3. تحميل مكتبات البرمجيات اللازمة: يمكنك تثبيت مكتبة Tesseract OCR ومكتبة iTextSharp ومكتبة Microsoft.Office.Interop.Word من خلال NuGet Package Manager في مشروعك.

  4. كتابة الكود: قم بكتابة الكود الذي يقوم بتحويل الصورة إلى نص باستخدام Tesseract، ثم إنشاء مستند PDF ومستند Word وإضافة النص إليهما.

فيما يلي مثال على كيفية تنفيذ هذه الخطوات:

csharp
using System; using System.Drawing; using Tesseract; using iTextSharp.text; using iTextSharp.text.pdf; using Microsoft.Office.Interop.Word; namespace ImageToPDFAndWord { class Program { static void Main(string[] args) { string imagePath = "path_to_your_image.jpg"; string outputPath = "output_path.pdf"; string outputWordPath = "output_path.docx"; // Convert image to text string text = RecognizeText(imagePath); // Create PDF CreatePdf(outputPath, text); // Create Word document CreateWordDocument(outputWordPath, text); Console.WriteLine("Conversion complete."); } static string RecognizeText(string imagePath) { using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default)) { using (var image = new Bitmap(imagePath)) { using (var page = engine.Process(image)) { return page.GetText(); } } } } static void CreatePdf(string outputPath, string text) { using (var document = new Document()) { using (var writer = PdfWriter.GetInstance(document, new System.IO.FileStream(outputPath, System.IO.FileMode.Create))) { document.Open(); document.Add(new Paragraph(text)); document.Close(); } } } static void CreateWordDocument(string outputPath, string text) { var wordApp = new Application(); var doc = wordApp.Documents.Add(); var range = doc.Range(0, 0); range.Text = text; doc.SaveAs2(outputPath); doc.Close(); wordApp.Quit(); } } }

هذا مثال بسيط للبدء، وقد تحتاج إلى تعديله بناءً على متطلباتك الخاصة ومعالجة الأخطاء بشكل مناسب. كما يجب التعامل بعناية مع مسارات الملفات وإدارة الموارد في تطبيقك.

مقالات ذات صلة

زر الذهاب إلى الأعلى
إغلاق

أنت تستخدم إضافة Adblock

يرجى تعطيل مانع الإعلانات حيث أن موقعنا غير مزعج ولا بأس من عرض الأعلانات لك فهي تعتبر كمصدر دخل لنا و دعم مقدم منك لنا لنستمر في تقديم المحتوى المناسب و المفيد لك فلا تبخل بدعمنا عزيزي الزائر