PPTX

线条与连接符

在元素之间添加线条和连接符形状

使用 LineShape 绘制直线,使用 ConnectorShape 绘制连接形状的线条。

线条形状

绘制简单的直线:

import { Slide, LineShape } from "@office-open/pptx";

new Slide({
    children: [
        new LineShape({
            x: 1,
            y: 1, // 起始位置
            width: 5, // 长度(英寸,水平方向)
            height: 0, // 高度(0 表示水平线)
            color: "2E74B5",
            width: 2, // 线条粗细(磅)
        }),
    ],
});

垂直线

new LineShape({
    x: 3,
    y: 1,
    width: 0,
    height: 4, // 垂直长度
    color: "ED7D31",
    width: 2,
});

对角线

new LineShape({
    x: 1,
    y: 1,
    width: 4, // 水平距离
    height: 3, // 垂直距离
    color: "70AD47",
    width: 1.5,
});

线条样式选项

new LineShape({
    x: 1,
    y: 1,
    width: 6,
    height: 0,
    color: "2E74B5",
    width: 2,
    dashStyle: "dash", // "solid" | "dash" | "dashDot" | "dot" | "lgDash" | "sysDash"
    beginArrow: "triangle", // "none" | "triangle" | "open" | "stealth" | "diamond" | "oval"
    endArrow: "stealth",
});

连接符形状

用连接符线条连接两个形状:

import { Slide, Shape, ConnectorShape } from "@office-open/pptx";

new Slide({
    children: [
        new Shape({
            x: 1,
            y: 1,
            width: 3,
            height: 2,
            text: "Shape A",
        }),
        new Shape({
            x: 6,
            y: 1,
            width: 3,
            height: 2,
            text: "Shape B",
        }),
        new ConnectorShape({
            x: 4,
            y: 2,
            width: 2,
            height: 0,
            color: "2E74B5",
            width: 2,
            beginType: "none", // "none" | "triangle" | "circle" | "square"
            endType: "triangle",
        }),
    ],
});

带路由的连接符

new ConnectorShape({
    x: 2,
    y: 3,
    width: 0,
    height: 2,
    color: "ED7D31",
    width: 1.5,
    beginType: "circle",
    endType: "triangle",
    style: "straight", // "straight" | "bent" | "curved"
});

完整示例 — 流程图连接符

import {
    Presentation,
    Slide,
    Shape,
    Paragraph,
    TextRun,
    LineShape,
    Packer,
} from "@office-open/pptx";
import fs from "node:fs";

const pres = new Presentation({
    slides: [
        new Slide({
            children: [
                new Shape({
                    x: 3,
                    y: 0.5,
                    width: 4,
                    height: 1.2,
                    geometry: "roundRect",
                    fill: { type: "solid", color: "2E74B5" },
                    paragraphs: [
                        new Paragraph({
                            alignment: "center",
                            children: [
                                new TextRun({
                                    text: "Start",
                                    color: "FFFFFF",
                                    bold: true,
                                    fontSize: 18,
                                }),
                            ],
                        }),
                    ],
                }),
                new LineShape({
                    x: 5,
                    y: 1.7,
                    width: 0,
                    height: 0.8,
                    color: "2E74B5",
                    width: 2,
                    endArrow: "triangle",
                }),
                new Shape({
                    x: 3,
                    y: 2.5,
                    width: 4,
                    height: 1.2,
                    geometry: "diamond",
                    fill: { type: "solid", color: "ED7D31" },
                    paragraphs: [
                        new Paragraph({
                            alignment: "center",
                            children: [
                                new TextRun({
                                    text: "Decision?",
                                    color: "FFFFFF",
                                    bold: true,
                                    fontSize: 16,
                                }),
                            ],
                        }),
                    ],
                }),
            ],
        }),
    ],
});

const buffer = await Packer.toBuffer(pres);
fs.writeFileSync("flowchart.pptx", buffer);
Copyright © 2026