数式テキスト
Contents
[
Hide
]
この記事では、Aspose.Slides for Java を使用して、数式テキストシェイプの操作と数式の書式設定を実演します。
数式テキストの追加
分数とピタゴラスの定理を含む数式シェイプを作成します。
static void addMathText() {
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
// スライドに Math シェイプを追加します。
IAutoShape mathShape = slide.getShapes().addMathShape(0, 0, 720, 150);
// 数式段落にアクセスします。
IParagraph paragraph = mathShape.getTextFrame().getParagraphs().get_Item(0);
IPortion textPortion = paragraph.getPortions().get_Item(0);
IMathParagraph mathParagraph = ((MathPortion) textPortion).getMathParagraph();
// 単純な分数を追加します: x / y.
IMathElement fraction = new MathematicalText("x").divide("y");
mathParagraph.add(new MathBlock(fraction));
// 方程式を追加します: c² = a² + b².
IMathBlock mathBlock = new MathematicalText("c")
.setSuperscript("2")
.join("=")
.join(new MathematicalText("a").setSuperscript("2"))
.join("+")
.join(new MathematicalText("b").setSuperscript("2"));
mathParagraph.add(mathBlock);
} finally {
presentation.dispose();
}
}
数式テキストへのアクセス
スライド上で数式段落を含むシェイプを検索します。
static void accessMathText() {
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
// 数式段落を含む最初のシェイプを検索します。
IAutoShape mathShape = null;
for (IShape shape : slide.getShapes()) {
if (shape instanceof IAutoShape) {
IAutoShape autoShape = (IAutoShape) shape;
ITextFrame textFrame = autoShape.getTextFrame();
if (textFrame != null) {
boolean hasMath = false;
for (IParagraph paragraph : textFrame.getParagraphs()) {
for (IPortion portion : paragraph.getPortions()) {
if (portion instanceof MathPortion) {
hasMath = true;
break;
}
}
if (hasMath) break;
}
if (hasMath) {
mathShape = autoShape;
break;
}
}
}
}
if (mathShape != null) {
IParagraph paragraph = mathShape.getTextFrame().getParagraphs().get_Item(0);
IPortion textPortion = paragraph.getPortions().get_Item(0);
IMathParagraph mathParagraph = ((MathPortion) textPortion).getMathParagraph();
// 例: 分数を作成します(ここでは追加しません)。
IMathElement fraction = new MathematicalText("x").divide("y");
// 必要に応じて mathParagraph または fraction を使用します…
}
} finally {
presentation.dispose();
}
}
数式テキストの削除
スライドから数式シェイプを削除します。
static void removeMathText() {
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(50, 50, 100, 50);
IParagraph paragraph = mathShape.getTextFrame().getParagraphs().get_Item(0);
IPortion textPortion = paragraph.getPortions().get_Item(0);
IMathParagraph mathParagraph = ((MathPortion) textPortion).getMathParagraph();
IMathElement fraction = new MathematicalText("x").divide("y");
mathParagraph.add(new MathBlock(fraction));
// 数式シェイプを削除します。
slide.getShapes().remove(mathShape);
} finally {
presentation.dispose();
}
}
数式テキストの書式設定
数式部分のフォントプロパティを設定します。
static void formatMathText() {
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape mathShape = slide.getShapes().addMathShape(50, 50, 100, 50);
IParagraph paragraph = mathShape.getTextFrame().getParagraphs().get_Item(0);
IPortion textPortion = paragraph.getPortions().get_Item(0);
IMathParagraph mathParagraph = ((MathPortion) textPortion).getMathParagraph();
IMathElement fraction = new MathematicalText("x").divide("y");
mathParagraph.add(new MathBlock(fraction));
textPortion.getPortionFormat().setFontHeight(20);
} finally {
presentation.dispose();
}
}