bokuweb / docx-rs

:memo: A .docx file writer with Rust/WebAssembly.
https://bokuweb.github.io/docx-rs/
MIT License
341 stars 59 forks source link

[Bug?] Runs don't respect newlines #621

Closed arifd closed 1 year ago

arifd commented 1 year ago

Describe the bug

Run::new().add_text("Hello\nworld")

will not produce text on two lines

Reproduced step

use docx_rs::*;

pub fn main() -> Result<(), DocxError> {
    let path = std::path::Path::new("./hello.docx");
    let file = std::fs::File::create(path).unwrap();
    Docx::new()
        .add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello\nworld")))
        .build()
        .pack(file)?;
    Ok(())
}

Expected behavior

image

Actual behavior

image

Griklit commented 1 year ago

You need to use Run::add_break(BreakType::TextWrapping) instead of \n. such as:

pub fn main() -> Result<(), DocxError> {
    let path = std::path::Path::new("./hello.docx");
    let file = std::fs::File::create(path).unwrap();
    Docx::new()
        .add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello").add_break(BreakType::TextWrapping).add_text("world")))
        .build()
        .pack(file)?;
    Ok(())
}
arifd commented 1 year ago

Oh, that's painful, but ok! Thanks for your help. :)