gpt4 book ai didi

c# - 居中段落文本

转载 作者:行者123 更新时间:2023-11-30 22:04:52 25 4
gpt4 key购买 nike

我已经为页眉创建了一个样式,我想将该文本居中。

StyleDefinitionsPart stylePart = mainPart.AddNewPart<StyleDefinitionsPart>();

Style UserHeading = new Style();
RunProperties UserHeadingRunPro = new RunProperties();
Color UserColour = new Color() { Val = "2E74B5" };

RunFonts UserHeadingFont = new RunFonts();
UserHeadingFont.Ascii = "Calibri Light (Headings)";

Justification CenterHeading = new Justification() { Val = JustificationValues.Center } ;

UserHeadingRunPro.Append(CenterHeading);
UserHeadingRunPro.Append(UserColour);
UserHeadingRunPro.Append(UserHeadingFont);
UserHeadingRunPro.Append(new Bold());
UserHeadingRunPro.Append(new FontSize() { Val = "26" });

UserHeading.StyleId = "userheading";
UserHeading.Append(new Name() { Val = "User Heading" });
UserHeading.Append(new BasedOn() { Val = "Heading1" });
UserHeading.Append(new NextParagraphStyle() { Val = "Normal" });
UserHeading.Append(UserHeadingRunPro);

stylePart.Styles = new Styles();
stylePart.Styles.Append(UserHeading, SideHeading);
stylePart.Styles.Save();

我遇到的问题是代码被调用并将样式应用到它应该做的事情,除了对齐段落以使文本居中。

从这里调用样式;

Paragraph NamePar = new Paragraph();
Run heading_run = new Run();
Text heading_text = new Text(UserName);
ParagraphProperties User_heading_pPr = new ParagraphProperties();

User_heading_pPr.ParagraphStyleId = new ParagraphStyleId() { Val = "userheading" };
NamePar.Append(User_heading_pPr);
heading_run.Append(heading_text);
NamePar.Append(heading_run);

我只是想知道如何将样式userheading 中的文本居中,以便它应用于文本。

最佳答案

来自 ECMA-376:

17.3.1.13 jc (Paragraph Alignment) This element specifies the paragraph alignment which shall be applied to text in this paragraph. If this element is omitted on a given paragraph, its value is determined by the setting previously set at any level of the style hierarchy[...]

这意味着你可以控制对齐方式:

  • 段落级别:

     ParagraphProperties User_heading_pPr = new ParagraphProperties();
    Justification CenterHeading = new Justification() { Val = JustificationValues.Center } ;
    User_heading_pPr.Append(CenterHeading);
    User_heading_pPr.ParagraphStyleId = new ParagraphStyleId() { Val = "userheading" };
    NamePar.Append(User_heading_pPr);

    我知道这不是您要找的。

  • 或在style 级别,您正在尝试做什么。

    你的错误是你将 Justification 对象添加到 runProperties 而必须将它添加到 paragraphProperties.

    ParagraphProperties UserHeadingParagPro = new DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties();
    Justification CenterHeading = new Justification { Val = JustificationValues.Center };
    ///UserHeadingRunPro.Append(CenterHeading) => Your mistake

    [...]

    UserHeadingParagPro.Append(CenterHeading);
    UserHeading.Append(UserHeadingParagPro);

关于c# - 居中段落文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24803519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com