Format Teks Presentasi di Java
Ikhtisar
Artikel ini menunjukkan cara memformat teks dalam presentasi PowerPoint dan OpenDocument menggunakan Aspose.Slides for Java. Ini mencakup warna latar belakang, transparansi, jarak karakter, properti font, rotasi, jarak paragraf, perilaku autofit, penambatan teks, hentian tab, dan pengaturan bahasa.
Kecuali dinyatakan lain, contoh menggunakan sample.pptx. Bentuk pertama pada slide pertama adalah kotak teks, dan paragraf pertamanya berisi teks yang ditunjukkan di bawah. Indeks slide dan bentuk menggunakan basis nol. Contoh yang menyorot bagian tebal menggunakan format efektif, termasuk format tebal yang diwariskan:

Untuk menemukan dan menyorot teks literal atau kecocokan ekspresi reguler, lihat Cari dan Ganti Teks.
Atur Warna Latar Belakang Teks
Gunakan IParagraphFormat.getDefaultPortionFormat untuk mengatur warna sorotan default bagi sebuah paragraf, atau gunakan IBasePortionFormat.getHighlightColor untuk bagian teks individual.
Contoh berikut mengatur sorotan abu‑abu terang sebagai default untuk paragraf pertama. Warna sorotan eksplisit pada bagian individual memiliki prioritas lebih tinggi daripada default ini:
import com.aspose.slides.*;
import java.awt.Color;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// Atur warna sorotan untuk seluruh paragraf.
paragraph.getParagraphFormat().getDefaultPortionFormat().getHighlightColor().setColor(Color.LIGHT_GRAY);
presentation.save("gray_paragraph.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Hasilnya:

Contoh kode di bawah ini menunjukkan cara mengatur warna latar belakang untuk bagian teks dengan font tebal:
import com.aspose.slides.*;
import java.awt.Color;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
for (IPortion portion : paragraph.getPortions()) {
if (portion.getPortionFormat().getEffective().getFontBold()) {
// Atur warna sorotan untuk bagian teks.
portion.getPortionFormat().getHighlightColor().setColor(Color.LIGHT_GRAY);
}
}
presentation.save("gray_text_portions.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Hasilnya:

Ratakan Paragraf Teks
Gunakan IParagraphFormat.setAlignment untuk mengatur perataan paragraf dalam sebuah bingkai teks. Nilainya dapat berupa centered, left‑aligned, right‑aligned, justified, dan sebagainya.
Contoh kode berikut menunjukkan cara meratakan paragraf ke tengah:
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// Atur perataan paragraf ke tengah.
paragraph.getParagraphFormat().setAlignment(TextAlignment.Center);
presentation.save("aligned_paragraph.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Hasilnya:

Ratakan Font dalam Baris
Gunakan IParagraphFormat.setFontAlignment untuk meratakan secara vertikal bagian teks dengan ukuran font berbeda dalam satu baris. Pengaturan ini berlaku untuk seluruh paragraf dan mengendalikan perataan dalam setiap barisnya.
Contoh mandiri berikut membuat empat kotak teks berlabel pada satu slide. Setiap paragraf berisi teks yang sama dengan ukuran 18, 36, dan 54 poin, dengan perataan font yang berbeda. Menggunakan Arial, menonaktifkan autofit dan pembungkus, serta menjaga bingkai teks cukup besar untuk satu baris.
import com.aspose.slides.*;
import java.awt.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
int[] alignments = { FontAlignment.Baseline, FontAlignment.Top, FontAlignment.Center, FontAlignment.Bottom };
String[] alignmentNames = { "Baseline", "Top", "Center", "Bottom" };
float[] fontSizes = { 18f, 36f, 54f };
for (int i = 0; i < alignments.length; i++) {
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 20 + i * 130, 660, 120);
shape.getFillFormat().setFillType(FillType.NoFill);
shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
ITextFrame textFrame = shape.getTextFrame();
textFrame.getTextFrameFormat().setAnchoringType(TextAnchorType.Top);
textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.None);
textFrame.getTextFrameFormat().setWrapText(NullableBool.False);
IParagraph label = textFrame.getParagraphs().get_Item(0);
label.setText(alignmentNames[i]);
label.getParagraphFormat().setAlignment(TextAlignment.Left);
label.getParagraphFormat().getDefaultPortionFormat().setFontHeight(14);
label.getParagraphFormat().getDefaultPortionFormat().setLatinFont(new FontData("Arial"));
label.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
label.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY);
Paragraph paragraph = new Paragraph();
paragraph.getParagraphFormat().setFontAlignment(alignments[i]);
paragraph.getParagraphFormat().setAlignment(TextAlignment.Left);
paragraph.getParagraphFormat().getDefaultPortionFormat().setLatinFont(new FontData("Arial"));
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
for (float fontSize : fontSizes) {
Portion portion = new Portion("Ag ");
portion.getPortionFormat().setFontHeight(fontSize);
paragraph.getPortions().add(portion);
}
textFrame.getParagraphs().add(paragraph);
}
presentation.save("font_alignment.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Hasilnya:

Perataan font menggunakan metrik font, sehingga tepi tampak huruf individu tidak selalu berbaris persis. Contoh ini mencakup huruf kapital dan descender untuk memperlihatkan perbedaan antara perataan baseline dan bottom. Ketersediaan font dan substitusi, karakter yang digunakan, serta perbedaan ukuran font memengaruhi hasil. Dimensi bingkai, margin, jarak baris, pembungkus, dan autofit juga memengaruhi tata letak; gunakan font dan pengaturan tata letak yang sama saat membandingkan mode.
Pengaturan ini berbeda dari IParagraphFormat.setAlignment, yang mengendalikan perataan horizontal paragraf, dan ITextFrameFormat.setAnchoringType, yang memposisikan blok teks secara vertikal di dalam bentuknya. Format superscript dan subscript melalui IBasePortionFormat.setEscapement menggeser bagian individual relatif terhadap baseline alih-alih mengatur perataan font untuk baris paragraf.
Atur Transparansi untuk Teks
Transparansi teks dikendalikan melalui komponen alfa dari warna yang diberikan kepada IBasePortionFormat.getFillFormat. Pada contoh di bawah, alpha = 50 adalah nilai saluran alfa ARGB pada skala 0–255, bukan persentase transparansi.
Contoh kode berikut menunjukkan cara menerapkan transparansi pada seluruh paragraf:
import com.aspose.slides.*;
import java.awt.Color;
int alpha = 50;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// Atur warna isi teks ke warna transparan.
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(new Color(0, 0, 0, alpha));
presentation.save("transparent_paragraph.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Hasilnya:

Contoh kode berikut menunjukkan cara menerapkan transparansi pada bagian teks dengan font tebal:
import com.aspose.slides.*;
import java.awt.Color;
int alpha = 50;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
for (IPortion portion : paragraph.getPortions()) {
if (portion.getPortionFormat().getEffective().getFontBold()) {
// Atur transparansi bagian teks.
portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(new Color(0, 0, 0, alpha));
}
}
presentation.save("transparent_text_portions.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Hasilnya:

Atur Jarak Karakter untuk Teks
Gunakan IBasePortionFormat.setSpacing untuk memperlebar atau mempersempit jarak antar karakter dalam sebuah kotak teks. Contoh menambahkan 3 poin jarak; nilai negatif mempersempit teks.
Kode Java berikut menunjukkan cara memperlebar jarak karakter dalam seluruh paragraf:
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// Catatan: Gunakan nilai negatif untuk memperkecil jarak karakter.
paragraph.getParagraphFormat().getDefaultPortionFormat().setSpacing(3); // Perluas jarak karakter.
presentation.save("character_spacing_in_paragraph.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Hasilnya:

Contoh kode di bawah ini menunjukkan cara memperlebar jarak karakter dalam bagian teks dengan font tebal:
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
for (IPortion portion : paragraph.getPortions()) {
if (portion.getPortionFormat().getEffective().getFontBold()) {
// Catatan: Gunakan nilai negatif untuk memperkecil jarak karakter.
portion.getPortionFormat().setSpacing(3); // Perluas jarak karakter.
}
}
presentation.save("character_spacing_in_text_portions.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Hasilnya:

Nonaktifkan Kerning untuk Font Tertentu
Dalam beberapa kasus, teks yang dirender oleh Aspose.Slides dapat terlihat sedikit lebih rapat dibandingkan teks yang sama di PowerPoint. Hal ini bisa terjadi karena PowerPoint mengabaikan data kerning untuk font tertentu, meskipun font tersebut memiliki informasi kerning yang valid dan kerning diaktifkan di pengaturan PowerPoint.
Untuk membuat output yang lebih mendekati PowerPoint, Anda dapat menonaktifkan kerning untuk bagian teks yang menggunakan font yang terdampak. Atur IBasePortionFormat.setKerningMinimalSize ke nilai yang lebih besar daripada ukuran font sebenarnya. Contoh ini memerlukan “presentation.pptx” dengan kotak teks sebagai bentuk pertama pada slide pertama. Ia memeriksa nama font efektif, termasuk font yang diwariskan, dan menetapkan ambang 100 poin untuk bagian yang menggunakan Roboto. Ini menonaktifkan kerning untuk bagian yang cocok dengan ukuran font di bawah 100 poin:
import com.aspose.slides.*;
Presentation presentation = new Presentation("presentation.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
String targetFont = "Roboto";
for (IParagraph paragraph : autoShape.getTextFrame().getParagraphs()) {
for (IPortion portion : paragraph.getPortions()) {
IPortionFormatEffectiveData portionFormat = portion.getPortionFormat().getEffective();
if ((portionFormat.getLatinFont() != null &&
portionFormat.getLatinFont().getFontName().equals(targetFont)) ||
(portionFormat.getEastAsianFont() != null &&
portionFormat.getEastAsianFont().getFontName().equals(targetFont)) ||
(portionFormat.getComplexScriptFont() != null &&
portionFormat.getComplexScriptFont().getFontName().equals(targetFont))) {
portion.getPortionFormat().setKerningMinimalSize(100);
}
}
}
presentation.save("output.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Untuk teks yang cocok di bawah ambang, pengaturan ini mencegah kerning dan dapat membantu menyamakan rendering Aspose.Slides dengan output visual PowerPoint bagi font yang dipengaruhi perilaku khusus PowerPoint ini.
Kelola Properti Font Teks
Properti font dapat diatur pada tingkat paragraf melalui IParagraphFormat.getDefaultPortionFormat atau pada bagian individual melalui IPortionFormat.
Contoh berikut mengatur font default paragraf pertama menjadi Times New Roman 12 poin dengan format tebal, miring, dan underline titik. Format eksplisit pada bagian individual memiliki prioritas lebih tinggi daripada default ini.
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// Atur properti font untuk paragraf.
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(12);
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontBold(NullableBool.True);
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontItalic(NullableBool.True);
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontUnderline(TextUnderlineType.Dotted);
paragraph.getParagraphFormat().getDefaultPortionFormat().setLatinFont(new FontData("Times New Roman"));
presentation.save("font_properties_for_paragraph.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Hasilnya:

Contoh berikut menerapkan Times New Roman 13 poin, format miring, dan underline titik pada bagian yang format efektifnya tebal:
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
for (IPortion portion : paragraph.getPortions()) {
if (portion.getPortionFormat().getEffective().getFontBold()) {
// Atur properti font untuk bagian teks.
portion.getPortionFormat().setFontHeight(13);
portion.getPortionFormat().setFontItalic(NullableBool.True);
portion.getPortionFormat().setFontUnderline(TextUnderlineType.Dotted);
portion.getPortionFormat().setLatinFont(new FontData("Times New Roman"));
}
}
presentation.save("font_properties_for_text_portions.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Hasilnya:

Kontrol Normalisasi Tinggi Teks
Gunakan IBasePortionFormat.setNormaliseHeight saat mereproduksi tipografi templat atau menyelidiki perbedaan tinggi karakter. Di PowerPoint, mengaktifkan normalisasi meregangkan atau memampatkan karakter individual secara vertikal sehingga memiliki tinggi tampak yang seragam dalam bagian teks. Ini tidak menjamin tinggi teks yang sama lintas font yang berbeda.
Atur properti ke NullableBool.True atau False untuk secara eksplisit mengaktifkan atau menonaktifkan normalisasi. NullableBool.NotDefined membiarkan pengaturan lokal tidak ditetapkan sehingga bagian mewarisi nilainya, misalnya, dari getDefaultPortionFormat paragraf. Ini bukan berarti False. Gunakan IPortionFormat.getEffective dan baca IBasePortionFormatEffectiveData.getNormaliseHeight untuk memperoleh nilai Boolean yang terpecahkan.
Contoh mandiri berikut memerlukan Arial terpasang. Ia membuat dua blok teks yang berisi huruf kapital, huruf kecil, descender, dan karakter beraksen pada 54 poin. Kedua paragraf mengaktifkan normalisasi secara default. Bagian atas secara eksplisit menonaktifkannya; bagian bawah mewarisinya. Ukuran bingkai, margin, dan jarak paragraf identik, dengan pembungkus dan autofit dinonaktifkan untuk menjaga konsistensi perbandingan. Contoh mencetak False: false dan NotDefined: true, lalu menyimpan “text_height_normalization.pptx”.
import com.aspose.slides.*;
import java.awt.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
byte[] settings = { NullableBool.False, NullableBool.NotDefined };
String[] settingNames = { "False", "NotDefined" };
for (int i = 0; i < settings.length; i++) {
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 60 + i * 200, 660, 150);
shape.getFillFormat().setFillType(FillType.NoFill);
shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
ITextFrame textFrame = shape.getTextFrame();
textFrame.setText("Hxgjpq ÅÉ 0123");
textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.None);
textFrame.getTextFrameFormat().setWrapText(NullableBool.False);
textFrame.getTextFrameFormat().setAnchoringType(TextAnchorType.Top);
textFrame.getTextFrameFormat().setMarginTop(0);
textFrame.getTextFrameFormat().setMarginLeft(0);
IParagraph paragraph = textFrame.getParagraphs().get_Item(0);
paragraph.getParagraphFormat().setAlignment(TextAlignment.Left);
paragraph.getParagraphFormat().setSpaceBefore(0);
paragraph.getParagraphFormat().setSpaceAfter(0);
paragraph.getParagraphFormat().setSpaceWithin(100);
paragraph.getParagraphFormat().getDefaultPortionFormat().setNormaliseHeight(NullableBool.True);
IPortion portion = paragraph.getPortions().get_Item(0);
FontData font = new FontData("Arial");
portion.getPortionFormat().setLatinFont(font);
portion.getPortionFormat().setFontHeight(54);
portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
portion.getPortionFormat().setNormaliseHeight(settings[i]);
System.out.println(settingNames[i] + ": " + portion.getPortionFormat().getEffective().getNormaliseHeight());
}
presentation.save("text_height_normalization.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Perbandingan di bawah diekspor dari presentasi yang disimpan. Baris atas memiliki normalisasi dinonaktifkan (False); baris bawah mewarisi pengaturan aktif (NotDefined → True). Pastikan Arial terpasang untuk mereproduksi tampilan ini.

Normalisasi mengubah tinggi karakter tanpa mengubah nilai getFontHeight yang ditetapkan, yang menentukan ukuran font dalam poin. setSpaceWithin pada paragraf mengontrol jarak baris, sementara setAutofitType pada bingkai teks mengontrol cara teks atau kontainernya menyesuaikan diri. Pengaturan ini memiliki tujuan berbeda dan dapat memengaruhi tata letak secara independen. Untuk informasi lebih lanjut tentang format paragraf dan pewarisan, lihat Kelola Paragraf.
Terapkan dan Hapus Garis Coret
Gunakan IBasePortionFormat.setStrikethroughType pada bagian teks untuk menampilkan koreksi, istilah yang dibatalkan, atau harga sebelumnya tanpa menghapus teks asli. Atur ke TextStrikethroughType.Single atau Double untuk satu atau dua garis melalui teks.
Untuk menghapus garis coret, gunakan TextStrikethroughType.None. Ini secara eksplisit menonaktifkan efek, bahkan bila format di sekitarnya menentukan garis coret. Sebaliknya, TextStrikethroughType.NotDefined membiarkan pengaturan lokal tidak terdefinisi, memungkinkan bagian mewarisi format, misalnya, dari paragraf. Ini tidak menjamin garis coret dihapus. Gunakan IPortionFormat.getEffective untuk membaca format hasil, termasuk nilai yang diwariskan.
Contoh mandiri berikut membuat tiga perbandingan harga. Setiap paragraf memiliki bagian terpisah untuk label, harga sebelumnya, dan harga saat ini, sehingga hanya harga sebelumnya yang terpengaruh. Baris pertama menerapkan lalu menghapus garis coret; baris lain mempertahankan garis coret tunggal dan ganda. Mengubah pengaturan ini mempertahankan teks dan format lainnya, termasuk harga sebelumnya yang tebal.
import com.aspose.slides.*;
import java.awt.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 620, 220);
shape.getFillFormat().setFillType(FillType.NoFill);
shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
shape.getTextFrame().getParagraphs().clear();
String[] labels = { "None: ", "Single: ", "Double: " };
for (String label : labels) {
Paragraph paragraph = new Paragraph();
paragraph.getParagraphFormat().setAlignment(TextAlignment.Left);
paragraph.getParagraphFormat().setSpaceAfter(20);
IPortionFormat format = paragraph.getParagraphFormat().getDefaultPortionFormat();
format.setFontHeight(28);
FontData font = new FontData("Arial");
format.setLatinFont(font);
format.getFillFormat().setFillType(FillType.Solid);
format.getFillFormat().getSolidFillColor().setColor(Color.BLACK);
Portion labelPortion = new Portion(label);
paragraph.getPortions().add(labelPortion);
Portion previousPrice = new Portion("$100");
previousPrice.getPortionFormat().setFontBold(NullableBool.True);
paragraph.getPortions().add(previousPrice);
Portion currentPrice = new Portion(" → $80");
paragraph.getPortions().add(currentPrice);
shape.getTextFrame().getParagraphs().add(paragraph);
}
IParagraphCollection paragraphs = shape.getTextFrame().getParagraphs();
// Terapkan garis coret, lalu secara eksplisit hapus dari harga sebelumnya pertama.
paragraphs.get_Item(0).getPortions().get_Item(1).getPortionFormat().setStrikethroughType(TextStrikethroughType.Single);
paragraphs.get_Item(0).getPortions().get_Item(1).getPortionFormat().setStrikethroughType(TextStrikethroughType.None);
paragraphs.get_Item(1).getPortions().get_Item(1).getPortionFormat().setStrikethroughType(TextStrikethroughType.Single);
paragraphs.get_Item(2).getPortions().get_Item(1).getPortionFormat().setStrikethroughType(TextStrikethroughType.Double);
presentation.save("strikethrough.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Hasil, dari atas ke bawah: tanpa garis coret, garis coret tunggal, dan garis coret ganda.

Atur Rotasi Teks
Gunakan ITextFrameFormat.setTextVerticalType untuk menetapkan orientasi teks pra‑definisi dalam sebuah bentuk.
Contoh kode berikut mengatur orientasi teks dalam bentuk ke TextVerticalType.Vertical270, yang memutar teks 90 derajat berlawanan arah jarum jam:
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setTextVerticalType(TextVerticalType.Vertical270);
presentation.save("text_rotation.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Hasilnya:

Atur Rotasi Kustom untuk Bingkai Teks
Gunakan ITextFrameFormat.setRotationAngle untuk menetapkan sudut rotasi kustom bagi sebuah ITextFrame.
Contoh kode di bawah ini memutar bingkai teks sebesar 3 derajat searah jarum jam dalam bentuk:
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setRotationAngle(3);
presentation.save("custom_text_rotation.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Hasilnya:

Atur Jarak Baris Paragraf
Aspose.Slides menyediakan IParagraphFormat.setSpaceAfter, IParagraphFormat.setSpaceBefore, dan IParagraphFormat.setSpaceWithin untuk mengontrol jarak paragraf. Properti‑propperti ini digunakan sebagai berikut:
- Gunakan nilai positif untuk menentukan jarak baris sebagai persentase dari tinggi baris.
- Gunakan nilai negatif untuk menentukan jarak baris dalam poin.
Contoh berikut mengatur jarak dalam paragraf pertama menjadi 200 % dari tinggi baris (jarak ganda):
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getParagraphFormat().setSpaceWithin(200);
presentation.save("line_spacing.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Hasilnya:

Kontrol Pemutusan Baris
Aturan pemutusan baris paragraf berguna dalam blok teks sempit dan presentasi yang mencampur teks Latin dan Asia Timur. Metode berikut milik IParagraphFormat, sehingga berlaku untuk seluruh paragraf:
- setLatinLineBreak mengontrol aturan pemutusan baris Latin. Pada teks campuran, mengubahnya juga dapat mengubah tempat pembungkusan teks dan tanda baca Asia Timur yang berdekatan.
- setEastAsianLineBreak mengontrol aturan pemutusan baris Asia Timur, termasuk pembatasan karakter di awal dan akhir baris.
Aturan‑aturan ini tidak menggantikan ITextFrameFormat.setWrapText, yang mengaktifkan pembungkus otomatis dalam bingkai teks. Mereka memengaruhi tata letak saat pembungkus terjadi; mereka tidak menyisipkan karakter pemutusan baris. Pemutusan baris eksplisit memaksa baris baru dalam paragraf terlepas dari lebar yang tersedia.
Contoh mandiri berikut membuat blok teks sempit yang berisi teks China dan Latin. Ia mengatur kedua opsi pemutusan baris secara eksplisit dan menyimpan “line_breaking.pptx”. Untuk bereksperimen dengan salah satu aturan, ubah nilai yang bersangkutan sambil mempertahankan pengaturan lainnya. Contoh menggunakan Arial 24 poin dan SimSun dengan lebar bingkai 160 poin serta margin horizontal bingkai teks nol. ITextFrameFormat.setAutofitType dipanggil dengan TextAutofitType.None sehingga ukuran teks dan bingkai tetap tetap.
import com.aspose.slides.*;
import java.awt.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 160, 300);
shape.getFillFormat().setFillType(FillType.NoFill);
ITextFrame textFrame = shape.getTextFrame();
textFrame.getTextFrameFormat().setWrapText(NullableBool.True);
textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.None);
textFrame.getTextFrameFormat().setMarginLeft(0);
textFrame.getTextFrameFormat().setMarginRight(0);
IParagraph paragraph = textFrame.getParagraphs().get_Item(0);
paragraph.setText("中文排版测试,PowerPoint 中文演示。");
IParagraphFormat format = paragraph.getParagraphFormat();
format.setAlignment(TextAlignment.Left);
format.getDefaultPortionFormat().setFontHeight(24);
FontData latinFont = new FontData("Arial");
format.getDefaultPortionFormat().setLatinFont(latinFont);
FontData eastAsianFont = new FontData("SimSun");
format.getDefaultPortionFormat().setEastAsianFont(eastAsianFont);
format.getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
format.getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
format.setLatinLineBreak(NullableBool.False);
format.setEastAsianLineBreak(NullableBool.True);
presentation.save("line_breaking.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Kontrol Tanda Baca Menggantung
IParagraphFormat.setHangingPunctuation memungkinkan tanda baca yang memenuhi syarat melampaui tepi kanan baris teks alih‑alih menempati baris berikutnya. Itu berlaku untuk seluruh paragraf dan berbeda dari indentasi menggantung.
Contoh mandiri berikut mengaktifkan tanda baca menggantung dalam bingkai teks seluas 100 poin dan menyimpan “hanging_punctuation.pptx”. Dengan Arial 24 poin dan margin horizontal bingkai teks nol, titik akhir akhir tetap berada setelah “sentence” dan melampaui tepi kanan teks. Atur properti ke NullableBool.False untuk membandingkan: dengan pengaturan ini, titik berada pada baris terpisah. Pembungkus diaktifkan dan autofit dinonaktifkan untuk menjaga lebar yang tersedia tetap konstan.
import com.aspose.slides.*;
import java.awt.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 100, 200);
shape.getFillFormat().setFillType(FillType.NoFill);
ITextFrame textFrame = shape.getTextFrame();
textFrame.getTextFrameFormat().setWrapText(NullableBool.True);
textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.None);
textFrame.getTextFrameFormat().setMarginLeft(0);
textFrame.getTextFrameFormat().setMarginRight(0);
IParagraph paragraph = textFrame.getParagraphs().get_Item(0);
paragraph.setText("Simple text, next sentence.");
IParagraphFormat format = paragraph.getParagraphFormat();
format.setAlignment(TextAlignment.Left);
format.getDefaultPortionFormat().setFontHeight(24);
FontData latinFont = new FontData("Arial");
format.getDefaultPortionFormat().setLatinFont(latinFont);
format.getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
format.getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
format.setHangingPunctuation(NullableBool.True);
presentation.save("hanging_punctuation.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Tidak semua tanda baca dapat menggantung. Kondisi font dan tata letak yang dijelaskan di atas juga berlaku untuk perbandingan ini: mengubah font, lebar yang tersedia, margin, atau pengaturan autofit dapat menghilangkan perbedaan yang terlihat.
Atur Tipe Autofit untuk Bingkai Teks
ITextFrameFormat.setAutofitType menentukan bagaimana teks berperilaku ketika melebihi batas wadahnya. Gunakan untuk mengontrol apakah teks menyusut, meluap, atau mengubah ukuran bentuk secara otomatis. Contoh berikut mengonfigurasi bentuk agar menyesuaikan ukuran untuk menampung teksnya dan menyimpan hasil ke “autofit_type.pptx”.
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setAutofitType(TextAutofitType.Shape);
presentation.save("autofit_type.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Untuk menghitung baris setelah pembungkus otomatis dan melihat bagaimana lebar teks atau bentuk mengubah hasil, lihat Hitung Baris yang Dirender. Jumlah baris saja tidak menunjukkan apakah teks meluap dari wadahnya.
Atur Anchor Bingkai Teks
ITextFrameFormat.setAnchoringType menentukan bagaimana teks diposisikan secara vertikal di dalam sebuah bentuk, misalnya di atas, tengah, atau bawah. Contoh berikut menambatkan teks ke bagian bawah bentuk pertama dan menyimpan hasil ke “text_anchor.pptx”.
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Bottom);
presentation.save("text_anchor.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Atur Tabulasi Teks
Gunakan IParagraphFormat.setDefaultTabSize dan IParagraphFormat.getTabs untuk mengonfigurasi hentian tab dalam sebuah paragraf. Contoh berikut menetapkan interval tab default menjadi 100 poin dan menambahkan hentian tab rata kiri pada 30 poin. Pengaturan ini memengaruhi teks yang mengandung karakter tab.
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getParagraphFormat().setDefaultTabSize(100);
paragraph.getParagraphFormat().getTabs().add(30, TabAlignment.Left);
presentation.save("paragraph_tabs.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Hasilnya:

Atur Bahasa Proofing
Aspose.Slides menyediakan IBasePortionFormat.setLanguageId, yang memungkinkan Anda mengatur bahasa proofing untuk sebuah bagian teks. Bahasa proofing menentukan bahasa yang digunakan untuk pemeriksaan ejaan dan tata bahasa di PowerPoint.
Contoh berikut memerlukan “presentation.pptx” dengan kotak teks sebagai bentuk pertama pada slide pertama dan setidaknya satu paragraf. Ia mengganti isi paragraf pertama dengan “1。”, menetapkan SimSun sebagai fontnya, dan menetapkan bahasa proofing Mandarin Sederhana (zh-CN). Hasil disimpan ke “proofing_language.pptx”:
import com.aspose.slides.*;
Presentation presentation = new Presentation("presentation.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getPortions().clear();
FontData font = new FontData("SimSun");
Portion textPortion = new Portion();
textPortion.getPortionFormat().setComplexScriptFont(font);
textPortion.getPortionFormat().setEastAsianFont(font);
textPortion.getPortionFormat().setLatinFont(font);
// Atur Id bahasa proofing.
textPortion.getPortionFormat().setLanguageId("zh-CN");
textPortion.setText("1。");
paragraph.getPortions().add(textPortion);
presentation.save("proofing_language.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Atur Bahasa Default
Gunakan LoadOptions.setDefaultTextLanguage untuk mendefinisikan bahasa default bagi teks yang dibuat saat memuat atau membuat presentasi. Contoh berikut membuat presentasi dengan bahasa teks default Bahasa Inggris AS, menambahkan kotak teks, dan mencetak en-US untuk bagian teks pertamanya.
import com.aspose.slides.*;
LoadOptions loadOptions = new LoadOptions();
loadOptions.setDefaultTextLanguage("en-US");
Presentation presentation = new Presentation(loadOptions);
try {
ISlide slide = presentation.getSlides().get_Item(0);
// Tambahkan bentuk persegi panjang baru dengan teks.
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 20, 150, 50);
shape.getTextFrame().setText("Sample text");
// Periksa bahasa bagian pertama.
IPortion portion = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
System.out.println(portion.getPortionFormat().getLanguageId());
} finally {
presentation.dispose();
}
Atur Gaya Teks Default
Untuk menerapkan format teks default pada tingkat presentasi, gunakan IPresentation.getDefaultTextStyle.
Contoh berikut menetapkan font tebal 14 poin sebagai default untuk paragraf tingkat atas dalam presentasi baru dan menyimpannya ke “default_text_style.pptx”. Teks dapat mewarisi default ini kecuali format yang lebih spesifik menimpanya.
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
// Dapatkan format paragraf tingkat atas.
IParagraphFormat paragraphFormat = presentation.getDefaultTextStyle().getLevel(0);
if (paragraphFormat != null) {
paragraphFormat.getDefaultPortionFormat().setFontHeight(14);
paragraphFormat.getDefaultPortionFormat().setFontBold(NullableBool.True);
}
presentation.save("default_text_style.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Ekstrak Teks dengan Efek All‑Caps
Di PowerPoint, menerapkan efek font All Caps membuat teks muncul dalam huruf kapital pada slide meskipun semula diketik dengan huruf kecil. Saat Anda mengambil bagian teks tersebut dengan Aspose.Slides, perpustakaan mengembalikan teks persis seperti yang dimasukkan. Untuk menyesuaikan dengan teks yang ditampilkan, periksa TextCapType dan konversi string yang dikembalikan ke huruf kapital ketika nilainya All.
Contoh ini memerlukan “sample2.pptx” dengan kotak teks sebagai bentuk pertama pada slide pertama. Bagian pertama paragraf pertamanya berisi “Hello, Aspose!” dengan efek All Caps diterapkan, seperti yang ditunjukkan di bawah.

Contoh kode di bawah ini menunjukkan cara mengekstrak teks dengan efek All Caps yang diterapkan:
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample2.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = (IAutoShape)slide.getShapes().get_Item(0);
IPortion textPortion = autoShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
System.out.println("Original text: " + textPortion.getText());
IPortionFormatEffectiveData textFormat = textPortion.getPortionFormat().getEffective();
if (textFormat.getTextCapType() == TextCapType.All) {
String text = textPortion.getText().toUpperCase();
System.out.println("All-Caps effect: " + text);
}
} finally {
presentation.dispose();
}
Output:
Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!
FAQ
Bagaimana cara memodifikasi teks dalam tabel pada slide?
Untuk memodifikasi teks dalam tabel pada slide, gunakan ITable. Iterasi sel‑sel dan perbarui masing‑masing melalui ICell.getTextFrame serta format paragraf melalui IParagraph.getParagraphFormat.
Bagaimana cara menerapkan warna gradasi pada teks di slide PowerPoint?
Untuk menerapkan warna gradasi pada teks, gunakan IBasePortionFormat.getFillFormat. Atur IFillFormat.setFillType ke FillType.Gradient dan konfigurasikan titik‑titik gradasi, arah, serta transparansi.
Fitur terkait: Format teks dalam slide.