Создайте или загрузите документ

Практически любая задача, которую вы хотите выполнить с помощью 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.

Загрузка из потока

Чтобы открыть документ из потока, просто передайте объект stream, содержащий документ, в конструктор документа.

В следующем примере кода показано, как открыть документ из потока:

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