Skip to content

PDF 生成模板编辑器

开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。

查看 GitHub

测量弧长 ​

弧长测量工具可用于绘制曲线和圆弧,以测量用户选择的图形的弧长。

弧长测量工具有两类:曲线测量工具与圆弧测量工具。

  • 选择曲线测量工具后,用户可用指针点击起点与终点,然后拖动中间的两个控制点,达到想要的曲度,测量出起始和结束两点间的弧长。
  • 圆弧测量工具需要用户点击三个控制点,分别是起点、中点(在弧上的点)和终点,即可绘制出一个圆弧,测量结果包括半径、弧长和角度。

curvearc

配置测量属性后,可通过以下步骤创建弧长测量注释:

  1. 将工具类型设置为测量注释创建模式。
  2. 获取对应测量工具的默认属性(如果创建的是曲线测量,则使用 Tools.ToolNames.CURVE_MEASUREMENT;如果创建的是圆弧测量,则使用 Tools.ToolNames.ARC_MEASUREMENT),在创建时将默认属性和工具的 Measure 附加到测量注释上。再用 Annotations 中对应的类创建测量注释,并使用 annotationManager 的 addAnnotation 方法添加。

创建曲线测量 ​

以下是创建曲线测量注释的示例代码:

javascript
ComPDFKitViewer(...)
  .then(instance => {
    const { documentViewer, annotationManager, Annotations, Tools } = instance.Core;

    documentViewer.addEventListener('documentLoaded', async () => {
        // 进入曲线测量注释创建模式。
        instance.UI.setToolbarGroup('toolbarGroup-Measure');
        instance.UI.setToolMode(Tools.ToolNames.CURVE_MEASUREMENT);

        // 为注释设置默认测量属性。
        const curveTool = documentViewer.getTool(Tools.ToolNames.CURVE_MEASUREMENT);
        const { StrokeColor, StrokeThickness, Opacity, Font, FontSize } = curveTool.defaults;

        const annotation = new Annotations.FreeHandAnnotation({
            PageNumber: 1,
            StrokeColor,
            StrokeThickness,
            Opacity,
            Font,
            FontSize
        });
        annotation.Measure = { ...curveTool.Measure };
        // 曲线的起点、两个控制点和终点。
        annotation.setCurveControlPoints([
            new Annotations.Point(106, 366),
            new Annotations.Point(200, 440),
            new Annotations.Point(288, 323),
            new Annotations.Point(390, 367)
        ]);

        // 创建测量注释。
        await annotationManager.addAnnotation(annotation);
        // 渲染注释。
        annotationManager.drawAnnotationsFromList([annotation]);
    });
  });

创建圆弧测量 ​

以下是创建圆弧测量注释的示例代码:

javascript
ComPDFKitViewer(...)
  .then(instance => {
    const { documentViewer, annotationManager, Annotations, Tools } = instance.Core;

    documentViewer.addEventListener('documentLoaded', async () => {
        // 进入圆弧测量注释创建模式。
        instance.UI.setToolbarGroup('toolbarGroup-Measure');
        instance.UI.setToolMode(Tools.ToolNames.ARC_MEASUREMENT);

        // 为注释设置默认测量属性。
        const arcTool = documentViewer.getTool(Tools.ToolNames.ARC_MEASUREMENT);
        const { StrokeColor, StrokeThickness, Opacity, Font, FontSize } = arcTool.defaults;

        const annotation = new Annotations.ArcAnnotation({
            PageNumber: 1,
            StrokeColor,
            StrokeThickness,
            Opacity,
            Font,
            FontSize
        });
        annotation.Measure = { ...arcTool.Measure };
        annotation.setMeasurementDisplayOptions(arcTool.getDefaultMeasurementDisplayOptions());
        // 起点、弧上一点和终点。
        annotation.setPath([
            new Annotations.Point(92, 225),
            new Annotations.Point(233, 169),
            new Annotations.Point(143, 98)
        ]);

        // 创建测量注释。
        await annotationManager.addAnnotation(annotation);
        // 渲染注释。
        annotationManager.drawAnnotationsFromList([annotation]);
    });
  });