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

There is a problem with numbering parsing when reading the document #759

Open
sxhxliang opened this issue Sep 29, 2024 · 2 comments
Open
Labels
bug Something isn't working

Comments

@sxhxliang
Copy link

sxhxliang commented Sep 29, 2024

Describe the bug

There is a problem with numbering parsing when reading the document.

Reproduced step

use docx_rs::*;
use std::io::{Read, Write};
pub fn main() -> Result<(), DocxError> {
    let path = std::path::Path::new("./numbering.docx");
    let file = std::fs::File::create(path).unwrap();
    Docx::new()
        .add_paragraph(
            Paragraph::new()
                .add_run(Run::new().add_text("Hello"))
                .numbering(NumberingId::new(2), IndentLevel::new(0)),
        )
        .add_abstract_numbering(
            AbstractNumbering::new(2).add_level(
                Level::new(
                    0,
                    Start::new(1),
                    NumberFormat::new("decimal"),
                    LevelText::new("Section %1."),
                    LevelJc::new("left"),
                )
                .indent(
                    Some(1620),
                    Some(SpecialIndentType::Hanging(320)),
                    None,
                    None,
                ),
            ),
        )
        .add_numbering(Numbering::new(2, 2))
        .build()
        .pack(file)?;


    // read file
    let mut file = std::fs::File::open("./numbering.docx").unwrap();
    let mut buf = vec![];
    file.read_to_end(&mut buf).unwrap();
    let docx_copy = read_docx(&buf).unwrap();

    // pack file
    let path = std::path::Path::new("./numbering_copy.docx");
    let file = std::fs::File::create(path).unwrap();
    docx_copy.build().pack(file)?;

    Ok(())

}

Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior

A clear and concise description of what you expected to happen.

Actual behavior

A clear and concise description of what you actual to happen.

Screenshots

./numbering.docx:
image
./numbering_copy.docx:
image

If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information)

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]
@sxhxliang sxhxliang added the bug Something isn't working label Sep 29, 2024
@drewmiller
Copy link

These are the two offending lines around line 40 of numberings.rs:

impl BuildXML for Numberings {
    fn build(&self) -> Vec<u8> {
        let mut b = XMLBuilder::new().declaration(Some(true)).open_numbering();
        //b = b.add_child(&create_default_numbering()); // not needed on the second save
        for n in &self.abstract_nums {
            b = b.add_child(n);
        }
        //b = b.add_child(&Numbering::new(1, 1)); // not needed on the second save
        for n in &self.numberings {
            b = b.add_child(n);
        }
        b.close().build()
    }
}

@drewmiller
Copy link

Also needs:
...

    // Read numberings
    let num_path = rels.find_target_path(NUMBERING_RELATIONSHIP_TYPE);
    if let Some(paths) = num_path {
        if let Some((_, num_path, ..)) = paths.get(0) {
            let data = read_zip(
                &mut archive,
                num_path.to_str().expect("should have numberings"),
            )?;
            let nums = Numberings::from_xml(&data[..])?;
            docx = docx.numberings(nums);
            docx.document_rels.has_numberings = true; // this
        }
    }

Around line 679 in reader/mod.rs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants