使用 C# 在 Word 中设置段落格式

本简短指南可帮助您使用 C# 设置 Word 中的 段落格式。它包含设置 IDE 的详细信息、步骤列表以及使用 C# 更改 MS Word 段落格式 的示例代码。它将解释各种属性并演示段落格式、边框和样式的自定义。

使用 C# 设置段落写作格式的步骤

  1. 将 IDE 设置为使用 Aspose.Words for .NET 来格式化文本
  2. 使用 Document 类创建一个新的 Word 文件来设置段落格式
  3. 使用上面的Word文档创建DocumentBuilder类对象
  4. 设置段落格式的缩进和对齐属性
  5. 设置段落边框和字体设置
  6. 添加一些文本以检查上述段落格式
  7. Save 带有格式化文本的新 Word 文件

这些步骤说明如何使用 C# 设置 Word 文档段落格式。创建一个新的 Word 文档,附加一个 DocumentBuilder 对象,并访问 ParagraphFormat 属性以设置对齐方式、左缩进、右缩进和后空格。同样,您可以设置边框和字体属性,例如大小、颜色和粗体。

使用 C# 设置 Microsoft Word 段落格式的代码

using Aspose.Words;
class Program
{
static void Main(string[] args)
{
License lic = new License();
lic.SetLicense("license.lic");
Document document = new Document();
DocumentBuilder docBuilder = new DocumentBuilder(document);
// Set formatting
ParagraphFormat paragraphFormat = docBuilder.ParagraphFormat;
paragraphFormat.Alignment = ParagraphAlignment.Justify;
paragraphFormat.LeftIndent = 45;
paragraphFormat.RightIndent = 45;
paragraphFormat.SpaceAfter = 20;
paragraphFormat.Borders.Horizontal.LineStyle = LineStyle.Double;
paragraphFormat.Style.Font.Size = 12;
paragraphFormat.Style.Font.Color = System.Drawing.Color.Blue;
paragraphFormat.Style.Font.Bold = true;
// Output text
docBuilder.Writeln("Text formatting in a Word file allows users to enhance the appearance of their documents, making them more readable and visually appealing. Common formatting options include changing the font type and size, applying bold, italics, or underline for emphasis, and adjusting text alignment (left, center, right, or justified).");
docBuilder.Writeln("You can also modify line spacing, add bullet points or numbered lists, and use indentation to organize content.");
document.Save("Output.docx");
System.Console.WriteLine("Paragraph formatted in Word file");
}
}

此代码段演示了使用 C# 缩进段落。当我们为文档创建 DocumentBuilder 对象时,我们可以访问其所有属性,例如 ParagraphFormat。您还可以设置页面设置、对齐、行距、分页符、底纹、自动换行,甚至在需要时使用一个命令清除格式。

本文指导我们如何设置新 Word 或现有 Word 文件的格式。如果您想删除 Word 文件的页眉和页脚,请参阅 使用 C# 删除 Word 中的页眉和页脚 上的文章。

 简体中文