ליצור או לטעון מסמך

כמעט כל משימה שאתה רוצה לעשות עם Aspose.Words כולל טעינה של מסמך. The The The Document הכיתה מייצגת מסמך טעון בזיכרון. המסמך יש כמה מבני בנייה מוגזמים המאפשרים לך ליצור מסמך ריק או לטעון אותו מקובץ או זרם. ניתן לטעון את המסמך בכל פורמט עומס נתמך על ידי Aspose.Words. לרשימה של כל פורמטי העומס הנתמכים, ראה את LoadFormat אזהרה.

יצירת מסמך חדש

נקרא The Document לבנות ללא פרמטרים כדי ליצור מסמך ריק חדש. אם אתה רוצה ליצור מסמך באופן מתודולוגי, הדרך הפשוטה ביותר היא להשתמש DocumentBuilder שיעור להוסיף תוכן מסמך.

הדוגמה הבאה של הקוד מראה כיצד ליצור מסמך באמצעות בונה המסמך:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Hello World!");
doc.Save(ArtifactsDir + "AddContentUsingDocumentBuilder.CreateNewDocument.docx");
view raw create-docx.cs hosted with ❤ by GitHub

לטעון מסמך

על מנת לטעון מסמך קיים בכל אחד מהם LoadFormat פורמטים, להעביר את שם הקובץ או את הזרם לתוך אחד מבני המסמך. פורמט המסמך המוטע נקבע באופן אוטומטי על ידי הרחבה שלו.

תגית: a File

להעביר שם קובץ כמחרוזת אל בונה המסמך כדי לפתוח מסמך קיים מקובץ.

לדוגמה הקוד הבא מראה כיצד לפתוח מסמך מקובץ:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(MyDir + "Document.docx");
view raw load-docx.cs hosted with ❤ by GitHub

ניתן להוריד את קובץ התבנית של דוגמה זו Aspose.Words GitHub.

תגית: a Stream

כדי לפתוח מסמך מזרם, פשוט להעביר אובייקט זר המכיל את המסמך לתוך בונה המסמכים.

לדוגמה הקוד הבא מראה כיצד לפתוח מסמך מתוך זרם:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Read only access is enough for Aspose.Words to load a document.
Stream stream = File.OpenRead(MyDir + "Document.docx");
Document doc = new Document(stream);
// You can close the stream now, it is no longer needed because the document is in memory.
stream.Close();