使用 JavaScript 管理 PowerPoint 簡報中的 SmartArt
概述
SmartArt 是由節點、節點形狀和佈局組成的 PowerPoint 圖表。使用 Aspose.Slides for Node.js via Java,您可以建立 SmartArt、從其節點讀取文字、變更其佈局、檢查隱藏節點、配置組織圖佈局,並建立圖片組織圖。
從 SmartArt 物件取得文字
SmartArt 節點可以包含一個或多個形狀。若要讀取可見文字,請遍歷SmartArt.getAllNodes,然後讀取由SmartArtShape.getTextFrame 回傳的TextFrame。
let presentation = new aspose.slides.Presentation("sample.pptx");
try {
let slide = presentation.getSlides().get_Item(0);
let shape = slide.getShapes().get_Item(0);
if (java.instanceOf(shape, "com.aspose.slides.ISmartArt")) {
let smartArt = shape;
let nodes = smartArt.getAllNodes();
for (let nodeIndex = 0; nodeIndex < nodes.size(); nodeIndex++) {
let node = nodes.get_Item(nodeIndex);
let nodeShapes = node.getShapes();
for (let shapeIndex = 0; shapeIndex < nodeShapes.size(); shapeIndex++) {
let nodeShape = nodeShapes.get_Item(shapeIndex);
if (nodeShape.getTextFrame() != null) {
console.log(nodeShape.getTextFrame().getText());
}
}
}
}
} finally {
presentation.dispose();
}
變更 SmartArt 物件的佈局類型
SmartArt 佈局控制節點的排列與連接方式。以下範例建立一個使用SmartArtLayoutType BasicBlockList 值的 SmartArt 物件,將其變更為 BasicProcess 值,並儲存簡報。
let presentation = new aspose.slides.Presentation();
try {
let smartArt = presentation.getSlides().get_Item(0).getShapes().addSmartArt(
10, 10, 400, 300, aspose.slides.SmartArtLayoutType.BasicBlockList);
smartArt.setLayout(aspose.slides.SmartArtLayoutType.BasicProcess);
presentation.save("ChangeSmartArtLayout_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
檢查 SmartArt 節點是否為隱藏
SmartArtNode.isHidden 表示該節點在 SmartArt 資料模型中是否被隱藏。即使所選佈局未將其顯示為可見的圖表元素,隱藏節點仍可能存在於結構中。
以下範例向使用SmartArtLayoutType RadialCycle 值的 SmartArt 物件新增一個節點,並檢查該節點的隱藏狀態。
let presentation = new aspose.slides.Presentation();
try {
let smartArt = presentation.getSlides().get_Item(0).getShapes().addSmartArt(
10, 10, 400, 300, aspose.slides.SmartArtLayoutType.RadialCycle);
let node = smartArt.getAllNodes().addNode();
let isHidden = node.isHidden();
if (isHidden) {
console.log("The node is hidden in the SmartArt data model.");
}
presentation.save("CheckSmartArtHiddenProperty_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
取得或設定組織圖佈局
對於使用組織圖佈局的 SmartArt 圖表,SmartArtNode.getOrganizationChartLayout 和 SmartArtNode.setOrganizationChartLayout 定義子節點在父節點下的排列方式。例如,您可以根據所選的OrganizationChartLayoutType,將子節點掛在左側、右側或兩側。
以下範例建立一個組織圖,並將第一個節點的佈局設定為OrganizationChartLayoutType LeftHanging 值。
let presentation = new aspose.slides.Presentation();
try {
let smartArt = presentation.getSlides().get_Item(0).getShapes().addSmartArt(
10, 10, 400, 300, aspose.slides.SmartArtLayoutType.OrganizationChart);
let rootNode = smartArt.getNodes().get_Item(0);
rootNode.setOrganizationChartLayout(aspose.slides.OrganizationChartLayoutType.LeftHanging);
presentation.save("OrganizationChartLayout_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
建立圖片組織圖
圖片組織圖是專為包含圖像佔位符的層級圖表設計的 SmartArt 佈局。將 SmartArt 物件加入投影片時,請使用SmartArtLayoutType PictureOrganizationChart 值。
let presentation = new aspose.slides.Presentation();
try {
let smartArt = presentation.getSlides().get_Item(0).getShapes().addSmartArt(
0, 0, 400, 400, aspose.slides.SmartArtLayoutType.PictureOrganizationChart);
presentation.save("PictureOrganizationChart_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
常見問題
SmartArt 是否支援 RTL 語言的鏡像或反轉?
是的。當所選 SmartArt 佈局支援反轉時,SmartArt.setReversed 方法會將圖表方向從左至右切換為右至左,或反向切換。
如何在保留格式的情況下將 SmartArt 複製到同一投影片或其他簡報中?
您可以透過ShapeCollection.addClone clone the SmartArt shape 或clone the whole slide 其中包含 SmartArt 的投影片。兩種方式均保留大小、位置與格式。
如何將 SmartArt 轉換為點陣圖以進行預覽或網路匯出?
Render the slide 或將整個簡報轉換為 PNG 或 JPEG。SmartArt 會作為投影片的一部份被渲染。
如果投影片上有多個 SmartArt,如何找出其中特定的 SmartArt 物件?
在 SmartArt 形狀上設定唯一的Shape.setAlternativeText 或Shape.setName 值,於BaseSlide.getShapes 中搜尋該值,然後確認匹配的形狀是SmartArt。