יצירה או טעינה של מסמך

כמעט כל משימה שברצונך לבצע עם 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();