Skep Of Laai'n Dokument

Byna enige taak wat u met Aspose.Words wil uitvoer, behels die laai van’n dokument. Die Document klas verteenwoordig’n dokument gelaai in die geheue. Die dokument het verskeie oorlaai konstruktors sodat jy’n leë dokument te skep of om dit te laai van’n lêer of stroom. Die dokument kan gelaai word in enige laai formaat ondersteun deur Aspose.Words. Vir die lys van alle ondersteun vrag formate, sien die LoadFormat opsomming.

Skep’n Nuwe Dokument

Ons sal die Document konstruktor sonder parameters noem om’n nuwe leë dokument te skep. As jy’n dokument programmaties wil genereer, is die eenvoudigste manier om die DocumentBuilder klas te gebruik om dokumentinhoud by te voeg.

Die volgende kode voorbeeld toon hoe om’n dokument te skep met behulp van die dokument bouer:

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);

Laai’n Dokument

Om’n bestaande dokument in enige van die LoadFormat formate te laai, gee die lêernaam of die stroom in een van die Dokumentkonstrukteurs. Die formaat van die gelaaide dokument word outomaties bepaal deur die uitbreiding daarvan.

Laai vanaf’n Lêer

Gee’n lêernaam as’n string aan die Dokumentkonstrukteur om’n bestaande dokument uit’n lêer oop te maak.

Die volgende kode voorbeeld toon hoe om’n dokument oop te maak van’n lêer:

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");

Jy kan die sjabloon lêer van hierdie voorbeeld aflaai van Aspose.Words GitHub.

Laai vanaf’n Stroom

Om’n dokument van’n stroom oop te maak, eenvoudig’n stroom voorwerp wat die dokument bevat in die Dokument konstruktor.

Die volgende kode voorbeeld toon hoe om’n dokument oop te maak van’n stroom:

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();