文書を分割する
Splittingまたはsplit a documentは、大きな文書をより多くの小さなファイルに分割するプロセスです。 ファイルを分割するにはさまざまな理由があります。 たとえば、特定の文書の一部のページのみが必要であり、全体のページは必要ありません。 または、プライバシー上の理由から、ドキュメントの一部のみを他の人と共有したいと考えています。 分割機能を使用すると、ドキュメントの必要な部分のみを取得し、マークアップ、保存、送信などの必要なアクションを実行できます。
Aspose.Wordsは、見出しまたはセクションによって一つの文書を複数の文書に分割する効率的な方法を提供します。 文書をページまたはページ範囲で分割することもできます。 両方の分割オプションについては、この記事で説明します。
Aspose.Wordsを使用して文書を小さなファイルに分割するには、次の手順を実行する必要があります:
- サポートされている任意の形式で文書をロードします。
- 文書を分割します。
- 出力文書を保存します。
文書を分割した後、必要なページ、テキストなどで始まるすべての出力文書を開くことができます。
オンラインで試す
あなたは私たちのこの機能を試すことができます 無料オンラインドキュメントスプリッタ.
異なる条件 {#split-a-document-using-different-criteria}を使用して文書を分割する
Aspose.Wordsを使用すると、さまざまな基準に従ってEPUBまたはHTML文書をチャプターに分割できます。 このプロセスでは、ソース文書のスタイルとレイアウトが出力文書のために保持されます。
DocumentSplitCriteria列挙体を使用して条件を指定できます。 したがって、次のいずれかの基準を使用してドキュメントを章に分割したり、複数の基準を組み合わせたりすることができます:
- 見出し段落,
- セクションブレーク,
- コラムブレーク,
- 改ページ。
出力をHTMLに保存する場合、Aspose.Wordsは個々のチャプターを個別のHTMLファイルとして保存します。 その結果、文書は複数のHTMLファイルに分割されます。 出力をEPUBに保存する場合、Aspose.Wordsは、使用したDocumentSplitCriteria
値に関係なく、単一のEPUBファイルに結果を保存します。 したがって、EPUBドキュメントにDocumentSplitCriteriaを使用すると、リーダーアプリケーションでのコンテンツの外観にのみ影響します。
このセクションでは、可能な分割基準の一部のみを検討します。
見出し {#split-a-document-by-headings}で文書を分割する
見出しで文書を章に分割するには、DocumentSplitCriteriaプロパティのHeadingParagraph値を使用します。
見出し1、2、3など、特定のレベルの見出し段落でドキュメントを分割する必要がある場合は、DocumentSplitHeadingLevelプロパティも使用します。 出力は、指定された見出しレベルで書式設定された段落で分割されます。
次のコード例は、見出しを使用してドキュメントを小さな部分に分割する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Open a Word document | |
Document doc = new Document(dataDir + "Test File (doc).docx"); | |
HtmlSaveOptions options = new HtmlSaveOptions(); | |
// Split a document into smaller parts, in this instance split by heading | |
options.setDocumentSplitCriteria(DocumentSplitCriteria.HEADING_PARAGRAPH); | |
// Save the output file | |
doc.save(dataDir + "SplitDocumentByHeadings_out.html", options); |
この基準では、Aspose.Wordsは分割時にHTML形式への保存のみをサポートしていることに注意してください。
EPUBに保存すると、文書は複数のファイルに分割されず、出力ファイルは1つだけになります。
セクション {#split-a-document-by-sections}で文書を分割する
Aspose.Wordsでは、セクション区切りを使用して文書を分割し、HTMLに保存することもできます。 この目的のために、SectionBreakをDocumentSplitCriteriaとして使用します:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Open a Word document | |
Document doc = new Document(dataDir + "Test File (doc).docx"); | |
HtmlSaveOptions options = new HtmlSaveOptions(); | |
// Split a document into smaller parts, in this instance split by heading | |
options.setDocumentSplitCriteria(DocumentSplitCriteria.SECTION_BREAK); | |
// Save the output file | |
doc.save(dataDir + "SplitDocumentByHeadings_out.html", options); |
ソース文書を複数の出力文書に分割する別の方法があり、Aspose.Wordsでサポートされている任意の出力形式を選択できます。
次のコード例は、(DocumentSplitCriteria
プロパティを使用せずに)セクション区切りによってドキュメントを小さな部分に分割する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Open a Word document | |
Document doc = new Document(dataDir + "TestFile (Split).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(); | |
Document newDoc = new Document(); | |
newDoc.getSections().clear(); | |
Section newSection = (Section) newDoc.importNode(section, true); | |
newDoc.getSections().add(newSection); | |
// Save each section as a separate document | |
newDoc.save(dataDir + "SplitDocumentBySectionsOut_" + i + ".docx"); | |
} |
ページで分割
また、ドキュメントページをページごと、ページ範囲ごと、または指定したページ番号で開始することもできます。 このような場合、ExtractPagesメソッドはジョブを実行できます。
このセクションでは、DocumentクラスとExtractPagesメソッドを使用して、ドキュメントをページ単位で分割するいくつかのユースケースについて説明します。
文書ページをページ {#split-a-document-page-by-page}で分割する
Aspose.Words複数ページのドキュメントをページごとに分割できます。
次のコード例は、ドキュメントを分割し、各ページを個別のドキュメントとして保存する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "TestFile (Split).docx"); | |
int pageCount = doc.getPageCount(); | |
// Save each page as a separate document. | |
for (int page = 0; page <= pageCount; page++) | |
{ | |
Document extractedPage = doc.extractPages(page, 1); | |
extractedPage.save(dataDir + "SplitDocumentPageByPageOut_" + (page + 1) + ".docx"); | |
} |
ページ範囲 {#split-a-document-by-page-ranges}で文書を分割する
Aspose.Words複数ページのドキュメントをページ範囲で分割できます。 1つのファイルをさまざまなページ範囲で複数のファイルに分割することも、1つの範囲を選択してソースドキュメントのこの部分のみを保存することもできます。 ドキュメントの最大ページ番号と最小ページ番号に応じて、ページ範囲を選択できることに注意してください。
次のコード例は、特定の開始インデックスと終了インデックスを使用して、ページ範囲ごとに文書を小さな部分に分割する方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "TestFile (Split).docx"); | |
// Get part of the document. | |
Document extractedPages = doc.extractPages(3, 6); | |
extractedPages.save(dataDir + "SplitDocumentByPageRangeOut.docx"); |
ドキュメント {#callback-option-for-saving-a-document}を保存するコールバックオプション
DocumentPartSavingCallbackプロパティを使用して、このドキュメントをHTML形式にエクスポートするときにAspose.Wordsがドキュメントパーツを保存する方法を制御できます。 このプロパティを使用すると、出力ファイルの名前を変更したり、カスタムストリームにリダイレク
このコールバックは、すべての出力部分を単一のコンテナに保存する必要があるため、EPUBに保存するときには有用ではないことに注意してください。epubファイル。 そのため、ストリームのリダイレクトはサポートされておらず、ファイルはコンテナ内で名前が変更されるため、名前の変更の効果は表示されません。
分割された文書を別の文書 {#merge-the-split-document-with-another-file}とマージする
Aspose.Wordsを使用すると、出力分割文書を別の文書とマージして新しい文書を作成できます。 これはドキュメントマージと呼ぶことができます。
次のコード例は、分割された文書を別の文書とマージする方法を示しています:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
public static void MergeDocuments(String dataDir) throws Exception{ | |
// Find documents using for merge | |
File f = new File(dataDir); | |
FilenameFilter filter = new FilenameFilter() { | |
@Override | |
public boolean accept(File f, String name) { | |
return name.endsWith(".docx"); | |
} | |
}; | |
String[] documentPaths = f.list(filter); | |
String sourceDocumentPath = dataDir + documentPaths[0]; | |
// Open the first part of the resulting document | |
Document sourceDoc = new Document(sourceDocumentPath); | |
// Create a new resulting document | |
Document mergedDoc = new Document(); | |
DocumentBuilder mergedDocBuilder = new DocumentBuilder(mergedDoc); | |
// Merge document parts one by one | |
for (String documentPath : documentPaths) | |
{ | |
String documentPathFull = dataDir + documentPath; | |
if (documentPathFull == sourceDocumentPath) | |
continue; | |
mergedDocBuilder.moveToDocumentEnd(); | |
mergedDocBuilder.insertDocument(sourceDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING); | |
sourceDoc = new Document(documentPathFull); | |
} | |
// Save the output file | |
mergedDoc.save(dataDir + "MergeDocuments_out.docx"); | |
} |