Skip to main content

docx_theme/
docx_theme.rs

1//! Word document theme: the color and font palette behind Word's own
2//! "Theme Colors"/"Theme Fonts" pickers. A theme applies to a whole
3//! document (like page setup), so this example writes two small files —
4//! one per theme — rather than two pages of a single one.
5//!
6//! Run with: `cargo run -p office-toolkit --example docx_theme`
7
8use std::path::{Path, PathBuf};
9
10use office_toolkit::SaveToFile;
11use office_toolkit::prelude::*;
12use office_toolkit::word::{ColorScheme, FontScheme, Theme};
13
14fn main() -> office_toolkit::Result<()> {
15    let default_theme_document = Document::new()
16        .with_theme(Theme::office_default())
17        .with_paragraph(Paragraph::with_text("Office's built-in default theme."))
18        .with_paragraph(Paragraph::with_text(
19            "Cambria for headings, Calibri for body text, the classic Office palette.",
20        ));
21    let default_path = output_path("docx_theme_default.docx");
22    default_theme_document.save_to_file(&default_path)?;
23    println!("Wrote {}", default_path.display());
24
25    // A custom, branded theme: a dark navy/orange palette and a different
26    // heading/body font pairing.
27    let custom_colors = ColorScheme::new(
28        "1B1B1B", "FFFFFF", "0A2540", "F5F5F5", "0A2540", "E8622C", "2E86AB", "6FB98F", "F4A259",
29        "8E4585", "1155CC", "6B3FA0",
30    );
31    let custom_fonts = FontScheme::new("Georgia", "Verdana");
32    let custom_theme_document = Document::new()
33        .with_theme(Theme::new("Custom Brand", custom_colors, custom_fonts))
34        .with_paragraph(Paragraph::with_text("A custom, branded theme."))
35        .with_paragraph(Paragraph::with_text(
36            "Georgia for headings, Verdana for body text, a navy/orange palette.",
37        ));
38    let custom_path = output_path("docx_theme_custom.docx");
39    custom_theme_document.save_to_file(&custom_path)?;
40    println!("Wrote {}", custom_path.display());
41
42    Ok(())
43}
44
45/// Every example in this directory writes its output under
46/// `tests-data/output/`, resolved relative to this crate's own manifest so
47/// it works no matter what directory `cargo run` was invoked from.
48fn output_path(filename: &str) -> PathBuf {
49    Path::new(env!("CARGO_MANIFEST_DIR"))
50        .join("../../tests-data/output")
51        .join(filename)
52}