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

Add ability to use NewLines ("\r\n"/Environment.NewLine) in AddParagraph, AddText #127

Merged
merged 7 commits into from
Apr 8, 2023

Conversation

PrzemyslawKlys
Copy link
Member

@PrzemyslawKlys PrzemyslawKlys commented Mar 11, 2023

This PR adds the ability to create line breaks using \n, \r\n or Environment.NewLine

var test = document.AddParagraph("TestMe").AddText("\nFirst line\r\nAnd more " + Environment.NewLine + "in new line\r\n");

Is exactly the same as using it in explicit way:

var  test = document.AddParagraph("TestMe").AddBreak().AddText("First line").AddBreak().AddText("And more ").AddBreak().AddText("in new line").AddBreak();

Keep in mind that doing document.Paragraphs[0].Text = "\nFirst line\r\nAnd more " + Environment.NewLine + "in new line\r\n"); doesn't do anything for the Breaks(). I believe that using Text property on paragraph explicitly that would create multiple WordParagraphs from a simple Set should not do that.

Full example:

public static void Example_BasicWordWithNewLines(string folderPath, bool openWord) {
    Console.WriteLine("[*] Creating standard document with different default style (PL)");
    string filePath = System.IO.Path.Combine(folderPath, "BasicWordWithTabs.docx");
    using (WordDocument document = WordDocument.Create(filePath)) {

        var paragraph1 = document.AddParagraph("This is a start");

        Console.WriteLine("Paragraph count (expected 1): " + document.Paragraphs.Count);

        var paragraph2 = document.AddParagraph("This is a start \t\t And more");

        Console.WriteLine("Paragraph count (expected 2): " + document.Paragraphs.Count);

        var paragraph3 = document.AddParagraph("This is a start \t\t And more");
        paragraph3.Underline = UnderlineValues.DashLong;

        Console.WriteLine("Paragraph count (expected 3): " + document.Paragraphs.Count);

        // now we will try to add new line characters, that will force the paragraph to be split into pieces
        // following will split the paragraph into 3 paragraphs - 2 paragraphs with Text, and one Break()
        var paragraph4 = document.AddParagraph("First line\r\nAnd more in new line");

        Console.WriteLine("Paragraph count (expected 6): " + document.Paragraphs.Count);

        // following will split the paragraph into 3 paragraphs - 2 paragraphs with Text, and one Break()
        var paragraph6 = document.AddParagraph("First line\nnd more in new line");

        Console.WriteLine("Paragraph count (expected 9): " + document.Paragraphs.Count);

        // following will split the paragraph into 3 paragraphs - 2 paragraphs with Text, and one Break()
        var paragraph7 = document.AddParagraph("First line" + Environment.NewLine + "And more in new line");

        Console.WriteLine("Paragraph count (expected 12): " + document.Paragraphs.Count);

        // following will split the paragraph into 7 paragraphs - 3 paragraphs with Text, and 4 paragraphs with Break()
        // additionally there's one paragraph at start
        var paragraph8 = document.AddParagraph("TestMe").AddText("\nFirst line\r\nAnd more " + Environment.NewLine + "in new line\r\n");

        Console.WriteLine("Paragraph count (expected 20): " + document.Paragraphs.Count);

        // following will split the paragraph into 7 paragraphs - 3 paragraphs with Text, and 4 paragraphs with Break()
        // additionally there's one paragraph at start
        // it's the same as above but written in a direct way. All above methods are just shortcuts for this
        var paragraph9 = document.AddParagraph("TestMe").AddBreak().AddText("First line").AddBreak().AddText("And more ").AddBreak().AddText("in new line").AddBreak();

        Console.WriteLine("Paragraph count (expected 28): " + document.Paragraphs.Count);

        document.Save(openWord);
    }
}

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

Successfully merging this pull request may close these issues.

1 participant