Skip to content

PDF 生成模板编辑器

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

查看 GitHub

调整已存在的测量注释 ​

对于已经存在的测量注释,可以重新设置比例尺,单位,精确度等测量相关信息,而无需重新绘制该注释。

您可以通过以下步骤,调整已存在的测量注释的属性:

  1. 准备好需要被调整的注释。
  2. 使用 annotationManager 的 selectAnnotation 方法选中注释。
  3. 使用 annotationManager 的 setAnnotationStyles 方法调整注释,传递的参数为一个对象,其中包含要修改的注释属性,例如 Precision、Scale、StrokeColor、FillColor、TextColor 和 Opacity。周长、面积、半径、弧长和角度是否显示到注释外观中,使用注释的 setMeasurementDisplayOptions 方法设置(键为 perimeter、area、radius、arcLength、angle),之后调用 annotationManager 的 updateAnnotation。要直接设置测量长度,直线使用测量管理器(documentViewer.getMeasurementManager())的 setMeasuredLength 方法,矩形使用它的 setMeasuredSize 方法,数值的单位为注释比例尺的单位。

以直线测量注释和矩形测量注释(有四个顶点的多边形注释)为例,以下是调整已存在的测量注释的示例代码:

javascript
const measurementManager = documentViewer.getMeasurementManager();
const [annotation] = annotationManager.getPageAnnotations(1); // 第一页的第一个注释。

if (annotation.Measure && annotation instanceof Annotations.LineAnnotation) {
    annotationManager.selectAnnotation(annotation);
    await annotationManager.setAnnotationStyles(annotation, {
        Precision: 1,
        Opacity: 0.5,
        StrokeColor: new Annotations.Color('#2D77FA'),
        TextColor: new Annotations.Color('#64BC38')
    });
    await measurementManager.setMeasuredLength(annotation, 3); // 单位为比例尺的单位。
}

if (annotation.Measure && annotation instanceof Annotations.PolygonAnnotation) {
    annotationManager.selectAnnotation(annotation);
    await annotationManager.setAnnotationStyles(annotation, {
        Precision: 0.01,
        StrokeColor: new Annotations.Color('#64BC38'),
        FillColor: new Annotations.Color(255, 236, 102, 0.5) // #FFEC66,不透明度 0.5。
    });
    await measurementManager.setMeasuredSize(annotation, { width: 5.45 }); // 水平边长,单位为比例尺的单位。
    annotation.setMeasurementDisplayOptions({ perimeter: true });
    await annotationManager.updateAnnotation(annotation);
}