在 Android 上向演示文稿添加线形状

创建普通直线

要在演示文稿的选定幻灯片上添加一条简单的普通直线,请按照以下步骤操作:

  • 创建 Presentation 类的实例。
  • 通过使用其索引获取幻灯片的引用。
  • 使用 IShapeCollection 对象提供的 addAutoShape 方法添加 Line 类型的 AutoShape。
  • 将修改后的演示文稿写入为 PPTX 文件。

在下面的示例中,我们已在演示文稿的第一张幻灯片上添加了一条线。

// 实例化表示 PPTX 文件的 PresentationEx 类
Presentation pres = new Presentation();
try {
    // 获取第一张幻灯片
    ISlide sld = pres.getSlides().get_Item(0);
    
    // 添加类型为 line 的 AutoShape
    sld.getShapes().addAutoShape(ShapeType.Line, 50, 150, 300, 0);
    
    // 将 PPTX 写入磁盘
    pres.save("LineShape.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

创建带箭头的直线

Aspose.Slides for Android via Java 还允许开发人员配置线条的某些属性,使其更具吸引力。让我们尝试配置线条的几个属性,使其看起来像箭头。请按照以下步骤操作:

  • 创建 Presentation 类的实例。
  • 通过使用其索引获取幻灯片的引用。
  • 使用 IShapeCollection 对象提供的 addAutoShape 方法添加 Line 类型的 AutoShape。
  • Line Style 设置为 Aspose.Slides for Android via Java 提供的样式之一。
  • 设置线条的宽度。
  • 将线条的 Dash Style 设置为 Aspose.Slides for Android via Java 提供的样式之一。
  • 设置线条起点的 Arrow Head StyleLength
  • 设置线条终点的 Arrow Head StyleLength
  • 将修改后的演示文稿写入为 PPTX 文件。
// 实例化表示 PPTX 文件的 PresentationEx 类
Presentation pres = new Presentation();
try {
    // 获取第一张幻灯片
    ISlide sld = pres.getSlides().get_Item(0);

    // 添加类型为 line 的 AutoShape
    IAutoShape shp = sld.getShapes().addAutoShape(ShapeType.Line, 50, 150, 300, 0);

    // 对线条应用一些格式设置
    shp.getLineFormat().setStyle(LineStyle.ThickBetweenThin);
    shp.getLineFormat().setWidth(10);

    shp.getLineFormat().setDashStyle(LineDashStyle.DashDot);

    shp.getLineFormat().setBeginArrowheadLength(LineArrowheadLength.Short);
    shp.getLineFormat().setBeginArrowheadStyle(LineArrowheadStyle.Oval);

    shp.getLineFormat().setEndArrowheadLength(LineArrowheadLength.Long);
    shp.getLineFormat().setEndArrowheadStyle(LineArrowheadStyle.Triangle);

    shp.getLineFormat().getFillFormat().setFillType(FillType.Solid);
    shp.getLineFormat().getFillFormat().getSolidFillColor().setColor(new Color(PresetColor.Maroon));

    // 将 PPTX 写入磁盘
    pres.save("LineShape.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

常见问题

我可以将普通线条转换为连接线,使其“捕捉”到形状吗?

不可以。普通线条(类型为 LineAutoShape)不会自动变为连接线。要使其捕捉到形状,请使用专用的 Connector 类型以及用于连接的 corresponding APIs

如果线条的属性是从主题继承的,且难以确定最终值,我该怎么办?

通过 ILineFormatEffectiveData/ILineFillFormatEffectiveData 接口读取 有效属性,这些接口已经考虑了继承和主题样式。

我可以锁定线条以防止编辑(移动、调整大小)吗?

可以。形状提供 lock objects 使您能够禁止编辑操作。