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
280 stars 50 forks source link

SetFontFamily pattern for every document created #45

Closed eliseudev closed 1 year ago

eliseudev commented 1 year ago

Would you have to set a default FontFamily for every document, without having to add to each paragraph you create?

follows the example in the image.

image

PrzemyslawKlys commented 1 year ago

It's not exposed now, but I would suggest doing this differently. Just work without setting font family and then once everything is done just loop thru all paragraphs

foreach (var paragraph in document.Paragraphs) {
    paragraph.SetFontFamily(...).SetFontSize(10)
}

You will fix this in 3 lines. I'll keep this open, as it should be possible to define default font/style.

eliseudev commented 1 year ago

Não está exposto agora, mas eu sugeriria fazer isso de forma diferente. Basta trabalhar sem definir a família font e, em seguida, uma vez que tudo é feito apenas loop th através de todos os parágrafos

foreach (var paragraph in document.Paragraphs) {
    paragraph.SetFontFamily(...).SetFontSize(10)
}

Você vai consertar isso em 3 linhas. Vou manter isso aberto, pois deve ser possível definir fonte padrão/style.

Thanks, I thought I had a generic method, but so too solves my problem.

PrzemyslawKlys commented 1 year ago

I will add native way to do it in next release:

PrzemyslawKlys commented 1 year ago

PR was merged

      public static void Example_BasicWordWithDefaultStyleChange(string folderPath, bool openWord) {
            Console.WriteLine("[*] Creating standard document with different default style");
            string filePath = System.IO.Path.Combine(folderPath, "BasicWordWithDefaultStyleChange.docx");
            using (WordDocument document = WordDocument.Create(filePath)) {
                document.Settings.FontSize = 30;
                document.Settings.FontFamily = "Calibri Light";
                document.Settings.Language = "pl-PL";

                var paragraph1 = document.AddParagraph("To jest po polsku");

                var paragraph2 = document.AddParagraph("Adding paragraph1 with some text and pressing ENTER");
                paragraph2.FontSize = 15;
                paragraph2.FontFamily = "Courier New";

                document.Save(openWord);
            }
        }

Should be available in next version