EvotecIT / OfficeIMO

Fast and easy to use cross-platform .NET library that creates or modifies Microsoft Word (DocX) and later also Excel (XLSX) files without installing any software. Library is based on Open XML SDK
MIT License
286 stars 50 forks source link

How To get the correct style for custom list ? #256

Open khofesh opened 3 weeks ago

khofesh commented 3 weeks ago

OfficeIMO version: 0.18.0

Hi, I'm trying to change the color of the bullet point

here's my code


using WordDocument document = WordDocument.Create();

NumberingDefinitionsPart numberingPart = document._document.MainDocumentPart
    .AddNewPart<NumberingDefinitionsPart>("OSgBuNd6wIbboubIU5c0");
numberingPart.Numbering = new Numbering(
    new AbstractNum(
        new Level(
            new NumberingFormat() { Val = NumberFormatValues.Bullet },
            new LevelText() { Val = "•" },
            new RunProperties(
                new Color { Val = fontColorStr }
            ),
            new ParagraphProperties(
                new Indentation() { Left = "700", Hanging = "360" }
            )
        )
        { LevelIndex = 0 }
    )
    { AbstractNumberId = 1 },
    new NumberingInstance(
        new AbstractNumId() { Val = 1 }
    )
    { NumberID = 420 }
);

a function to generate list

    public static WordParagraph CreateCustomBulletListItem(WordDocument document, string text, string textColor, int fontSize)
    {
        var paragraph = new Paragraph();

        var numberingProperties = new NumberingProperties(
            new NumberingLevelReference() { Val = 0 },
            new NumberingId() { Val = 420 }
        );

        var textRunProperties = new RunProperties(
            new Color() { Val = textColor },
            new RunFonts() { Ascii = "Calibri", HighAnsi = "Calibri" },
            new FontSize() { Val = (fontSize * 2).ToString() }  // OpenXML uses half-points
        );

        var paragraphProperties = new ParagraphProperties(
            numberingProperties,
            new SpacingBetweenLines() { After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto }
        );

        // spacing properties
        SpacingBetweenLines spacingBetweenLines = new SpacingBetweenLines()
        {
            Before = "0", // Spacing before (in twips, 1/20 pt)
            After = "0",  // Spacing after (in twips, 1/20 pt)
            Line = "240", // Line spacing (240 twips = 12 points = single spacing)
            LineRule = LineSpacingRuleValues.Auto // Automatically adjust line spacing
        };

        paragraphProperties.Append(spacingBetweenLines);

        var textRun = new Run(
            textRunProperties,
            new Text(text)
        );

        paragraph.Append(paragraphProperties, textRun);

        return document.AddParagraph(
            new WordParagraph(document, paragraph)
        );
    }

creating the custom list

                CustomHorizontalLine(document);
                document.AddParagraph(RegulerStyle(""));

                CreateCustomBulletListItem(
                    document, "this is a test", fontColorStr, defaultFontSize
                );
                CreateCustomBulletListItem(
                    document, "this is a test2", fontColorStr, defaultFontSize
                );

                CustomHorizontalLine(document);
                document.AddParagraph(RegulerStyle(""));

the result is not quite satistying

image (2)

How to get the default style like this ? image

var softwareList = document.AddList(WordListStyle.Bulleted);
foreach (var skill in someData)
{
    var item = pccList.AddItem($"{skill.Name}", 0)
        .SetColor(fontColor);
    item.FontSize = defaultFontSize;
    item.LineSpacing = 240;
    item.LineSpacingAfter = 0;
    item.LineSpacingBefore = 0;
    item.LineSpacingRule = LineSpacingRuleValues.Auto;
}

thanks in advance

PrzemyslawKlys commented 3 weeks ago

To be honest - I don't know :-)

Try looking at this PR:

I started building something that would allow creating custom lists or loading ones, but I am not sure at the moment what is the state of it and whether it actually works

PrzemyslawKlys commented 3 weeks ago

It could also be related to work in here:

khofesh commented 3 weeks ago

@PrzemyslawKlys Hi, thank you.

I'll look into the URLs you shared