Створіть або завантажте документ

Майже будь-яке завдання, яке ви хочете виконати за допомогою 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();