วิธีการสกัดข้อความภาพและ metadata จากเอกสาร Word ใน .NET
การ extract text, images, and metadata from Word documents is essential for document analysis and processing. ด้วย Aspose.Words for .NET ผู้พัฒนาสามารถรับเนื้อหาเอกสารและคุณสมบัติในรูปแบบการใช้งานต่างๆเช่นการ indexing, archiving, หรือการแปลงเนื้อหา
ข้อกําหนด
- ติดตั้ง .NET SDK .
- เพิ่มแพคเกจ Aspose.Words NuGet:
dotnet add package Aspose.Words
- การเตรียมเอกสาร Word (
document.docx
) ด้วยข้อความภาพและ metadata
คู่มือขั้นตอนเพื่อ استخراجเนื้อหาจากไฟล์ Word
1. ดาวน์โหลดเอกสาร Word
using System;
using Aspose.Words;
class Program
{
static void Main()
{
// Step 1: Load the Word document
string filePath = "document.docx";
Document doc = new Document(filePath);
// Steps 2, 3, and 4 will be added below
}
}
คําอธิบาย: รหัสนี้โหลดเอกสาร Word ที่ระบุไว้ในหน่วยความจําสําหรับการประมวลผลเพิ่มเติม
2. เอาข้อความจากเอกสาร
using System;
using Aspose.Words;
class Program
{
static void Main()
{
string filePath = "document.docx";
Document doc = new Document(filePath);
// Step 2: Extract Text
string text = doc.GetText();
Console.WriteLine("Extracted Text: " + text);
// Steps 3 and 4 will be added below
}
}
คําอธิบาย: รหัสนี้จะสกัดเนื้อหาข้อความทั้งหมดจากเอกสาร Word ที่โหลดและพิมพ์ไว้บนคอนโซล
3. extract metadata จากเอกสาร
using System;
using Aspose.Words;
class Program
{
static void Main()
{
string filePath = "document.docx";
Document doc = new Document(filePath);
string text = doc.GetText();
Console.WriteLine("Extracted Text: " + text);
// Step 3: Extract Metadata
Console.WriteLine("Title: " + doc.BuiltInDocumentProperties.Title);
Console.WriteLine("Author: " + doc.BuiltInDocumentProperties.Author);
Console.WriteLine("Created Date: " + doc.BuiltInDocumentProperties.CreatedTime);
// Step 4 will be added below
}
}
คําอธิบาย: รหัสนี้จะสกัดและพิมพ์ชื่อผู้เขียนและวันที่สร้างข้อมูลโลหะจากเอกสาร Word
4. extract รูปภาพจากเอกสาร
using System;
using Aspose.Words;
class Program
{
static void Main()
{
string filePath = "document.docx";
Document doc = new Document(filePath);
string text = doc.GetText();
Console.WriteLine("Extracted Text: " + text);
Console.WriteLine("Title: " + doc.BuiltInDocumentProperties.Title);
Console.WriteLine("Author: " + doc.BuiltInDocumentProperties.Author);
Console.WriteLine("Created Date: " + doc.BuiltInDocumentProperties.CreatedTime);
// Step 4: Extract Images
int imageCount = 0;
foreach (var shape in doc.GetChildNodes(NodeType.Shape, true))
{
if (shape is Shape { HasImage: true } imageShape)
{
string imageFilePath = $"Image_{++imageCount}.png";
imageShape.ImageData.Save(imageFilePath);
Console.WriteLine($"Saved Image: {imageFilePath}");
}
}
Console.WriteLine("Content extraction completed.");
}
}
คําอธิบาย: รหัสนี้จะสกัดภาพทั้งหมดจากเอกสาร Word และบันทึกเป็นไฟล์ PNG ในไดเรกทอรีโครงการ
5. การทดสอบโซลูชัน
- การรับประกัน
document.docx
มีอยู่ในแผนที่โครงการ - ดําเนินการโปรแกรมและตรวจสอบ:- หมายเลขที่ถอดออกจากคอนโซล
- รายละเอียด metadata พิมพ์
- รูปภาพที่ถูกบันทึกไว้ในโฟลเดอร์โครงการ
วิธีการวางและทํางานบนแพลตฟอร์มหลัก
หน้าต่าง
- ติดตั้ง .NET Runtime และเปิดใช้งานแอป
- ตรวจสอบแอพพลิเคชันโดยดําเนินการผ่านเส้นคําสั่ง
ลิงค์
- ติดตั้ง .NET runtime
- ใช้คําสั่งปลายทางเพื่อดําเนินการแอพหรือโฮสต์แอพบนเซิร์ฟเวอร์
แม็กซ์
- ดําเนินการแอปพลิเคชันโดยใช้ Kestrel หรือ deploy it on a cloud service.
ปัญหาทั่วไปและข้อกําหนด
รูปภาพที่ไม่ได้ดึงดูด:- ตรวจสอบให้แน่ใจว่าเอกสารประกอบด้วยภาพรวมและไม่เชื่อมโยงภายนอก
ข้อมูลโลหะที่หายไป:- ตรวจสอบว่าเอกสารมีคุณสมบัติ metadata เช่น Title หรือ Author Set
การประมวลผลไฟล์ขนาดใหญ:- ใช้วิธีการที่สมบูรณ์แบบเช่นการประมวลผลส่วนที่เฉพาะเจาะจงของเอกสาร
ด้วยคู่มือนี้คุณสามารถ استخراجเนื้อหาที่มีค่าจากเอกสาร Word โดยใช้ Aspose.Words สําหรับ .NET