تم تصميم هذا البرنامج التعليمي السريع لشرح ** كيفية إزالة الصفحة الفارغة في Word باستخدام C# ** مثل DOCX ، DOC ، إلخ. ويوفر تفاصيل لتهيئة البيئة بالإضافة إلى الخطوات التي يجب تنفيذها لـ المهمة ونموذج التعليمات البرمجية التي تنفذ المنطق الكامل ** لمسح الصفحة الفارغة في Word باستخدام C# **. يمكنك تشغيل هذا الرمز دون تثبيت أي برنامج تابع لجهة خارجية باستخدام أي من أنظمة التشغيل مثل Windows أو Linux أو macOS.
خطوات حذف الصفحة الفارغة في Word باستخدام C#
- تكوين المشروع لإضافة مكتبة Aspose.Words إلى المشروع
- قم بتحميل ملف Word المصدر في كائن فئة Document لإزالة الصفحات الفارغة
- قم بتحليل جميع الصفحات في المستند المصدر وجلب كل صفحة على حدة
- قم بإنشاء مستند منفصل مع كل صفحة مفردة فقط من المستند المصدر
- قم بتحليل مستند الصفحة الواحدة هذا وتحقق مما إذا كان أي section يحتوي على بعض عقد النص / الشكل أم لا
- احتفظ بقائمة بجميع الصفحات غير الفارغة في المستند الأصلي
- إلحاق كافة الصفحات غير الفارغة بملف Word الجديد
- احفظ هذا الملف الذي يحتوي على الصفحات مع بعض النص أو الشكل فقط
توفر هذه الخطوات تفاصيل * حذف الصفحة الفارغة في Word باستخدام C# *. أولاً ، تقوم بتحميل ملف Word المصدر ثم تحليل كل صفحة من الصفحات الموجودة فيه بحيث يتم إنشاء مستند Word جديد لكل صفحة. إنه مطلوب لأنه يتعين علينا تحليل كل صفحة على حدة وستساعدنا ميزات فئة المستند على تحديد الصفحات غير الفارغة. أثناء التحليل ، تتم إضافة كل صفحة غير فارغة إلى ملف Word جديد يحتوي على كل هذه الصفحات.
رمز لحذف الصفحة الفارغة في Word باستخدام C#
using System.Collections; | |
using Aspose.Words; | |
namespace DeleteABlankPageInWordUsingCSharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to delete a blank page in Word using CSharp | |
{ | |
// Instantiate the license to avoid any trial version limitations | |
// and watermark in the output Word file | |
Aspose.Words.License licDeleteBlankPages= new Aspose.Words.License(); | |
licDeleteBlankPages.SetLicense("Aspose.Words.lic"); | |
// Load the Word file having blank pages in it | |
Document originalDocWithFewBlankPages = new Document("WordFileWithBlankPages.docx"); | |
// Declare an array to store page numbers of the empty or blank pages | |
ArrayList listOfBlankPageNumbers = new ArrayList(); | |
listOfBlankPageNumbers.Add(-1); | |
// Extract each page of the loaded document as a separate Word document | |
int totalPagesInOriginalDoc = originalDocWithFewBlankPages.PageCount; | |
for (int iCount = 0; iCount < totalPagesInOriginalDoc; iCount++) | |
{ | |
// Create a new Word document containing the single page | |
Document DocWithOnePage = originalDocWithFewBlankPages.ExtractPages(iCount, 1); | |
// Parse through all the sections of the newly created document | |
// to count text segments and shapes if any | |
int shapesCounter = 0; | |
string pageText = ""; | |
foreach (Section docSection in DocWithOnePage.Sections) | |
{ | |
// Extract text from the section and also get shapes count in the newly created document | |
pageText = pageText + docSection.Body.ToString(SaveFormat.Text); | |
shapesCounter += docSection.Body.GetChildNodes(NodeType.Shape, true).Count; | |
} | |
// Check if the extracted page is null or not and also check if shapes count is 0 or not | |
// If text is blank and shapes count is 0, it means the page is blank | |
if (string.IsNullOrEmpty(pageText.Trim()) && shapesCounter == 0) | |
listOfBlankPageNumbers.Add(iCount); | |
} | |
listOfBlankPageNumbers.Add(totalPagesInOriginalDoc); | |
// Add all the non-empty pages to the final document | |
Document nonEmptyDocument = (Document)originalDocWithFewBlankPages.Clone(false); | |
nonEmptyDocument.RemoveAllChildren(); | |
for (int iCount = 1; iCount < listOfBlankPageNumbers.Count; iCount++) | |
{ | |
int index = (int)listOfBlankPageNumbers[iCount - 1] + 1; | |
int count = (int)listOfBlankPageNumbers[iCount] - index; | |
if (count > 0) | |
nonEmptyDocument.AppendDocument(originalDocWithFewBlankPages.ExtractPages(index, count), ImportFormatMode.KeepSourceFormatting); | |
} | |
// Save the output file having all the non-empty pages | |
nonEmptyDocument.Save(@"NonEmptyPages.docx"); | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
يصف هذا الرمز * لإزالة صفحة فارغة من مستند Word باستخدام C# * المنطق الكامل حيث يتم حفظ كل صفحة من ملف Word المصدر في ملف Word جديد ثم تحليلها لأي محتوى فيه. أثناء هذه العملية ، لا يتم أخذ الرؤوس والتذييلات في الاعتبار ولكن يمكنك تضمينها أيضًا. بمجرد تحليل جميع الصفحات وحفظ أرقامها في مصفوفة ، يتم استخدام هذه المجموعة لإلحاق كل تلك الصفحات بملف Word جديد.
في هذا البرنامج التعليمي ، أزلنا جميع الصفحات الفارغة من ملف Word. إذا كنت تريد تعلم بعض الوظائف الأخرى مثل تحويل مستندات Word إلى HTML ، فراجع المقالة على كيفية تحويل مستند Word إلى HTML باستخدام C#.