使用文本文档

在本文中,我们将了解哪些选项可用于通过Aspose.Words处理文本文档。 请注意,这不是可用选项的完整列表,而只是使用其中一些选项的示例。

添加双向标记

您可以使用AddBidiMarks属性指定在以纯文本格式导出时是否在每次BiDi运行之前添加双向标记。 Aspose.Words插入Unicode字符’RIGHT-TO-LEFT MARK' (U+200F)在文本中的每个双向运行之前。 此选项对应于导出为纯文本格式时MSWord文件转换对话中的"添加双向标记"选项。 请注意,只有在MS字中添加了阿拉伯语或希伯来语编辑语言时,它才会出现在对话中。

下面的代码示例演示如何使用AddBidiMarks属性。 此属性的默认值为false:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Input.docx");
System::SharedPtr<TxtSaveOptions> saveOptions = System::MakeObject<TxtSaveOptions>();
saveOptions->set_AddBidiMarks(true);
System::String outputPath = outputDataDir + u"WorkingWithTxt.AddBidiMarks.txt";
doc->Save(outputPath, 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-C
System::SharedPtr<TxtLoadOptions> loadOptions = System::MakeObject<TxtLoadOptions>();
loadOptions->set_DetectNumberingWithWhitespaces(false);
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"LoadTxt.txt", loadOptions);
System::String outputPath = outputDataDir + u"WorkingWithTxt.DetectNumberingWithWhitespaces.docx";
doc->Save(outputPath);

如何在加载TXT期间处理前导和尾随空格

您可以在加载TXT文件期间控制处理前导和尾随空格的方式。 前导空格可以被修剪、保留或转换为缩进,尾随空格可以被修剪或保留。

下面的代码示例演示如何在导入TXT文件时修剪前导和尾随空格:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<TxtLoadOptions> loadOptions = System::MakeObject<TxtLoadOptions>();
loadOptions->set_LeadingSpacesOptions(TxtLeadingSpacesOptions::Trim);
loadOptions->set_TrailingSpacesOptions(TxtTrailingSpacesOptions::Trim);
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"LoadTxt.txt", loadOptions);
System::String outputPath = outputDataDir + u"WorkingWithTxt.HandleSpacesOptions.docx";
doc->Save(outputPath);

在输出TXT中导出页眉和页脚

如果要在输出TXT文档中导出页眉和页脚,可以使用ExportHeadersFootersMode属性。 此属性指定将页眉和页脚导出为纯文本格式的方式。

下面的代码示例演示如何将页眉和页脚导出为纯文本格式:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TxtExportHeadersFootersMode.docx");
System::SharedPtr<TxtSaveOptions> options = System::MakeObject<TxtSaveOptions>();
options->set_SaveFormat(SaveFormat::Text);
// All headers and footers are placed at the very end of the output document.
options->set_ExportHeadersFootersMode(TxtExportHeadersFootersMode::AllAtEnd);
doc->Save(outputDataDir + u"WorkingWithTxt.ExportHeadersFootersMode.AllAtEnd.txt", options);
// Only primary headers and footers are exported at the beginning and end of each section.
options->set_ExportHeadersFootersMode(TxtExportHeadersFootersMode::PrimaryOnly);
doc->Save(outputDataDir + u"WorkingWithTxt.ExportHeadersFootersMode.PrimaryOnly.txt", options);
// No headers and footers are exported.
options->set_ExportHeadersFootersMode(TxtExportHeadersFootersMode::None);
doc->Save(outputDataDir + u"WorkingWithTxt.ExportHeadersFootersMode.None.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-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"input_document");
System::SharedPtr<TxtSaveOptions> options = System::MakeObject<TxtSaveOptions>();
options->get_ListIndentation()->set_Count(1);
options->get_ListIndentation()->set_Character(u'\t');
doc->Save(outputDataDir + u"WorkingWithTxt.UseTabCharacterPerLevelForListIndentation.txt", options);

使用空格字符

下面的代码示例演示如何使用空格字符导出列表级别:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"input_document");
System::SharedPtr<TxtSaveOptions> options = System::MakeObject<TxtSaveOptions>();
options->get_ListIndentation()->set_Count(3);
options->get_ListIndentation()->set_Character(u' ');
doc->Save(outputDataDir + u"WorkingWithTxt.UseSpaceCharacterPerLevelForListIndentation.txt", options);

使用默认缩进

下面的代码示例演示如何使用默认缩进导出列表级别:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
System::SharedPtr<Document> doc1 = System::MakeObject<Document>(inputDataDir + u"input_document");
doc1->Save(outputDataDir + u"WorkingWithTxt.DefaultLevelForListIndentation1.txt");
//Document doc2 = new Document("input_document");
System::SharedPtr<Document> doc2 = System::MakeObject<Document>(inputDataDir + u"Input.docx");
System::SharedPtr<TxtSaveOptions> options = System::MakeObject<TxtSaveOptions>();
doc2->Save(outputDataDir + u"WorkingWithTxt.DefaultLevelForListIndentation2.txt", options);