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

Supports modify RunStyle #74

Open rstm-sf opened 1 year ago

rstm-sf commented 1 year ago

It would be nice to add modifiing RunStyle from RunProperties. This issue is on par with https://github.com/EvotecIT/OfficeIMO/issues/73

rstm-sf commented 1 year ago

@PrzemyslawKlys maybe you have a vision where/within what it is better to add it?

PrzemyslawKlys commented 1 year ago

I am not sure I understand the question. From what I see I use it exactly 2 times as part of WordHyperLink.

I assume you want to add ability to create custom style and RunStyle is part of making sure that happens.

When I look at Word there are multiple, but limited styles built in + the styles that are part of TOC, again quite limited. I've defined only styles as part of TOC I believe.

image

My idea would be to recreate all the styles that come built-in in Word with their respective name and so on so the user can find out by checking what style it is, but if it doesn't match defined styles it would refer to custom style. ANd custom style would be separate class with it's own definitions? This class would mainly limit inputs to what the GUI proposes?

image

I don't have super experience with this so I'm open to suggestions.

From what I see in Aspose:

Document doc = new Document();

// Create a paragraph style and specify some formatting for it
Style style = doc.Styles.Add(StyleType.Paragraph, "MyStyle1");
style.Font.Size = 24;
style.Font.Name = "Verdana";
style.ParagraphFormat.SpaceAfter = 12;
//Open the document for editing
document.BeginUpdate();

//Create a new paragraph style instance
//and specify the required properties
ParagraphStyle chapterStyle = document.ParagraphStyles.CreateNew();
chapterStyle.Name = "MyTitleStyle";
chapterStyle.ForeColor = Color.SteelBlue;
chapterStyle.FontSize = 16;
chapterStyle.FontName = "Segoe UI Semilight";
chapterStyle.Alignment = ParagraphAlignment.Left;
chapterStyle.SpacingBefore = Units.InchesToDocumentsF(0.2f);
chapterStyle.SpacingAfter = Units.InchesToDocumentsF(0.2f);
chapterStyle.OutlineLevel = 2;

//Add the object to the document collection
document.ParagraphStyles.Add(chapterStyle);

//Finalize the editing
document.EndUpdate();

//Apply the created style to every chapter in the document 
for (int i = 0; i < document.Paragraphs.Count; i++)
{
    string var = document.GetText(document.Paragraphs[i].Range);
    if (var.Contains("Chapter "))
    {
        document.Paragraphs[i].Style = chapterStyle;
    }
}
return;

🤷‍♂️

PrzemyslawKlys commented 1 year ago

Here's one from Syncfusion

//Creates a Word document
using (WordDocument document = new WordDocument())
{
    //This method adds a section and a paragraph in the document
    document.EnsureMinimal();
    //Adds a new paragraph style named "ParagraphStyle"
    WParagraphStyle paraStyle = document.AddParagraphStyle("ParagraphStyle") as WParagraphStyle;
    //Sets the formatting of the style
    paraStyle.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
    //Adds a new character style named "CharacterStyle"
    IWCharacterStyle charStyle = document.AddCharacterStyle("CharacterStyle");
    //Sets the formatting of the style
    charStyle.CharacterFormat.Bold = true;
    charStyle.CharacterFormat.Italic = true;
    //Link both paragraph and character style
     paraStyle.LinkedStyleName = "CharacterStyle";
    //Appends the contents into the paragraph
    document.LastParagraph.AppendText("AdventureWorks Cycles");
    //Applies the style to paragraph
    document.LastParagraph.ApplyStyle("ParagraphStyle");
    //Appends new paragraph in section
    document.LastSection.AddParagraph();
    //Appends the contents into the paragraph
    document.LastParagraph.AppendText("AdventureWorks Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company.");
    //Applies paragraph style to the text range
    (document.LastParagraph.ChildEntities[0] as WTextRange).ApplyStyle("ParagraphStyle");
    //Saves the document
    document.Save("Result.docx", FormatType.Docx);
}

So I guess separate class that you "define" and later on you build it up and apply to paragraphs. Hope that is what you are asking

rstm-sf commented 1 year ago

This is necessary to support the inline style. For example, as you rightly noted Hyperlink. Or another example — inlined code

rstm-sf commented 1 year ago

So I guess separate class that you "define" and later on you build it up and apply to paragraphs. Hope that is what you are asking

Not exactly, it's not for paragraphs, it's for the Run. Ran part of a paragraph and it can have its own style

PrzemyslawKlys commented 1 year ago

Right, but I treat 1 run = 1 WordParagraph in OfficeIMO. And then 1 Paragraph can have multiple runs and be attached to multiple WordParagraphs. So in the end you want a class named like WordParagraphStyle which would have Public property _RunStyle which would allow deep diving if someone doesn't like the "easy" get/set and there would be bunch of defined properties that control things? Or do I misunderstand? Feel free to ignore me tho.