图形、图案与着色
PDF Oxide 通过 ContentStreamBuilder 提供底层图形原语用于绘制路径和形状,TilingPatternBuilder 用于重复图案,渐变构建器用于线性和径向渐变,ExtGStateBuilder 用于透明度和混合模式。
绑定覆盖。
FluentPageBuilder的基元rect(x, y, w, h)、filled_rect(x, y, w, h, color)以及line(x1, y1, x2, y2)自 v0.3.38 起已在 Rust、Python、Node、C#、Go 和 WASM 中提供 — 参见 DocumentBuilder → 图形基元。本页其余ContentStreamBuilder能力(任意路径、贝塞尔曲线、色彩空间、TilingPatternBuilder、LinearGradientBuilder/RadialGradientBuilder、ExtGStateBuilder)仍仅限 Rust。其他绑定请通过DocumentBuilder链式调用简单基元,或通过Pdf::from_html_css/Pdf::from_markdown/Pdf::from_images生成 PDF,或调用 Rust CLI 以完成自定义绘制。
快速示例
Rust
use pdf_oxide::writer::{ContentStreamBuilder, LineCap, LineJoin};
let mut builder = ContentStreamBuilder::new();
builder
.save_state()
.set_stroke_color(0.0, 0.0, 1.0) // Blue stroke
.set_fill_color(0.8, 0.8, 1.0) // Light blue fill
.set_line_width(2.0)
.rect(72.0, 600.0, 200.0, 100.0)
.fill_and_stroke()
.restore_state();
ContentStreamBuilder – Drawing Primitives
ContentStreamBuilder 生成用于在页面上渲染图形的 PDF 内容流操作符。
路径操作
use pdf_oxide::writer::ContentStreamBuilder;
let mut cs = ContentStreamBuilder::new();
// Move/Line/Curve
cs.move_to(72.0, 700.0)
.line_to(200.0, 700.0)
.line_to(200.0, 600.0)
.close_path()
.stroke();
// Rectangle
cs.rect(72.0, 500.0, 150.0, 80.0)
.fill();
// Bezier curve
cs.move_to(72.0, 400.0)
.curve_to(100.0, 450.0, 200.0, 350.0, 250.0, 400.0)
.stroke();
颜色操作
// RGB colors (0.0 to 1.0)
cs.set_fill_color(1.0, 0.0, 0.0); // Red fill
cs.set_stroke_color(0.0, 0.5, 0.0); // Green stroke
// Grayscale
cs.set_fill_color_gray(0.5); // 50% gray fill
cs.set_stroke_color_gray(0.0); // Black stroke
// CMYK
cs.set_fill_color_cmyk(0.0, 1.0, 1.0, 0.0); // Red in CMYK
cs.set_stroke_color_cmyk(1.0, 0.0, 0.0, 0.0); // Cyan stroke
线条样式
use pdf_oxide::writer::{ContentStreamBuilder, LineCap, LineJoin};
let mut cs = ContentStreamBuilder::new();
cs.set_line_width(2.0)
.set_line_cap(LineCap::Round)
.set_line_join(LineJoin::Round)
.set_miter_limit(10.0)
.set_dash_pattern(vec![5.0, 3.0], 0.0); // 5pt dash, 3pt gap
LineCap variants: Butt (default), Round, Square
LineJoin variants: Miter (default), Round, Bevel
Graphics State
// Save/restore state for isolated operations
cs.save_state()
.set_fill_color(1.0, 0.0, 0.0)
.rect(100.0, 100.0, 50.0, 50.0)
.fill()
.restore_state();
// State is restored to what it was before save_state()
路径填充和描边
| Method | 描述 |
|---|---|
.stroke() |
描边路径轮廓 |
.fill() |
填充路径内部(非零缠绕) |
.fill_even_odd() |
使用奇偶规则填充 |
.fill_and_stroke() |
填充并描边 |
.fill_and_stroke_even_odd() |
使用奇偶规则填充并描边 |
.close_and_stroke() |
关闭路径然后描边 |
.close_fill_and_stroke() |
关闭、填充并描边 |
.end_path() |
结束路径但不绘制 |
裁剪
// Clip to rectangle, then draw inside
cs.save_state()
.rect(100.0, 100.0, 200.0, 200.0)
.clip()
.end_path()
// Everything drawn here is clipped to the rectangle
.set_fill_color(1.0, 0.0, 0.0)
.rect(50.0, 50.0, 300.0, 300.0) // Only visible within clip
.fill()
.restore_state();
变换
// Apply transformation matrix [a b c d e f]
cs.save_state()
.transform(1.0, 0.0, 0.0, 1.0, 100.0, 200.0) // Translate
.rect(0.0, 0.0, 50.0, 50.0)
.fill()
.restore_state();
ContentStreamOp 枚举
如需最大控制,直接构建操作:
use pdf_oxide::writer::ContentStreamOp;
let ops = vec![
ContentStreamOp::SaveState,
ContentStreamOp::SetLineWidth(2.0),
ContentStreamOp::SetStrokeColorRGB(0.0, 0.0, 1.0),
ContentStreamOp::MoveTo(72.0, 500.0),
ContentStreamOp::LineTo(300.0, 500.0),
ContentStreamOp::Stroke,
ContentStreamOp::RestoreState,
];
TilingPatternBuilder – Repeating Patterns
平铺图案在区域内重复一个小单元格。
use pdf_oxide::writer::{TilingPatternBuilder, PatternPaintType, PatternTilingType};
// Striped pattern
let (pattern_dict, content_bytes) = TilingPatternBuilder::new()
.bbox(0.0, 0.0, 10.0, 10.0)
.x_step(10.0)
.y_step(10.0)
.colored()
.tiling_type(PatternTilingType::ConstantSpacing)
.content_bytes(b"0.8 0 0 rg 0 0 5 10 re f".to_vec())
.build();
配置方法
| Method | 描述 |
|---|---|
.bbox(x, y, w, h) |
设置图案单元格的边界框 |
.x_step(step) |
单元格之间的水平间距 |
.y_step(step) |
单元格之间的垂直间距 |
.step(x, y) |
同时设置两个步长 |
.colored() |
颜色在图案内容中定义 |
.uncolored() |
使用图案时指定颜色 |
.tiling_type(type) |
设置平铺算法 |
.matrix(a, b, c, d, e, f) |
对图案应用变换 |
.content_bytes(bytes) |
设置原始内容流字节 |
.build() |
Returns (Object, Vec<u8>) |
PatternPresets
PDF Oxide 包含常用场景的预设图案:
use pdf_oxide::writer::PatternPresets;
// Access presets for common patterns like hatching, dots, etc.
LinearGradientBuilder – Linear Gradients
创建轴向(线性)渐变着色。
use pdf_oxide::writer::{LinearGradientBuilder, GradientStop};
use pdf_oxide::layout::Color;
let (shading_dict, function_dict) = LinearGradientBuilder::new()
.from(0.0, 0.0)
.to(468.0, 0.0)
.add_stop(0.0, Color { r: 1.0, g: 0.0, b: 0.0 }) // Red
.add_stop(0.5, Color { r: 1.0, g: 1.0, b: 0.0 }) // Yellow
.add_stop(1.0, Color { r: 0.0, g: 0.0, b: 1.0 }) // Blue
.extend(true)
.build();
双色快捷方式
use pdf_oxide::writer::LinearGradientBuilder;
use pdf_oxide::layout::Color;
let gradient = LinearGradientBuilder::two_color(
Color { r: 0.0, g: 0.0, b: 0.5 }, // Dark blue
Color { r: 0.5, g: 0.8, b: 1.0 }, // Light blue
);
配置方法
| Method | 描述 |
|---|---|
.from(x, y) |
渐变起点 |
.to(x, y) |
渐变终点 |
.add_stop(pos, color) |
添加颜色停止点(位置 0.0-1.0) |
.extend_start(bool) |
在起点之前扩展渐变 |
.extend_end(bool) |
在终点之后扩展渐变 |
.extend(bool) |
设置两个扩展标志 |
RadialGradientBuilder – Radial Gradients
创建圆形渐变着色。
use pdf_oxide::writer::RadialGradientBuilder;
use pdf_oxide::layout::Color;
let (shading_dict, function_dict) = RadialGradientBuilder::new()
.center(200.0, 400.0)
.radius(0.0, 150.0) // Inner radius 0, outer radius 150
.add_stop(0.0, Color { r: 1.0, g: 1.0, b: 1.0 }) // White center
.add_stop(1.0, Color { r: 0.0, g: 0.0, b: 0.5 }) // Dark blue edge
.build();
ExtGStateBuilder – 透明度 & Blend Modes
控制透明度、混合模式和其他图形状态参数。
use pdf_oxide::writer::{ExtGStateBuilder, BlendMode};
let gs_dict = ExtGStateBuilder::new()
.fill_alpha(0.5) // 50% transparent fill
.stroke_alpha(0.8) // 80% opaque stroke
.blend_mode(BlendMode::Multiply)
.build();
配置方法
| Method | 描述 |
|---|---|
.fill_alpha(a) |
填充不透明度(0.0 透明,1.0 不透明) |
.stroke_alpha(a) |
描边不透明度 |
.blend_mode(mode) |
设置混合模式 |
.line_width(w) |
覆盖线宽 |
.line_cap(cap) |
覆盖线帽样式 |
.line_join(join) |
覆盖线连接样式 |
.miter_limit(limit) |
覆盖斜接限制 |
.flatness(f) |
平坦度容差 |
.overprint_stroke(b) |
描边的叠印模式 |
.overprint_fill(b) |
填充的叠印模式 |
BlendMode 变体
Normal, Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight, SoftLight
GradientPresets
常用渐变模式的便捷方法:
use pdf_oxide::writer::GradientPresets;
// Access preset gradient configurations
高级示例
使用渐变绘制图表背景
use pdf_oxide::writer::{
ContentStreamBuilder, LinearGradientBuilder, ExtGStateBuilder, BlendMode,
};
use pdf_oxide::layout::Color;
let mut cs = ContentStreamBuilder::new();
// Draw chart area with rounded-corner appearance
cs.save_state()
.set_fill_color(0.95, 0.95, 0.97)
.rect(72.0, 200.0, 468.0, 400.0)
.fill()
.restore_state();
// Draw grid lines
cs.save_state()
.set_stroke_color(0.85, 0.85, 0.85)
.set_line_width(0.5);
for i in 0..5 {
let y = 200.0 + (i as f32 * 100.0);
cs.move_to(72.0, y).line_to(540.0, y).stroke();
}
cs.restore_state();
// Draw data bars
let values = [280.0, 350.0, 180.0, 420.0, 310.0];
let colors = [
(0.2, 0.5, 0.8),
(0.3, 0.7, 0.4),
(0.8, 0.3, 0.3),
(0.6, 0.4, 0.8),
(0.9, 0.6, 0.2),
];
for (i, (&val, &(r, g, b))) in values.iter().zip(colors.iter()).enumerate() {
let x = 100.0 + (i as f32 * 85.0);
cs.save_state()
.set_fill_color(r, g, b)
.rect(x, 200.0, 50.0, val)
.fill()
.restore_state();
}
相关页面
- DocumentBuilder 底层 API – 高级页面构建
- 表格渲染 – 表格创建 API
- 注释创建 – 向页面添加注释