Search code examples
c#openxmlopenxml-sdk

Center Paragraph Text


I have created a styling for header and I am wanting to center that text.

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();

The issue I have having is that the code is being called and applying the styling to what it should do except for justifying the paragraph so the text is center.

The style is being called from here;

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);

I was just wondering how would you center the text within the styling userheading so it applies to the text.


Solution

  • From 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[...]

    It means you can control alignement:

    • at paragraph level:

       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);
      

      I know this is not what you are looking for.

    • or at style level, what you are trying to do.

      Your mistake is that you add the Justification object to a runProperties when it must be added to a 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);