翻譯|使用教程|編輯:李顯亮|2021-02-01 09:51:13.310|閱讀 1076 次
概述:在各種情況下,可能需要將MS Word文檔拆分為多個(gè)文檔。為了自動(dòng)進(jìn)行文檔拆分,本文介紹了如何使用Java以編程方式拆分MS Word文檔。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷售中 >>
相關(guān)鏈接:
在各種情況下,可能需要將MS Word文檔拆分為多個(gè)文檔。例如,您可能需要為Word文檔中的每個(gè)頁(yè)面,每個(gè)部分或頁(yè)面集合創(chuàng)建一個(gè)單獨(dú)的文檔。為了自動(dòng)進(jìn)行文檔拆分,本文介紹了如何使用Java以編程方式拆分MS Word文檔。以下各節(jié)提供了上述拆分條件的分步教程和代碼示例。
Aspose.Words for Java是功能強(qiáng)大且功能豐富的文檔處理API,可讓您創(chuàng)建和處理MS Word文檔。除了基本的和高級(jí)的Word自動(dòng)化功能外,API還允許您將Word文檔拆分為多個(gè)文檔。如果想要測(cè)試這項(xiàng)新功能,可點(diǎn)擊這里下載最新版試用。
首先,讓我們看一下如何按頁(yè)面拆分MS Word文檔。在這種情況下,源文檔的每一頁(yè)都將轉(zhuǎn)換為單獨(dú)的Word文檔。以下是拆分Word文檔頁(yè)面的步驟。
下面的代碼示例演示如何使用Java拆分Word文檔。
// Open a Word document Document doc = new Document("Word.docx"); // Split nodes in the document into separate pages DocumentPageSplitter splitter = new DocumentPageSplitter(doc); // Save each page as a separate document for (int page = 1; page <= doc.getPageCount(); page++) { Document pageDoc = splitter.getDocumentOfPage(page); pageDoc.save("SplitDocumentByPage_" + page + ".docx"); }
還可以定義要從源Word文檔中拆分的頁(yè)面范圍。以下是執(zhí)行此操作的步驟。
下面的代碼示例演示如何使用Java按頁(yè)面范圍拆分Word文檔。
// Open a Word document Document doc = new Document("Word.docx"); // Split nodes in the document into separate pages DocumentPageSplitter splitter = new DocumentPageSplitter(doc); // Get part of the document Document pageDoc = splitter.getDocumentOfPageRange(3,6); pageDoc.save("SplitDocumentByPageRange.docx");
Aspose.Words for Java還允許您按分節(jié)符拆分Word文檔。以下是執(zhí)行此操作的步驟。
下面的代碼示例演示如何使用Java按部分拆分Word文檔。
// Load a Word DOCX document Document doc = new Document("word.docx"); for (int i = 0; i < doc.getSections().getCount(); i++) { // Split a document into smaller parts, in this instance split by section Section section = doc.getSections().get(i).deepClone(); // Create a new document Document newDoc = new Document(); newDoc.getSections().clear(); // Add section Section newSection = (Section) newDoc.importNode(section, true); newDoc.getSections().add(newSection); // Save each section as a separate document newDoc.save("splitted_" + i + ".docx"); }
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn