วิธีลบหน้าว่างใน Word โดยใช้ C#

วิธีลบหน้าว่างใน Word โดยใช้ C#

คู่มืออย่างรวดเร็วนี้อธิบายวิธีการลบหน้าว่างจากเอกสาร Word (DOCX, DOC, ฯลฯ) โดยใช้ C# กระบวนการนี้เกี่ยวข้องกับการโหลดไฟล์ Word การวิเคราะห์หน้าแต่ละหน้าการระบุหน้าว่างและในที่สุดสร้างเอกสารใหม่โดยไม่มีหน้าว่าง

ประโยชน์ของการลบหน้าสีขาวในเอกสาร Word

  • เอกสารทําความสะอาด:- เพิ่มความสามารถในการอ่านและความมืออาชีพ

  • ขนาดไฟล์ที่ลดลง:- การจัดเก็บข้อมูลที่มีประสิทธิภาพโดยการกําจัดหน้าที่ไม่จําเป็น

  • ความสามารถในการอัตโนมัต:- เหมาะสําหรับการทําความสะอาดเอกสารขนาดใหญ่โดยอัตโนมัติ

ข้อกําหนด: การเตรียมสิ่งแวดล้อม

  • Visual Studio หรืออื่น ๆ .NET IDE
  • Aspose.Words ที่เพิ่มผ่าน NuGet Package Manager

คู่มือขั้นตอนเพื่อลบหน้าสีขาวใน Word โดยใช้ C#

ขั้นตอนที่ 1: การตั้งค่าสภาพแวดล้อม

ติดตั้งห้องสมุด Aspose.Words ผ่านผู้จัดการแพคเกจ NuGet

Install-Package Aspose.Words

ขั้นตอนที่ 2: ดาวน์โหลดเอกสาร Word

ดาวน์โหลดไฟล์ Word ของคุณเดิมโดยใช้วัตถุชั้นเอกสาร

Document originalDoc = new Document("WordFileWithBlankPages.docx");

ขั้นตอนที่ 3: สารสกัดแต่ละหน้าแยก

ผ่านแต่ละหน้าและสกัดแต่ละหน้าไปยังเอกสารที่แยกต่างหากสําหรับการวิเคราะห์

int totalPages = originalDoc.PageCount;

for (int i = 0; i < totalPages; i++)
{
    Document singlePageDoc = originalDoc.ExtractPages(i, 1);
    // Analyze singlePageDoc in next steps
}

ขั้นตอน 4: การวิเคราะห์เอกสารแบบเดี่ยว

ตรวจสอบว่าเอกสารหน้าเดียวมีข้อความหรือรูปแบบ

int shapesCounter = 0;
string pageText = "";

foreach (Section docSection in singlePageDoc.Sections)
{
    pageText += docSection.Body.ToString(SaveFormat.Text);
    shapesCounter += docSection.Body.GetChildNodes(NodeType.Shape, true).Count;
}

ขั้นตอนที่ 5: รักษารายการหน้าที่ไม่ว่างเปล่า

ติดตามหมายเลขหน้าที่มีเนื้อหา

ArrayList blankPages = new ArrayList();
blankPages.Add(-1);

if (string.IsNullOrEmpty(pageText.Trim()) && shapesCounter == 0)
    blankPages.Add(i); // i is page index in loop

ขั้นตอน 6: เพิ่มหน้าที่ไม่ว่างเปล่าไปยังเอกสารใหม่

สร้างเอกสารใหม่และแนบหน้าที่ไม่ว่างเปล่าเท่านั้นโดยใช้รายการติดตาม

Document finalDoc = (Document)originalDoc.Clone(false);
finalDoc.RemoveAllChildren();

blankPages.Add(totalPages);

for (int i = 1; i < blankPages.Count; i++)
{
    int index = (int)blankPages[i - 1] + 1;
    int count = (int)blankPages[i] - index;

    if (count > 0)
        finalDoc.AppendDocument(originalDoc.ExtractPages(index, count), ImportFormatMode.KeepSourceFormatting);
}

ขั้นตอน 7: บันทึกเอกสารที่แก้ไข

เก็บเอกสารใหม่ที่มีหน้าสีขาวลบ

finalDoc.Save(@"cleaned.docx");

ตัวอย่างรหัสสมบูรณ์เพื่อลบหน้าสีขาวใน Word โดยใช้ C#

ต่อไปนี้เป็นตัวอย่างโค้ดที่สามารถดําเนินการได้อย่างสมบูรณ์แสดงให้เห็นถึงขั้นตอนข้างต้น:

Document originalDoc = new Document("WordFileWithBlankPages.docx");

ArrayList blankPages = new ArrayList();
blankPages.Add(-1);

int totalPages = originalDoc.PageCount;

for (int i = 0; i < totalPages; i++)
{
    Document singlePageDoc = originalDoc.ExtractPages(i, 1);
    int shapesCounter = 0;
    string pageText = "";

    foreach (Section docSection in singlePageDoc.Sections)
    {
        pageText += docSection.Body.ToString(SaveFormat.Text);
        shapesCounter += docSection.Body.GetChildNodes(NodeType.Shape, true).Count;
    }

    if (string.IsNullOrEmpty(pageText.Trim()) && shapesCounter == 0)
        blankPages.Add(i);
}

blankPages.Add(totalPages);

Document finalDoc = (Document)originalDoc.Clone(false);
finalDoc.RemoveAllChildren();

for (int i = 1; i < blankPages.Count; i++)
{
    int index = (int)blankPages[i - 1] + 1;
    int count = (int)blankPages[i] - index;

    if (count > 0)
        finalDoc.AppendDocument(originalDoc.ExtractPages(index, count), ImportFormatMode.KeepSourceFormatting);
}

finalDoc.Save(@"NonEmptyPages.docx");
System.Console.WriteLine("Blank pages removed successfully.");

ข้อสรุป

บทความนี้อธิบายวิธีการลบหน้าว่างเปล่าในไฟล์ Word โดยใช้ C# โดยปฏิบัติตามขั้นตอนที่กําหนดคุณสามารถตรวจจับหน้าว่างเปล่าและลบพวกเขาโดยการนําไปสู่เอกสารที่สะอาดมากขึ้น คุณสามารถสํารวจ Aspose.Words สําหรับงานการจัดการเอกสาร Wordเพิ่มเติม

 แบบไทย