إنشاء مستند أو تحميله

تتضمن أي مهمة تريد تنفيذها باستخدام 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-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، قم بتمرير اسم الملف أو الدفق إلى أحد منشئي المستندات. يتم تحديد تنسيق المستند الذي تم تحميله تلقائيا من خلال امتداده.

تحميل من ملف

قم بتمرير اسم ملف كسلسلة إلى منشئ المستند لفتح مستند موجود من ملف.

يوضح مثال التعليمات البرمجية التالية كيفية فتح مستند من ملف:

// 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.

تحميل من تيار

لفتح مستند من دفق، ما عليك سوى تمرير كائن دفق يحتوي على المستند إلى منشئ المستند.

يوضح مثال التعليمات البرمجية التالية كيفية فتح مستند من دفق:

// 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();