문서 만들기 또는 로드

당신이 수행 할 거의 모든 작업 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();