テキスト文書の操作

この記事では、Aspose.Wordsを介してテキスト文書を操作するためにどのようなオプションが役立つかを学びます。 これは利用可能なオプションの完全なリストではなく、それらのいくつかを操作する例にすぎないことに注意してください。

双方向マークの追加

AddBidiMarksプロパティを使用して、プレーンテキスト形式でエクスポートするときに、各BiDiを実行する前に双方向マークを追加するかどうかを指定できます。 Aspose.WordsUnicode文字を挿入します’RIGHT-TO-LEFT MARK' (U+200F)テキストの各二方向の操業の前に。 このオプションは、プレーンテキスト形式にエクスポートするときのMSWordファイル変換ダイアログの"双方向マークの追加"オプションに対応します。 アラビア語またはヘブライ語の編集言語のいずれかがMSWordに追加されている場合にのみ、ダイアログに表示されることに注意してください。

次のコード例は、TxtSaveOptions.AddBidiMarksプロパティを使用する方法を示しています。 このプロパティのデフォルト値はtrueです:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "Input.docx");
TxtSaveOptions saveOptions = new TxtSaveOptions();
//The default value is false.
saveOptions.setAddBidiMarks(true);
dataDir = dataDir + "Document.AddBidiMarks_out.txt";
doc.save(dataDir, saveOptions);

TXTの読み込み中にリスト項目を認識する

Aspose.Wordsは、テキストファイルのリスト項目をリスト番号またはプレーンテキストとしてドキュメントオブジェクトモデルにインポートできます。 DetectNumberingWithWhitespacesプロパティを使用すると、文書をプレーンテキスト形式からインポートするときに番号付きリスト項目を認識する方法を指定できます:

  • このオプションがtrueに設定されている場合、空白はリスト番号の区切り文字としても使用されます。アラビア語スタイルの番号付けのリスト認識アルゴリズム(1., 1.1.2.)空白とドット(".")記号の両方を使用します。
  • このオプションがfalseに設定されている場合、リスト認識アルゴリズムは、リスト番号がドット、右括弧、または箇条書き記号(次のような)で終わっている"•", “*”, “-” または"o")。

次のコード例は、このプロパティを使用する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
TxtLoadOptions loadOptions = new TxtLoadOptions();
loadOptions.setDetectNumberingWithWhitespaces(false);
Document doc = new Document(dataDir + "LoadTxt.txt", loadOptions);
dataDir = dataDir + "DetectNumberingWithWhitespaces_out.docx";
doc.save(dataDir);

TXTの読み込み中に先頭と末尾のスペースを処理する

TXTファイルの読み込み中に先頭と末尾のスペースを処理する方法を制御できます。 先頭のスペースはトリミング、保持、またはインデントに変換でき、末尾のスペースはトリミングまたは保持できます。

以下のコード例は、TXTファイルのインポート中に先頭と末尾のスペースをトリミングする方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
TxtLoadOptions loadOptions = new TxtLoadOptions();
loadOptions.setLeadingSpacesOptions(TxtLeadingSpacesOptions.TRIM);
loadOptions.setTrailingSpacesOptions(TxtTrailingSpacesOptions.TRIM);
Document doc = new Document(dataDir + "LoadTxt.txt", loadOptions);
dataDir = dataDir + "HandleSpacesOptions_out.docx";
doc.save(dataDir);

文書のテキストの方向を検出する

Aspose.Wordsは、文書内のテキストの方向(RTL/LTR)を検出するために、TxtLoadOptionsクラスのDocumentDirectionプロパティを提供します。 このプロパティは、DocumentDirection列挙体で提供されるドキュメントテキストの方向を設定または取得します。 デフォルト値は左から右です。

次のコード例は、TXTファイルのインポート中にドキュメントのテキスト方向を検出する方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
TxtLoadOptions loadOptions = new TxtLoadOptions();
loadOptions.setDocumentDirection(DocumentDirection.AUTO);
Document doc = new Document(dataDir + "arabic.txt", loadOptions);
Paragraph paragraph = doc.getFirstSection().getBody().getFirstParagraph();
System.out.println(paragraph.getParagraphFormat().getBidi());
dataDir = dataDir + "DocumentDirection_out.docx";
doc.save(dataDir);

出力TXTファイルのヘッダーとフッターをエクスポートする

出力TXTドキュメントのヘッダーとフッターをエクスポートする場合は、ExportHeadersFootersModeプロパティを使用できます。 このプロパティは、ヘッダーとフッターをプレーンテキスト形式にエクスポートする方法を指定します。

ヘッダーとフッターをプレーンテキスト形式にエクスポートする方法を次のコード例に示します:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "TxtExportHeadersFootersMode.docx");
TxtSaveOptions options = new TxtSaveOptions();
options.setSaveFormat(SaveFormat.TEXT);
// All headers and footers are placed at the very end of the output document.
options.setExportHeadersFootersMode(TxtExportHeadersFootersMode.ALL_AT_END);
doc.save(dataDir + "outputFileNameA.txt", options);
// Only primary headers and footers are exported at the beginning and end of each section.
options.setExportHeadersFootersMode(TxtExportHeadersFootersMode.PRIMARY_ONLY);
doc.save(dataDir + "outputFileNameB.txt", options);
// No headers and footers are exported.
options.setExportHeadersFootersMode(TxtExportHeadersFootersMode.NONE);
doc.save(dataDir + "outputFileNameC.txt", options);

出力TXTのリストインデントをエクスポートする

Aspose.Wordsプレーンテキスト形式にエクスポートする際にリストレベルをインデントする方法を指定できるTxtListIndentationクラスを導入しました。 TxtSaveOptionを使用している間、ListIndentationプロパティは、リストレベルのインデントに使用する文字を指定し、一つのリストレベルごとにインデントとして使用する文字数を指定するために提供されます。

Characterプロパティのデフォルト値は'\0’で、インデントがないことを示します。 Countプロパティのデフォルト値は0で、インデントがないことを意味します。

タブ文字を使用する

次のコード例は、タブ文字を使用してリストレベルをエクスポートする方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "Input.docx");
TxtSaveOptions options = new TxtSaveOptions();
options.getListIndentation().setCount(1);
options.getListIndentation().setCharacter('\t');
doc.save(dataDir + "output.txt", options);

スペース文字を使用する

次のコード例は、スペース文字を使用してリストレベルをエクスポートする方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "Input.docx");
TxtSaveOptions options = new TxtSaveOptions();
options.getListIndentation().setCount(3);
options.getListIndentation().setCharacter(' ');
doc.save(dataDir + "output.txt", options);

デフォルトのインデントを使用する

既定のインデントを使用してリストレベルをエクスポートする方法を次のコード例に示します:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "Input.docx");
doc.save(dataDir + "output1.txt");
Document doc2 = new Document("Input.docx");
TxtSaveOptions options = new TxtSaveOptions();
doc2.save(dataDir + "output2.txt", options);