Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling of new line characters #37

Closed
punitganshani opened this issue Oct 3, 2022 · 5 comments
Closed

Handling of new line characters #37

punitganshani opened this issue Oct 3, 2022 · 5 comments

Comments

@punitganshani
Copy link

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
Copy link
Member

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

  • Enter
    image

  • SHIFT+ENTER
    image

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
Copy link
Member

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

public WordParagraph AddPageBreak() {
WordParagraph newWordParagraph = new WordParagraph {
_run = new Run(new Break() { Type = BreakValues.Page }),
_document = this
};
newWordParagraph._paragraph = new Paragraph(newWordParagraph._run);
this._document.Body.Append(newWordParagraph._paragraph);
return newWordParagraph;
}
public WordParagraph AddBreak(BreakValues breakType = BreakValues.Page) {
WordParagraph newWordParagraph = new WordParagraph {
_run = new Run(new Break() { Type = breakType }),
_document = this
};
newWordParagraph._paragraph = new Paragraph(newWordParagraph._run);
this._document.Body.Append(newWordParagraph._paragraph);
this.Paragraphs.Add(newWordParagraph);
return newWordParagraph;
}

@PrzemyslawKlys
Copy link
Member

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
Copy link
Member

Will be available in 0.3.0

@PrzemyslawKlys
Copy link
Member

Added a way to use NewLines directly in text:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants