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 page number to footer #611

Open
arifd opened this issue Mar 25, 2023 · 1 comment
Open

Add page number to footer #611

arifd opened this issue Mar 25, 2023 · 1 comment

Comments

@arifd
Copy link
Contributor

arifd commented Mar 25, 2023

Hey thanks for this wonderful little library!

I'm trying to add a page number in the footer of every page, but I'm not able to figure out how to do it.

Using the footer example, I thought maybe just set has_numbering to true, but it doesn't appear to do anything

use docx_rs::*;

pub fn main() -> Result<(), DocxError> {
    let path = std::path::Path::new("footer.docx");
    let file = std::fs::File::create(&path).unwrap();
    let mut footer = Footer::new();
    footer.has_numbering = true;
    footer = footer.add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello")));
    Docx::new()
        .footer(footer)
        .add_paragraph(Paragraph::new().add_run(Run::new().add_text("World")))
        .build()
        .pack(file)?;
    Ok(())
}

If there is no handy API to do it directly yet, this stackoverflow post does describe a way it could be added manually, and I see docx-rs does offer some level of xml manipulation via functions like Docx::add_custom_item and Docx::custom_property but I don't understand the docx format well enough to implement this.

Could you advise?

Thanks in advance!!

@nikitavbv
Copy link

nikitavbv commented Jan 7, 2024

For anyone interested, here is a workaround that can be used here:

Docx::new().footer(
        Footer::new().add_paragraph(
            Paragraph::new().add_run(
                Run::new()
                    .add_field_char(FieldCharType::Begin, false)
                    .add_instr_text(InstrText::Unsupported("PAGE".to_owned()))
                    .add_field_char(FieldCharType::End, false),
            )
        )
    );

(based on the approach from the StackOverflow link from above)

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