翻譯|使用教程|編輯:李顯亮|2020-08-07 10:13:03.560|閱讀 1009 次
概述:Document類表示加載到內(nèi)存中的文檔。該文檔具有多個(gè)重載的構(gòu)造函數(shù),使您可以創(chuàng)建空白文檔或從文件或流中加載它。本文將介紹如何使用Aspose.words for java創(chuàng)建或加載文檔。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Aspose.Words for Java是功能豐富的文字處理API,開發(fā)人員可以在自己的Java應(yīng)用程序中嵌入生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印Microsoft Word支持的所有格式的功能。它不依賴于Microsoft Word,但是它提供了Microsoft Word通過其API支持的功能。
>>Aspose.Words for Java已經(jīng)更新至v20.7,有97項(xiàng)改進(jìn)和修復(fù),點(diǎn)擊下載體驗(yàn)
我們將調(diào)用 不帶參數(shù)的 Document構(gòu)造函數(shù)來創(chuàng)建一個(gè)新的空白文檔。如果要以編程方式生成文檔,最簡單的方法是使用 DocumentBuilder 類添加文檔內(nèi)容。以下代碼示例顯示了如何使用文檔構(gòu)建器創(chuàng)建文檔:
// 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");
注意默認(rèn)值:
要以任何LoadFormat 格式加載現(xiàn)有文檔 ,請將文件名或流傳遞到其中一個(gè)Document構(gòu)造函數(shù)中。加載文檔的格式由其擴(kuò)展名自動確定。
將文件名作為字符串傳遞給Document構(gòu)造函數(shù)以從文件中打開現(xiàn)有文檔。下面的代碼示例演示如何從文件中打開文檔:
String fileName = "Document.docx"; // Load the document from the absolute path on disk. Document doc = new Document(dataDir + fileName);
要從流中打開文檔,只需將包含文檔的流對象傳遞到Document構(gòu)造函數(shù)中。下面的代碼示例演示如何從流中打開文檔:
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();
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn