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

כמעט כל משימה שאתה רוצה לעשות עם 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-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(CreateDocument.class);
// Load the document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("hello world");
doc.save(dataDir + "output.docx");

לטעון מסמך

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

תגית: a File

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// For complete examples and data files, please go to
// https://github.com/aspose-words/Aspose.Words-for-Java
String fileName = "Document.docx";
// Load the document from the absolute path on disk.
Document doc = new Document(dataDir + fileName);

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

תגית: a Stream

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// For complete examples and data files, please go to
// https://github.com/aspose-words/Aspose.Words-for-Java
String filename = "Document.docx";
// Open the stream. Read only access is enough for Aspose.Words to load a
// document.
InputStream in = new FileInputStream(dataDir + filename);
// Load the entire document into memory.
Document doc = new Document(in);
System.out.println("Document opened. Total pages are " + doc.getPageCount());
// You can close the stream now, it is no longer needed because the document is
// in memory.
in.close();