在 JavaScript 中向演示文稿添加线条形状
概述
Aspose.Slides for Node.js via Java 允许您在 PowerPoint 幻灯片上绘制线条并在 JavaScript 中对其进行格式化。以下示例使用相同的水平线来演示如何设置其位置和长度,以及如何通过笔画和箭头改变其外观。
创建普通线条
使用 addAutoShape 与 ShapeType.Line 在第一张幻灯片上绘制一条线。该线起始于距离左边缘 50 点、距离顶部 150 点的位置。宽度为 300 点且高度为零,使其水平向右延伸。
const slides = require("aspose.slides.via.java");
const presentation = new slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
slide.getShapes().addAutoShape(slides.ShapeType.Line, 50, 150, 300, 0);
presentation.save("PlainLine.pptx", slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
创建带箭头的线条
使用 getLineFormat 对线条进行样式设置,并独立设置其端点。在本例中,左端点使用一个短椭圆标记,右端点使用一个长三角形箭头指向右侧。暗红色笔画宽度为 10 点,包含两条细线之间的粗条纹,并使用重复的点划线模式。
const slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const arrow = slide.getShapes().addAutoShape(slides.ShapeType.Line, 50, 150, 300, 0);
const lineFormat = arrow.getLineFormat();
lineFormat.setStyle(java.newByte(slides.LineStyle.ThickBetweenThin));
lineFormat.setWidth(10);
lineFormat.setDashStyle(java.newByte(slides.LineDashStyle.DashDot));
lineFormat.getFillFormat().setFillType(java.newByte(slides.FillType.Solid));
lineFormat.getFillFormat().getSolidFillColor().setPresetColor(java.newByte(slides.PresetColor.Maroon));
lineFormat.setBeginArrowheadStyle(java.newByte(slides.LineArrowheadStyle.Oval));
lineFormat.setBeginArrowheadLength(java.newByte(slides.LineArrowheadLength.Short));
lineFormat.setEndArrowheadStyle(java.newByte(slides.LineArrowheadStyle.Triangle));
lineFormat.setEndArrowheadLength(java.newByte(slides.LineArrowheadLength.Long));
presentation.save("ArrowLine.pptx", slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
比较箭头宽度
setWidth 设置线条的粗细(以点为单位)。在每一端,箭头样式决定形状,长度决定沿线的大小,宽度决定横跨线条的大小。setBeginArrowheadWidth 和 setEndArrowheadWidth 均从 LineArrowheadWidth 获取预定义的相对尺寸:Narrow、Medium 或 Wide。在每一端选择要设置的可见箭头样式。使用 None 时,宽度设置在该端不产生可见效果。
本示例绘制了四条 6 点粗的线条,箭头为中等长度的三角形。仅更改箭头宽度。最后一条线将窄的起始箭头与宽的结束箭头组合在一起。
const slides = require("aspose.slides.via.java");
const java = require("java");
const presentation = new slides.Presentation();
try {
const slide = presentation.getSlides().get_Item(0);
const narrow = slides.LineArrowheadWidth.Narrow;
const medium = slides.LineArrowheadWidth.Medium;
const wide = slides.LineArrowheadWidth.Wide;
const widths = [[narrow, narrow], [medium, medium], [wide, wide], [narrow, wide]];
let y = 60;
for (const pair of widths) {
const line = slide.getShapes().addAutoShape(slides.ShapeType.Line, 50, y, 300, 0);
const format = line.getLineFormat();
format.setWidth(6);
format.setBeginArrowheadStyle(java.newByte(slides.LineArrowheadStyle.Triangle));
format.setEndArrowheadStyle(java.newByte(slides.LineArrowheadStyle.Triangle));
format.setBeginArrowheadLength(java.newByte(slides.LineArrowheadLength.Medium));
format.setEndArrowheadLength(java.newByte(slides.LineArrowheadLength.Medium));
format.setBeginArrowheadWidth(java.newByte(pair[0]));
format.setEndArrowheadWidth(java.newByte(pair[1]));
y += 50;
}
presentation.save("ArrowheadWidths.pptx", slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
从上到下,PowerPoint 显示两个窄箭头、两个中等箭头、两个宽箭头,以及一个左侧窄箭头与右侧宽箭头的组合。

常见问题
如何在形状移动时保持线条附着在形状上?
使用 Connector 并将其端点 将其端点附加到形状。普通线条在形状移动时保持在原位。
当线条继承主题设置时,如何读取 PowerPoint 显示的线条格式?
读取 有效属性。LineFormatEffectiveData 为您提供解析后的笔画设置,LineFillFormatEffectiveData 为您提供解析后的填充设置。
我能阻止用户移动或调整线条大小吗?
可以。使用 getAutoShapeLock 将其 锁定移动、调整大小或两者.