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

Handling of new line characters #37

Closed punitganshani closed 1 year ago

punitganshani commented 1 year ago

Right now if the text contains NewLine character, the rendering as Text or Paragraph ignores the newline and renders its as plain text.

If we manually add Paragraph there is additional BREAK (ENTER) rendered which is not rendered if we press SHIFT+ENTER in Word.

Example

                    var json = JValue.Parse(oldJson).ToString(Formatting.Indented);
                    string[ ] newLineArray = { Environment.NewLine };
                    string[ ] textArray = json.Split( newLineArray, StringSplitOptions.None );
                    for (int index = 0; index < textArray.Length; index++)
                    {                             
                        document.AddParagraph(textArray[index]);          
                    }

This will render the Word Document as below,

image

What is expected is

image

Please suggest what is the best way to replace an ENTER with a SHIFT+ENTER

PrzemyslawKlys commented 1 year ago

So - the difference between ENTER and SHIFT+ENTER on the XML tells us what the issue is about:

I would probably need to add new method for paragraph AddBreak to allow you to use it. Unless you would be willing to do that

PrzemyslawKlys commented 1 year ago

Currntly break is exposed but for Document, and not Paragraph.

https://github.com/EvotecIT/OfficeIMO/blob/7010fd22e141db5880094a6d61ee953cd3b2c2e3/OfficeIMO.Word/WordDocument.PublicMethods.cs#L23-L44

PrzemyslawKlys commented 1 year ago

image

public static void Example_BasicWordWithBreaks(string folderPath, bool openWord) {
    Console.WriteLine("[*] Creating standard document with paragraph & breaks");
    string filePath = System.IO.Path.Combine(folderPath, "BasicDocumentWithParagraphsAndBreaks.docx");
    using (WordDocument document = WordDocument.Create(filePath)) {
        var paragraph1 = document.AddParagraph("Adding paragraph1 with some text and pressing ENTER");

        var paragraph2 = document.AddParagraph("Adding paragraph2 with some text and pressing SHIFT+ENTER");
        paragraph2.AddBreak();
        paragraph2.AddText("Continue1");
        paragraph2.AddBreak();
        paragraph2.AddText("Continue2");

        var paragraph3 = document.AddParagraph("Adding paragraph3 with some text and pressing ENTER");

        document.Save(openWord);
    }
}

Would that work?

image

public static void Example_BasicWordWithBreaks(string folderPath, bool openWord) {
    Console.WriteLine("[*] Creating standard document with paragraph & breaks");
    string filePath = System.IO.Path.Combine(folderPath, "BasicDocumentWithParagraphsAndBreaks.docx");
    using (WordDocument document = WordDocument.Create(filePath)) {
        var paragraph1 = document.AddParagraph("Adding paragraph1 with some text and pressing ENTER");

        var paragraph2 = document.AddParagraph("Adding paragraph2 with some text and pressing SHIFT+ENTER");
        paragraph2.AddBreak();
        paragraph2.AddText("Continue1");
        paragraph2.AddBreak();
        paragraph2.AddText("Continue2");

        var paragraph3 = document.AddParagraph("Adding paragraph3 with some text and pressing ENTER");

        var paragraph4 = document.AddParagraph("Adding paragraph4 with some text and pressing SHIFT+ENTER");
        paragraph4.AddBreak();

        document.Save(openWord);
    }
}

Which looks like this

image

PrzemyslawKlys commented 1 year ago

Will be available in 0.3.0

PrzemyslawKlys commented 1 year ago

Added a way to use NewLines directly in text: