diff --git a/docx-core/examples/header.rs b/docx-core/examples/header.rs index f4b9e82..b62bec1 100644 --- a/docx-core/examples/header.rs +++ b/docx-core/examples/header.rs @@ -3,8 +3,10 @@ use docx_rs::*; pub fn main() -> Result<(), DocxError> { let path = std::path::Path::new("./output/header.docx"); let file = std::fs::File::create(&path).unwrap(); + let header = + Header::new().add_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello"))); Docx::new() - .add_header_paragraph(Paragraph::new().add_run(Run::new().add_text("Hello"))) + .header(header) .add_paragraph(Paragraph::new().add_run(Run::new().add_text("World"))) .build() .pack(file)?; diff --git a/docx-core/src/documents/content_types.rs b/docx-core/src/documents/content_types.rs index f65c907..30ad444 100644 --- a/docx-core/src/documents/content_types.rs +++ b/docx-core/src/documents/content_types.rs @@ -12,6 +12,7 @@ pub struct ContentTypes { types: BTreeMap, web_extension_count: usize, custom_xml_count: usize, + header_count: usize, footer_count: usize, } @@ -71,10 +72,6 @@ impl ContentTypes { "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml" .to_owned(), ); - self.types.insert( - "/word/header1.xml".to_owned(), - "application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml".to_owned(), - ); self.types.insert( "/word/commentsExtended.xml".to_owned(), "application/vnd.openxmlformats-officedocument.wordprocessingml.commentsExtended+xml" @@ -116,6 +113,15 @@ impl ContentTypes { self } + pub fn add_header(mut self) -> Self { + self.types.insert( + format!("/word/header{}.xml", self.header_count), + "application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml".to_owned(), + ); + self.header_count += 1; + self + } + pub fn add_footer(mut self) -> Self { self.types.insert( format!("/word/footer{}.xml", self.footer_count), @@ -132,6 +138,7 @@ impl Default for ContentTypes { types: BTreeMap::new(), web_extension_count: 1, custom_xml_count: 1, + header_count: 1, footer_count: 1, } } @@ -212,6 +219,7 @@ mod tests { types, web_extension_count: 1, custom_xml_count: 1, + header_count: 1, footer_count: 1, }, c diff --git a/docx-core/src/documents/document.rs b/docx-core/src/documents/document.rs index 71c438b..c980aed 100644 --- a/docx-core/src/documents/document.rs +++ b/docx-core/src/documents/document.rs @@ -150,6 +150,11 @@ impl Document { self } + pub fn header_reference(mut self, r: HeaderReference) -> Self { + self.section_property = self.section_property.header_reference(r); + self + } + pub fn footer_reference(mut self, r: FooterReference) -> Self { self.section_property = self.section_property.footer_reference(r); self @@ -201,7 +206,7 @@ mod tests { str::from_utf8(&b).unwrap(), r#" - Hello + Hello "# ); } diff --git a/docx-core/src/documents/document_rels.rs b/docx-core/src/documents/document_rels.rs index 892a903..4323706 100644 --- a/docx-core/src/documents/document_rels.rs +++ b/docx-core/src/documents/document_rels.rs @@ -11,6 +11,7 @@ pub struct DocumentRels { pub has_numberings: bool, pub image_ids: Vec, pub custom_xml_count: usize, + pub header_count: usize, pub footer_count: usize, } @@ -32,6 +33,7 @@ impl Default for DocumentRels { has_numberings: false, image_ids: vec![], custom_xml_count: 0, + header_count: 0, footer_count: 0, } } @@ -85,6 +87,14 @@ impl BuildXML for DocumentRels { ) } + for i in 0..self.header_count { + b = b.relationship( + &create_header_rid(i + 1), + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header", + &format!("header{}.xml", i + 1), + ) + } + for i in 0..self.footer_count { b = b.relationship( &create_footer_rid(i + 1), diff --git a/docx-core/src/documents/elements/section.rs b/docx-core/src/documents/elements/section.rs index 667057e..f490f0c 100644 --- a/docx-core/src/documents/elements/section.rs +++ b/docx-core/src/documents/elements/section.rs @@ -53,7 +53,7 @@ mod tests { assert_eq!( str::from_utf8(&b).unwrap(), r#" - + "# ); } diff --git a/docx-core/src/documents/elements/section_property.rs b/docx-core/src/documents/elements/section_property.rs index 8e473fc..1edf199 100644 --- a/docx-core/src/documents/elements/section_property.rs +++ b/docx-core/src/documents/elements/section_property.rs @@ -12,7 +12,7 @@ pub struct SectionProperty { page_margin: PageMargin, columns: usize, doc_grid: DocGrid, - header_reference: HeaderReference, + header_reference: Option, footer_reference: Option, section_type: Option, } @@ -42,6 +42,11 @@ impl SectionProperty { self } + pub fn header_reference(mut self, r: HeaderReference) -> Self { + self.header_reference = Some(r); + self + } + pub fn footer_reference(mut self, r: FooterReference) -> Self { self.footer_reference = Some(r); self @@ -55,7 +60,7 @@ impl Default for SectionProperty { page_margin: PageMargin::new(), columns: 425, doc_grid: DocGrid::default(), - header_reference: HeaderReference::default(), + header_reference: None, footer_reference: None, section_type: None, } @@ -69,9 +74,9 @@ impl BuildXML for SectionProperty { .open_section_property() .add_child(&self.page_size) .add_child(&self.page_margin) - .add_child(&self.header_reference) .columns(&format!("{}", &self.columns)) .add_child(&self.doc_grid) + .add_optional_child(&self.header_reference) .add_optional_child(&self.footer_reference); if let Some(t) = self.section_type { @@ -95,7 +100,7 @@ mod tests { let b = c.build(); assert_eq!( str::from_utf8(&b).unwrap(), - r#""# + r#""# ); } @@ -105,7 +110,7 @@ mod tests { let b = c.build(); assert_eq!( str::from_utf8(&b).unwrap(), - r#""# + r#""# ); } } diff --git a/docx-core/src/documents/header.rs b/docx-core/src/documents/header.rs index 49d00ee..546d7c3 100644 --- a/docx-core/src/documents/header.rs +++ b/docx-core/src/documents/header.rs @@ -8,6 +8,7 @@ use crate::xml_builder::*; #[derive(Debug, Clone, PartialEq, Serialize)] #[serde(rename_all = "camelCase")] pub struct Header { + pub has_numbering: bool, pub children: Vec, } @@ -17,19 +18,17 @@ impl Header { } pub fn add_paragraph(mut self, p: Paragraph) -> Self { - // TODO: support numberings - // if p.has_numbering { - // self.has_numbering = true - // } + if p.has_numbering { + self.has_numbering = true + } self.children.push(HeaderChild::Paragraph(p)); self } pub fn add_table(mut self, t: Table) -> Self { - // TODO: support numberings - // if t.has_numbering { - // self.has_numbering = true - // } + if t.has_numbering { + self.has_numbering = true + } self.children.push(HeaderChild::Table(t)); self } @@ -37,7 +36,10 @@ impl Header { impl Default for Header { fn default() -> Self { - Self { children: vec![] } + Self { + children: vec![], + has_numbering: false, + } } } diff --git a/docx-core/src/documents/header_id.rs b/docx-core/src/documents/header_id.rs index 9b3d195..439a718 100644 --- a/docx-core/src/documents/header_id.rs +++ b/docx-core/src/documents/header_id.rs @@ -16,8 +16,7 @@ pub fn generate_header_id() -> usize { pub fn generate_header_id() -> usize { 123 } - +*/ pub fn create_header_rid(id: usize) -> String { - format!("rIdImage{}", id) + format!("rIdHeader{}", id) } -*/ \ No newline at end of file diff --git a/docx-core/src/documents/mod.rs b/docx-core/src/documents/mod.rs index 45dd8bb..06447a0 100644 --- a/docx-core/src/documents/mod.rs +++ b/docx-core/src/documents/mod.rs @@ -48,6 +48,7 @@ pub use font_table::*; pub use footer::*; pub use footer_id::*; pub use header::*; +pub use header_id::*; pub use numberings::*; pub use rels::*; pub use settings::*; @@ -74,7 +75,7 @@ pub struct Docx { pub settings: Settings, pub font_table: FontTable, pub media: Vec<(usize, Vec)>, - pub header: Header, + pub header: Option
, pub footer: Option