Add a new document part to a package

This example adds a Custom XML part to the main document part of a WordprocessingML package.

Open XML packages are made of parts and relationships. A document part is not just a file in the ZIP archive; it also needs a relationship from its owning package or parent part. In ooxmlsdk, typed part methods create the new part and its relationship together.

Add a Custom XML part

The example opens an existing .docx, gets the main document part, adds a Custom XML part, writes XML bytes into that part, and saves the package to memory.

#![allow(unused)]
fn main() {
pub fn add_custom_xml_part(path: &Path, xml: &[u8]) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
  let mut document = WordprocessingDocument::new_from_file(path)?;
  let main_part = document.main_document_part()?;
  let custom_xml_part = main_part.add_custom_xml_part(&mut document, "application/xml")?;

  custom_xml_part.set_data(&mut document, xml.to_vec())?;

  let mut buffer = Cursor::new(Vec::new());
  document.save(&mut buffer)?;
  Ok(buffer.into_inner())
}
}

The key calls are:

  • WordprocessingDocument::new_from_file, which opens the package.
  • main_document_part, which gets the required main document part.
  • add_custom_xml_part, which creates a child part relationship from the main document part.
  • set_data, which replaces the part payload.
  • save, which writes the updated package to a writer.

The function returns the updated package bytes so the caller can write them to disk, upload them, or reopen them for further processing.

Relationship behavior

add_custom_xml_part chooses a new relationship ID automatically. Use the corresponding *_with_id methods when you need to control the relationship ID explicitly.