مكتبة .NET لإنشاء مستندات معالجة الكلمات

قراءة ملفات Word وكتابتها ومعالجتها وتحويلها وإنشاء جداول في DOCX عبر Open Source .NET API.

Open XML SDK هي واجهة برمجة تطبيقات مفتوحة المصدر ، تم تطويرها بواسطة Microsoft وتوزيعها بموجب كود سلوك Microsoft مفتوح المصدر للتعامل مع مستندات معالجة الكلمات بما في ذلك DOCX و DOTX و DOCM ، تنسيقات ملفات DOTM

باستخدام واجهة برمجة التطبيقات ، يمكنك إضافة نص ورأس وتذييل وتعليقات ختامية وحواشي سفلية وأنماط وسمات والمزيد. يسمح لك بإنشاء مستندات Word عالية الأداء واستخراج البيانات منها. تدعم واجهة برمجة التطبيقات العديد من منصات .NET بما في ذلك .NET 3.5 و .NET 4.0 و .NET 4.6 و .NET Standard 1.3.

Previous Next

الشروع في العمل مع Open XML SDK

بادئ ذي بدء ، يجب أن يكون لديك .NET Framework 3.5 أو أعلى. بعد ذلك ، يرجى تنزيل المستودع يدويًا من GitHub أو تثبيته من NuGet .

التثبيت افتح XML SDK من NuGet

 Install-Package DocumentFormat.OpenXml

معالجة ملف DOCX باستخدام C #

تسمح Open XML SDK لمبرمجي .NET بإنشاء وتعديل معالجة الكلمات من تطبيقات .NET الخاصة بهم. لتعديل ملف موجود ، يمكنك فتح ملف موجود وإلحاق التغييرات مثل النص والفقرات والجداول والمزيد.

إضافة فقرة في DOCX - C #

// Open an existing word processing document
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open("fileformat.docx", true))
{
  Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
  // Add paragraph
  Paragraph para = body.AppendChild(new Paragraph());
  Run run = para.AppendChild(new Run());
  run.AppendChild(new Text("File Format Developer Guide"));
}

قم بإنشاء جدول في DOCX باستخدام C #

تسمح واجهة برمجة التطبيقات للمطورين بإضافة جدول في مستندات معالجة الكلمات. يمكنك إضافة جدول ، وتعيين خصائص الجدول ، وتعيين شبكة الجدول ، وخصائص شبكة العمود. علاوة على ذلك ، يمكنك إدارة خلايا الجدول والصفوف باستخدام فئات TableCell و TableRow على التوالي.

إنشاء جدول في DOCX - C #

// Open an existing word processing document
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open("fileformat.docx", true))
{
  Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
  // Create a table.
  Table table = new Table();
  // Set the style and width for the table.
  TableProperties tableProperties = new TableProperties();
  TableStyle tableStyle = new TableStyle() { Val = "TableGrid" };
  // Make the table width 100% of the page width.
  TableWidth tableWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct };
  // Apply
  tableProperties.Append(tableStyle, tableWidth);
  table.AppendChild(tableProperties);
  // Add columns
  TableGrid tableGrid = new TableGrid(new GridColumn(), new GridColumn(), new GridColumn());
  table.AppendChild(tableGrid);
  // Create 1 row to the table.
  TableRow tableRow = new TableRow();
  // Add a cell to each column in the row.
  TableCell tableCell = new TableCell(new Paragraph(new Run(new Text("Column 1"))));
  TableCell tableCell1 = new TableCell(new Paragraph(new Run(new Text("Column 2"))));
  //Append data
  tableRow.Append(tableCell, tableCell1);
  // Add row to the table.
  table.AppendChild(tableRow);
  // Add the table to the document
  body.AppendChild(table);
}

الرؤوس والتذييلات في مستند معالجة الكلمات

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

إدارة الرؤوس في مستند Word

 
public static void ApplyHeader(WordprocessingDocument doc)
{
  // Get the main document part.
  MainDocumentPart mainDocPart = doc.MainDocumentPart;
  HeaderPart headerPart1 = mainDocPart.AddNewPart("r97");
  Header header1 = new Header();
  Paragraph paragraph1 = new Paragraph(){ };
  Run run1 = new Run();
  Text text1 = new Text();
  text1.Text = "Header stuff";
  run1.Append(text1);
  paragraph1.Append(run1);
  header1.Append(paragraph1);
  headerPart1.Header = header1;
  SectionProperties sectionProperties1 = mainDocPart.Document.Body.Descendants().FirstOrDefault();
  if (sectionProperties1 == null)
  {
  sectionProperties1 = new SectionProperties() { };
  mainDocPart.Document.Body.Append(sectionProperties1);
  }
  HeaderReference headerReference1 = new HeaderReference() { Type = HeaderFooterValues.Default, Id = "r97" };
  sectionProperties1.InsertAt(headerReference1,0);
}

إدارة التذييلات في مستند Word

 
public static void ApplyFooter(WordprocessingDocument doc)
{
  // Get the main document part.
  MainDocumentPart mainDocPart = doc.MainDocumentPart;
  FooterPart footerPart1 = mainDocPart.AddNewPart("r98");
  Footer footer1 = new Footer();
  Paragraph paragraph1 = new Paragraph() { };
  Run run1 = new Run();
  Text text1 = new Text();
  text1.Text = "Footer stuff";
  run1.Append(text1);
  paragraph1.Append(run1);
  footer1.Append(paragraph1);
  footerPart1.Footer = footer1;
  SectionProperties sectionProperties1 = mainDocPart.Document.Body.Descendants().FirstOrDefault();
  if (sectionProperties1 == null)
  {
    sectionProperties1 = new SectionProperties() { };
    mainDocPart.Document.Body.Append(sectionProperties1);
  }
  FooterReference footerReference1 = new FooterReference() { Type = DocumentFormat.OpenXml.Wordprocessing.HeaderFooterValues.Default, Id = "r98" };
  sectionProperties1.InsertAt(footerReference1, 0);
}
 عربي