office-rs 0.1.1

A Rust library for reading and writing XML Office files
Documentation
# OFD API 设计方案


### 1. 统一的文档结构


```rust
// OFD 构建器实现统一接口
pub struct OfdBuilder {
    document: Document,
}

impl DocumentBuilder<Document> for OfdBuilder {
    fn new() -> Self {
        Self { document: Document::new() }
    }
    
    fn save<P: AsRef<Path>>(self, path: P) -> Result<()> {
        self.document.save_as_ofd(path)
    }
    
    fn build(self) -> Result<Document> {
        Ok(self.document)
    }
}

// 简化的页面构建
impl OfdBuilder {
    pub fn page<F>(mut self, builder: F) -> Self
    where F: FnOnce(PageBuilder) -> PageBuilder
    {
        let page = builder(PageBuilder::new()).build();
        self.document.add_page(page);
        self
    }
    
    pub fn metadata(mut self, title: &str, author: &str) -> Self {
        self.document.set_metadata(title, author);
        self
    }
    
    pub fn signature<F>(mut self, builder: F) -> Self
    where F: FnOnce(SignatureBuilder) -> SignatureBuilder
    {
        let signature = builder(SignatureBuilder::new()).build();
        self.document.add_signature(signature);
        self
    }
}

// 页面尺寸预设
pub struct PageSizes;

impl PageSizes {
    pub fn a4() -> (f32, f32) { (595.0, 842.0) }
    pub fn a3() -> (f32, f32) { (842.0, 1191.0) }
    pub fn letter() -> (f32, f32) { (612.0, 792.0) }
    pub fn custom(width: f32, height: f32) -> (f32, f32) { (width, height) }
}
```

### 2. 简化的页面和图形构建


```rust
// 页面构建器
pub struct PageBuilder {
    page: Page,
}

impl PageBuilder {
    pub fn new() -> Self {
        Self { page: Page::new() }
    }
    
    pub fn size(mut self, width: f32, height: f32) -> Self {
        self.page.set_size(width, height);
        self
    }
    
    pub fn text(mut self, content: &str, x: f32, y: f32) -> Self {
        self.page.add_text(content, x, y);
        self
    }
    
    pub fn formatted_text<F>(mut self, content: &str, x: f32, y: f32, formatter: F) -> Self
    where F: FnOnce(TextFormatter) -> TextFormatter
    {
        let format = formatter(TextFormatter::new()).build();
        self.page.add_formatted_text(content, x, y, format);
        self
    }
    
    pub fn image<P: AsRef<Path>>(mut self, path: P, x: f32, y: f32, width: f32, height: f32) -> Self {
        self.page.add_image(path, x, y, width, height);
        self
    }
    
    pub fn rectangle(mut self, x: f32, y: f32, width: f32, height: f32) -> Self {
        self.page.add_rectangle(x, y, width, height);
        self
    }
    
    pub fn line(mut self, x1: f32, y1: f32, x2: f32, y2: f32) -> Self {
        self.page.add_line(x1, y1, x2, y2);
        self
    }
    
    pub fn circle(mut self, x: f32, y: f32, radius: f32) -> Self {
        self.page.add_circle(x, y, radius);
        self
    }
    
    pub fn build(self) -> Page {
        self.page
    }
}

// 文本格式化器
pub struct TextFormatter {
    font_name: String,
    font_size: f32,
    color: Color,
    bold: bool,
    italic: bool,
}

impl TextFormatter {
    pub fn new() -> Self {
        Self {
            font_name: "SimSun".to_string(),
            font_size: 12.0,
            color: Color::BLACK,
            bold: false,
            italic: false,
        }
    }
    
    pub fn font(mut self, name: &str, size: f32) -> Self {
        self.font_name = name.to_string();
        self.font_size = size;
        self
    }
    
    pub fn color(mut self, color: Color) -> Self {
        self.color = color;
        self
    }
    
    pub fn bold(mut self) -> Self {
        self.bold = true;
        self
    }
    
    pub fn italic(mut self) -> Self {
        self.italic = true;
        self
    }
    
    pub fn build(self) -> TextFormat {
        TextFormat {
            font_name: self.font_name,
            font_size: self.font_size,
            color: self.color,
            bold: self.bold,
            italic: self.italic,
        }
    }
}

// 数字签名构建器
pub struct SignatureBuilder {
    signature: Signature,
}

impl SignatureBuilder {
    pub fn new() -> Self {
        Self { signature: Signature::new() }
    }
    
    pub fn signer(mut self, name: &str) -> Self {
        self.signature.set_signer(name);
        self
    }
    
    pub fn certificate<P: AsRef<Path>>(mut self, cert_path: P) -> Self {
        self.signature.load_certificate(cert_path);
        self
    }
    
    pub fn position(mut self, x: f32, y: f32, width: f32, height: f32) -> Self {
        self.signature.set_position(x, y, width, height);
        self
    }
    
    pub fn build(self) -> Signature {
        self.signature
    }
}
```

### 3. 统一的使用示例


```rust
// 简洁的OFD文档创建
fn create_ofd_document() -> Result<()> {
    OfdBuilder::new()
        .metadata("技术报告", "张三")
        
        // 封面页
        .page(|page| page
            .size(PageSizes::a4().0, PageSizes::a4().1)
            .formatted_text("技术报告", 200.0, 100.0, |f| f
                .font("SimHei", 24.0)
                .bold()
                .color(Color::BLUE)
            )
            .formatted_text("2024年度总结", 200.0, 150.0, |f| f
                .font("SimSun", 16.0)
                .color(Color::BLACK)
            )
            .text("作者:张三", 200.0, 200.0)
            .text("日期:2024年12月", 200.0, 230.0)
        )
        
        // 内容页
        .page(|page| page
            .size(PageSizes::a4().0, PageSizes::a4().1)
            .formatted_text("第一章 概述", 50.0, 50.0, |f| f
                .font("SimHei", 18.0)
                .bold()
            )
            .text("本报告详细介绍了2024年的技术发展情况...", 50.0, 100.0)
            .rectangle(50.0, 150.0, 500.0, 200.0)  // 图表框
            .text("图1:技术发展趋势", 250.0, 370.0)
            .image("chart.png", 60.0, 160.0, 480.0, 180.0)
        )
        
        // 签名页
        .page(|page| page
            .size(PageSizes::a4().0, PageSizes::a4().1)
            .text("报告审核", 50.0, 50.0)
            .line(50.0, 100.0, 550.0, 100.0)  // 分割线
        )
        .signature(|sig| sig
            .signer("审核人:李四")
            .certificate("cert.p12")
            .position(400.0, 700.0, 150.0, 80.0)
        )
        
        .save("technical_report.ofd")
}

// 简单的表单文档
fn create_form_document() -> Result<()> {
    OfdBuilder::new()
        .metadata("申请表", "系统")
        .page(|page| page
            .size(PageSizes::a4().0, PageSizes::a4().1)
            .formatted_text("申请表", 250.0, 50.0, |f| f
                .font("SimHei", 20.0)
                .bold()
            )
            .text("姓名:_________________", 50.0, 150.0)
            .text("部门:_________________", 50.0, 200.0)
            .text("申请事由:", 50.0, 250.0)
            .rectangle(50.0, 280.0, 500.0, 200.0)  // 文本框
            .text("申请人签名:", 50.0, 550.0)
            .text("日期:", 350.0, 550.0)
        )
        .save("application_form.ofd")
}
```

### 关键设计优势:


1. **统一接口**:与其他Office格式使用相同的构建器模式
2. **固定布局**:专为精确定位和打印优化的文档格式
3. **数字签名**:内置数字签名支持,确保文档安全性
4. **中文支持**:针对中文环境优化的字体和排版
5. **标准兼容**:符合OFD国家标准规范
6. **简洁易用**:减少复杂配置,专注内容创建