การสร้างหรือโหลดเอกสาร

เกือบทุกงานที่คุณต้องการดำเนินการด้วยAspose.Wordsเกี่ยวข้องกับการโหลดเอกสาร คลาสDocumentแทนเอกสารที่โหลดลงในหน่วยความจำ เอกสารมีหลายตัวสร้างมากเกินไปช่วยให้คุณสามารถสร้างเอกสารที่ว่างเปล่าหรือการโ เอกสารสามารถโหลดในรูปแบบโหลดใดๆที่สนับสนุนโดยAspose.Words สำหรับรายการของรูปแบบการโหลดที่รองรับทั้งหมดโปรดดูที่การแจงนับLoadFormat.

สร้างเอกสารใหม่

เราจะเรียกตัวสร้างDocumentโดยไม่มีพารามิเตอร์ในการสร้างเอกสารที่ว่างเปล่าใหม่. ถ้าคุณต้องการสร้างเอกสารแบบโปรแกรมวิธีที่ง่ายที่สุดคือใช้คลาสDocumentBuilderเพื่อเพิ่มเนื้อหาของเ.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการสร้างเอกสารโดยใช้ตัวสร้างเอกสาร:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directory.
System::String outputDataDir = GetOutputDataDir_LoadingAndSaving();
// Initialize a Document.
System::SharedPtr<Document> doc = System::MakeObject<Document>();
// Use a document builder to add content to the document.
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Hello World!");
System::String outputPath = outputDataDir + u"CreateDocument.docx";
// Save the document to disk.
doc->Save(outputPath);

ป้อนเอกสาร

เมื่อต้องการโหลดเอกสารที่มีอยู่ในรูปแบบใดๆของLoadFormatให้ส่งชื่อไฟล์หรือสตรีมไปยังตัวสร้างเอ รูปแบบของเอกสารที่โหลดจะถูกกำหนดโดยอัตโนมัติโดยส่วนขยาย.

โหลดจากแฟ้ม

ผ่านชื่อแฟ้มเป็นสตริงไปยังตัวสร้างเอกสารเพื่อเปิดเอกสารที่มีอยู่จากแฟ้ม.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเปิดเอกสารจากแฟ้ม:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_LoadingAndSaving();
System::String outputDataDir = GetOutputDataDir_LoadingAndSaving();
// Load the document from the absolute path on disk.
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Document.docx");

คุณสามารถดาวน์โหลดไฟล์แม่แบบของตัวอย่างนี้ได้จาก Aspose.Words GitHub.

โหลดจากสตรีม

เมื่อต้องการเปิดเอกสารจากสตรีมเพียงผ่านวัตถุสตรีมที่ประกอบด้วยเอกสารลงในตัวสร้.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเปิดเอกสารจากสตรีม:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_LoadingAndSaving();
System::String outputDataDir = GetOutputDataDir_LoadingAndSaving();
// Open the stream. Read only access is enough for Aspose.Words to load a document.
System::SharedPtr<System::IO::Stream> stream = System::IO::File::OpenRead(inputDataDir + u"Document.docx");
// Load the entire document into memory.
System::SharedPtr<Document> doc = System::MakeObject<Document>(stream);
// You can close the stream now, it is no longer needed because the document is in memory.
stream->Close();