قم بتثبيت fileformat.words وتحرير ملفات docx برمجيا. تعتبر معالجة مستندات Word مسألة بضعة أسطر من رمز المصدر مع واجهة برمجة التطبيقات المفتوحة المصدر.

نظرة عامة
مرحبًا بك في منشور مدونة آخر في استمرار fileformat.words لـ .NET الاستكشاف. فينا [المقالة] السابقة 3 ، تعلمنا كيفية إنشاء مستند Word في تطبيق .NET باستخدام ملفات fileformat.words مفتوحة المصدر. ومع ذلك ، يتيح لك هذا محرر DOCX مفتوح المصدر إنشاء مستندات Word ويوفر ميزات لتحرير مستندات Word الموجودة برمجيًا. بالإضافة إلى ذلك ، تساعدك مكتبة .NET هذه على إنشاء وحدة مولد مستندات لبرنامج عملك. في منشور المدونة هذا ، سنرى كيفية تحرير مستند Word في C#**عن طريق تثبيت fileformat.words في مشروع تطبيق .NET الخاص بنا. لذلك ، يرجى الاطلاع على منشور المدونة هذا تمامًا لتعلم العملية بأكملها وهي سهلة ومباشرة. سنغطي النقاط التالية في هذه المقالة:
- محرر DOCX مفتوح المصدر-تثبيت API
- كيفية تحرير ملف docx باستخدام fileformat.words
- كيفية تغيير الخط في مستند Word - الميزات المتقدمة
محرر DOCX مفتوح المصدر-تثبيت API
عملية تثبيت محرر DOCX المفتوح المصدر**بسيطة للغاية حيث توجد طريقتان للحصول على مكتبة .NET في مشروع التطبيق الخاص بك. ومع ذلك ، يمكنك تنزيل Nuget Package أو فقط تشغيل الأمر التالي في Nuget Package Manager.
Install-Package FileFormat.Words
لمزيد من المعلومات حول التثبيت ، يرجى زيارة هذا الرابط.
كيفية تحرير ملف docx باستخدام fileformat.words
يوضح هذا القسم كيفية تحرير ملف DOCX في C# باستخدام مكتبة .NET مفتوحة المصدر هذه. يرجى اتباع الخطوات التالية ومقتطف الكود لتحقيق الوظيفة:
- تهيئة مثيل للفئة الوثيقة وتحميل مستند كلمة موجود.
- مثبتة على مُنشئ الفئة 10 مع كائن فئة المستند.
- قم بإنشاء كائن من فئة الفقرة.
- إنشاء مثيل لفئة Run التي تمثل مجموعة من الأحرف في مستند Word.
- الوصول إلى خاصية النص لفئة التشغيل لتعيين النص.
- استدعاء طريقة AppendChild لإرفاق كائن فئة التشغيل مع كائن فئة الفقرة.
- استدعاء طريقة AppendChild لفئة الجسم لإضافة فقرة إلى المستند.
- ستوفر طريقة حفظ مستند Word على القرص.
using FileFormat.Words;
namespace Example
{
class Program
{
static void Main(string[] args)
{
// Initialize an instance of the Document class and load an existing Word document.
using (Document doc = new Document("/Docs.docx"))
{
//Instantiate the constructor of the Body class with the Document class object.
Body body = new Body(doc);
// Create an object of the Paragraph class.
Paragraph para = new Paragraph();
// Instantiate an instance of the Run class that represents a run of characters in a Word document.
Run run = new Run();
// Access the Text property of the Run class to set the text.
run.Text = "This is a sample text.";
// Call the AppendChild() method to attach the object of the Run class with the object of the Paragraph class.
para.AppendChild(run);
// Invoke AppendChild method of the body class to add paragraph to the document.
body.AppendChild(para);
// The Save method will save the Word document onto the disk.
doc.Save("/Docs.docx");
}
}
}
}
يظهر إخراج مقتطف الرمز أعلاه في الصورة أدناه:

كيفية تغيير الخط في مستند Word - الميزات المتقدمة
يوفر FileFormat.words أيضًا بعض الخيارات المتقدمة لتعديل مستندات Word. دعونا نرى كيف يمكننا تحرير ملفات docx بشكل أكبر. يمكنك اتباع الخطوات التالية ومقتطف الرمز:
- قم بتعيين الخاصية Bold إلى True لجعل النص جريئًا.
- اجعل النص مائلًا عن طريق تعيين قيمة الخاصية المائل.
- اضبط قيمة الخاصية fontfamily لتعيين عائلة الخط للنص.
- الوصول إلى خاصية fontsize لتعيين حجم الخط.
- قم بتعيين الخاصية تسطير لتسديد النص.
- الخاصية COLOR ستقوم بتعيين لون النص.
using FileFormat.Words;
namespace Example
{
class Program
{
static void Main(string[] args)
{
// Initialize an instance of the Document class and load an existing Word document.
using (Document doc = new Document("/Users/Mustafa/Desktop/Docs.docx"))
{
//Instantiate the constructor of the Body class with the Document class object.
Body body = new Body(doc);
// Create an object of the Paragraph class.
Paragraph para = new Paragraph();
// Instantiate an instance of the Run class that represents a run of characters in a Word document.
Run run = new Run();
// Access the Text property of the Run class to set the text.
run.Text = "This is a sample text.";
// Set the Bold property to true.
run.Bold = true;
// Make the Text Italic.
run.Italic = true;
// Set the value of FontFamily of the Text.
run.FontFamily = "Algerian";
// Access the FontSize property to set the font size.
run.FontSize = 40;
// Set the Underline property to true to underline the text.
run.Underline = true;
// The Color property will set the color of the text.
run.Color = "FF0000";
// Call the AppendChild() method to attach the object of the Run class with the object of the Paragraph class.
para.AppendChild(run);
// Invoke AppendChild method of the body class to add paragraph to the document.
body.AppendChild(para);
// The Save method will save the Word document onto the disk.
doc.Save("/Docs.docx");
}
}
}
}
يجب أن يبدو الملف الرئيسي مثل مقتطف الرمز أعلاه. يرجى تشغيل المشروع وسترى الإخراج التالي:

الخاتمة
في منشور المدونة هذا ، تعلمنا كيفية تحرير مستند Word في C# باستخدام مكتبة .NET مفتوحة المصدر. FileFormat.words for .NET هي واجهة برمجة تطبيقات سهلة الاستخدام توفر ميزات لإنشاء مستندات Word ومعالجتها برمجيًا. بالإضافة إلى ذلك ، مررنا أيضًا كيفية تغيير الخط في مستند Word مع خصائص أخرى. أخيرًا ، هناك وثائق متاح فيما يتعلق بتطوير واستخدام محرر DOCX المفتوح المصدر.** أخيرًا ، يواصل fileformat.com كتابة منشورات المدونة حول مواضيع أخرى. لذا ، يرجى البقاء على اتصال للحصول على التحديثات. علاوة على ذلك ، يمكنك متابعتنا على منصات الوسائط الاجتماعية الخاصة بنا ، بما في ذلك Facebook و LinkedIn و Twitter.
المساهمة
نظرًا لأن fileformat.words for .NET هو مشروع مفتوح المصدر وهو متاح على github. لذلك ، فإن مساهمة المجتمع موضع تقدير كبير.
اطرح سؤالاً
يمكنك إخبارنا بأسئلتك أو استفساراتك في المنتدى.
الأسئلة الشائعة
** كيف تكتب في مستند Word باستخدام C#؟** يمكنك تثبيت fileformat.words لـ .NET في مشروع .NET الخاص بك لتحرير ملفات DOCX برمجيًا. ** كيف أقوم بتحرير مستند كلمة تمامًا؟** يرجى متابعة هذا الرابط لمعرفة كيفية تحرير مستندات الكلمات باستخدام مكتبة C#.