Formátování textu prezentace v JavaScriptu
Přehled
Tento článek ukazuje, jak formátovat text v prezentacích PowerPoint a OpenDocument pomocí Aspose.Slides pro Node.js prostřednictvím Javy. Popisuje zvýrazňování, barvy pozadí, průhlednost, mezery mezi znaky, vlastnosti písma, rotaci, mezery odstavců, chování automatického přizpůsobení, ukotvení textu, tabulátory a nastavení jazyka.
V následujících příkladech použijeme soubor s názvem “sample.pptx”, který obsahuje jediný textový rámeček na první snímku s následujícím textem:

Zvýraznění textu
Použijte metodu TextFrame.highlightText když potřebujete zvýraznit text, který odpovídá konkrétnímu vzorku v textovém rámci. Metoda aplikuje barvu zvýraznění na odpovídající úryvky textu a lze ji použít s TextSearchOptions k řízení způsobu vyhledávání, například pro shodu pouze celých slov.
Ukázkový kód níže zvýrazní všechny výskyty znaků “try” a poté zvýrazní pouze celé slovo “to”.
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const textFrame = shape.getTextFrame();
// Zvýrazněte slovo "try" v tvaru.
textFrame.highlightText("try", java.getStaticFieldValue("java.awt.Color", "LIGHT_GRAY"));
const searchOptions = new aspose.slides.TextSearchOptions();
searchOptions.setWholeWordsOnly(true);
// Zvýrazněte slovo "to" v tvaru.
textFrame.highlightText("to", java.getStaticFieldValue("java.awt.Color", "MAGENTA"), searchOptions, null);
presentation.save("highlighted_text.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Výsledek:

Zvýraznění textu pomocí regulárních výrazů
Metoda TextFrame.highlightRegex zvýrazňuje shody textu nalezené regulárním výrazem. V Node.js přes Javu je toto API exponováno na TextFrame.
Ukázkový kód níže zvýrazní všechna slova, která obsahují sedm nebo více znaků:
const Pattern = java.import("java.util.regex.Pattern");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const regex = Pattern.compile("\\b[^\\s]{7,}\\b");
// Zvýrazněte všechna slova s délkou alespoň sedmi znaků.
shape.getTextFrame().highlightRegex(regex, java.getStaticFieldValue("java.awt.Color", "YELLOW"), null);
presentation.save("highlighted_text_using_regex.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Výsledek:

Nastavení barvy pozadí textu
Použijte ParagraphFormat.getDefaultPortionFormat k nastavení výchozí barvy zvýraznění pro odstavec, nebo použijte PortionFormat.getHighlightColor pro jednotlivé textové úseky.
Následující ukázkový kód ukazuje, jak nastavit barvu pozadí pro celý odstavec:
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// Nastavte barvu zvýraznění pro celý odstavec.
paragraph.getParagraphFormat().getDefaultPortionFormat().getHighlightColor().setColor(java.getStaticFieldValue("java.awt.Color", "LIGHT_GRAY"));
presentation.save("gray_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Výsledek:

Ukázkový kód níže demonstruje, jak nastavit barvu pozadí pro textové úseky s tučným písmem:
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const portions = paragraph.getPortions();
const portionCount = portions.getCount();
for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
const portion = portions.get_Item(portionIndex);
if (portion.getPortionFormat().getEffective().getFontBold()) {
// Nastavte barvu zvýraznění pro textový úsek.
portion.getPortionFormat().getHighlightColor().setColor(java.getStaticFieldValue("java.awt.Color", "LIGHT_GRAY"));
}
}
presentation.save("gray_text_portions.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Výsledek:

Zarovnání odstavců textu
Použijte ParagraphFormat.setAlignment k nastavení zarovnání odstavce v textovém rámci. Hodnota může být centrovaná, zarovnaná doleva, doprava, do bloku apod.
Následující ukázkový kód ukazuje, jak zarovnat odstavec do středu:
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// Nastavte zarovnání odstavce na střed.
paragraph.getParagraphFormat().setAlignment(aspose.slides.TextAlignment.Center);
presentation.save("aligned_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Výsledek:

Nastavení průhlednosti textu
Průhlednost textu je řízena alfa komponentou barvy přiřazené k PortionFormat.getFillFormat. V příkladech níže je alpha = 50 hodnota alfa kanálu ARGB na stupnici 0‑255, nikoli procento průhlednosti.
Ukázkový kód níže ukazuje, jak použít průhlednost na celý odstavec:
const alpha = 50;
const transparentBlack = java.newInstanceSync("java.awt.Color", 0, 0, 0, alpha);
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const fillFormat = paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat();
// Nastavte barvu výplně textu na průhlednou barvu.
fillFormat.setFillType(java.newByte(aspose.slides.FillType.Solid));
fillFormat.getSolidFillColor().setColor(transparentBlack);
presentation.save("transparent_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Výsledek:

Následující ukázkový kód ukazuje, jak použít průhlednost na textové úseky s tučným písmem:
const alpha = 50;
const transparentBlack = java.newInstanceSync("java.awt.Color", 0, 0, 0, alpha);
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const portions = paragraph.getPortions();
const portionCount = portions.getCount();
for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
const portion = portions.get_Item(portionIndex);
if (portion.getPortionFormat().getEffective().getFontBold()) {
const fillFormat = portion.getPortionFormat().getFillFormat();
// Nastavte průhlednost textového úseku.
fillFormat.setFillType(java.newByte(aspose.slides.FillType.Solid));
fillFormat.getSolidFillColor().setColor(transparentBlack);
}
}
presentation.save("transparent_text_portions.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Výsledek:

Nastavení mezery mezi znaky textu
Použijte BasePortionFormat.setSpacing k rozšíření nebo zúžení mezery mezi znaky v textovém rámečku.
Následující JavaScriptový kód ukazuje, jak rozšířit mezeru mezi znaky v celém odstavci:
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// Poznámka: Použijte záporné hodnoty ke zmenšení mezery mezi znaky.
paragraph.getParagraphFormat().getDefaultPortionFormat().setSpacing(3); // Rozšířit mezeru mezi znaky.
presentation.save("character_spacing_in_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Výsledek:

Ukázkový kód níže ukazuje, jak rozšířit mezeru mezi znaky v textových úsecích s tučným písmem:
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const portions = paragraph.getPortions();
const portionCount = portions.getCount();
for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
const portion = portions.get_Item(portionIndex);
if (portion.getPortionFormat().getEffective().getFontBold()) {
// Poznámka: Použijte záporné hodnoty ke zmenšení mezery mezi znaky.
portion.getPortionFormat().setSpacing(3); // Rozšířit mezeru mezi znaky.
}
}
presentation.save("character_spacing_in_text_portions.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Výsledek:

Zakázat kerning pro konkrétní písma
V některých případech může text vykreslený pomocí Aspose.Slides vypadat mírně těsněji než stejný text zobrazený v PowerPointu. K tomu může dojít, protože PowerPoint může ignorovat data kerningu pro určitá písma, i když písmo obsahuje platné informace o kerningu a kerning je v nastavení PowerPointu povolen.
Aby byl výstup vykreslení blíže PowerPointu, můžete v takových případech zakázat kerning pro textové úseky používající dotčené písmo. Nastavte BasePortionFormat.setKerningMinimalSize na hodnotu podstatně větší než skutečná velikost písma:
const presentation = new aspose.slides.Presentation("presentation.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraphs = autoShape.getTextFrame().getParagraphs();
const paragraphCount = paragraphs.getCount();
const targetFont = "Roboto";
for (let paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++) {
const portions = paragraphs.get_Item(paragraphIndex).getPortions();
const portionCount = portions.getCount();
for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
const portion = portions.get_Item(portionIndex);
const portionFormat = portion.getPortionFormat();
const latinFont = portionFormat.getLatinFont();
const eastAsianFont = portionFormat.getEastAsianFont();
const complexScriptFont = portionFormat.getComplexScriptFont();
if ((latinFont !== null && latinFont.getFontName() === targetFont) ||
(eastAsianFont !== null && eastAsianFont.getFontName() === targetFont) ||
(complexScriptFont !== null && complexScriptFont.getFontName() === targetFont)) {
portionFormat.setKerningMinimalSize(100);
}
}
}
presentation.save("output.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Toto nastavení zabraňuje aplikaci kerningu na odpovídající textové úseky a může pomoci sladit vykreslování Aspose.Slides s vizuálním výstupem PowerPointu pro písma ovlivněná tímto specifickým chováním PowerPointu.
Správa vlastností písma textu
Vlastnosti písma lze nastavit na úrovni odstavce pomocí ParagraphFormat.getDefaultPortionFormat nebo na jednotlivých úsecích pomocí PortionFormat.
Následující kód nastavuje písmo a styl textu pro celý odstavec: aplikuje velikost písma, tučné, kurzívu, tečkované podtržení a písmo Times New Roman na všechny úseky v odstavci.
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const defaultPortionFormat = paragraph.getParagraphFormat().getDefaultPortionFormat();
// Nastavte vlastnosti písma pro odstavec.
defaultPortionFormat.setFontHeight(12);
defaultPortionFormat.setFontBold(java.newByte(aspose.slides.NullableBool.True));
defaultPortionFormat.setFontItalic(java.newByte(aspose.slides.NullableBool.True));
defaultPortionFormat.setFontUnderline(java.newByte(aspose.slides.TextUnderlineType.Dotted));
defaultPortionFormat.setLatinFont(new aspose.slides.FontData("Times New Roman"));
presentation.save("font_properties_for_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Výsledek:

Ukázkový kód níže aplikuje podobné vlastnosti na textové úseky s tučným písmem:
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const portions = paragraph.getPortions();
const portionCount = portions.getCount();
for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
const portion = portions.get_Item(portionIndex);
if (portion.getPortionFormat().getEffective().getFontBold()) {
const portionFormat = portion.getPortionFormat();
// Nastavte vlastnosti písma pro textový úsek.
portionFormat.setFontHeight(13);
portionFormat.setFontItalic(java.newByte(aspose.slides.NullableBool.True));
portionFormat.setFontUnderline(java.newByte(aspose.slides.TextUnderlineType.Dotted));
portionFormat.setLatinFont(new aspose.slides.FontData("Times New Roman"));
}
}
presentation.save("font_properties_for_text_portions.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Výsledek:

Nastavení rotace textu
Použijte TextFrameFormat.setTextVerticalType k nastavení předdefinované orientace textu uvnitř tvaru.
Následující ukázkový kód nastavuje orientaci textu ve tvaru na Vertical270, což otáčí text o 90 stupňů proti směru hodinových ručiček:
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setTextVerticalType(java.newByte(aspose.slides.TextVerticalType.Vertical270));
presentation.save("text_rotation.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Výsledek:

Nastavení vlastní rotace pro textové rámečky
Použijte TextFrameFormat.setRotationAngle k nastavení vlastního úhlu rotace pro TextFrame.
Ukázkový kód níže otáčí textový rámec o 3 stupně ve směru hodinových ručiček uvnitř tvaru:
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setRotationAngle(3);
presentation.save("custom_text_rotation.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Výsledek:

Nastavení řádkování odstavců
Aspose.Slides poskytuje ParagraphFormat.setSpaceAfter, ParagraphFormat.setSpaceBefore a ParagraphFormat.setSpaceWithin k řízení mezery odstavců. Tyto vlastnosti se používají následovně:
- Použijte kladnou hodnotu k určení řádkování jako procenta výšky řádku.
- Použijte zápornou hodnotu k určení řádkování v bodech.
Následující ukázkový kód ukazuje, jak specifikovat řádkování v odstavci:
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getParagraphFormat().setSpaceWithin(200);
presentation.save("line_spacing.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Výsledek:

Nastavení typu automatického přizpůsobení pro textové rámečky
TextFrameFormat.setAutofitType určuje, jak se text chová, když přesáhne hranice svého kontejneru. Použijte jej k řízení, zda se text zmenší, přeteče, nebo automaticky změní velikost tvaru.
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setAutofitType(java.newByte(aspose.slides.TextAutofitType.Shape));
presentation.save("autofit_type.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Nastavení ukotvení textových rámců
TextFrameFormat.setAnchoringType určuje, jak je text vertikálně umístěn uvnitř tvaru, například nahoře, uprostřed nebo dole.
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setAnchoringType(java.newByte(aspose.slides.TextAnchorType.Bottom));
presentation.save("text_anchor.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Nastavení tabulátorů textu
Použijte ParagraphFormat.setDefaultTabSize a ParagraphFormat.getTabs k nastavení tabulátorů v odstavci.
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getParagraphFormat().setDefaultTabSize(100);
paragraph.getParagraphFormat().getTabs().add(30, java.newByte(aspose.slides.TabAlignment.Left));
presentation.save("paragraph_tabs.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Výsledek:

Nastavení jazyka ověřování pravopisu
Aspose.Slides poskytuje PortionFormat.setLanguageId, který umožňuje nastavit jazyk ověřování pravopisu pro textový úsek. Jazyk ověřování určuje jazyk používaný pro kontrolu pravopisu a gramatiky v PowerPointu.
Následující ukázkový kód ukazuje, jak nastavit jazyk ověřování pravopisu pro textový úsek:
const presentation = new aspose.slides.Presentation("presentation.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
paragraph.getPortions().clear();
const font = new aspose.slides.FontData("SimSun");
const textPortion = new aspose.slides.Portion();
textPortion.getPortionFormat().setComplexScriptFont(font);
textPortion.getPortionFormat().setEastAsianFont(font);
textPortion.getPortionFormat().setLatinFont(font);
// Nastavte Id jazykové kontroly pravopisu.
textPortion.getPortionFormat().setLanguageId("zh-CN");
textPortion.setText("1.");
paragraph.getPortions().add(textPortion);
presentation.save("proofing_language.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Nastavení výchozího jazyka
Použijte LoadOptions.setDefaultTextLanguage k definování výchozího jazyka pro text vytvářený při načítání nebo vytváření prezentace.
const loadOptions = new aspose.slides.LoadOptions();
loadOptions.setDefaultTextLanguage("en-US");
const presentation = new aspose.slides.Presentation(loadOptions);
try {
const slide = presentation.getSlides().get_Item(0);
// Přidejte nový obdélníkový tvar s textem.
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 20, 20, 150, 50);
shape.getTextFrame().setText("Sample text");
// Zkontrolujte jazyk první úseky.
const portion = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
console.log(portion.getPortionFormat().getLanguageId());
} finally {
presentation.dispose();
}
Nastavení výchozího stylu textu
Pro použití výchozího formátování textu na úrovni prezentace použijte Presentation.getDefaultTextStyle.
Následující ukázkový kód ukazuje, jak nastavit výchozí tučné písmo o velikosti 14 pt pro celý text napříč snímky v nové prezentaci.
const presentation = new aspose.slides.Presentation();
try {
// Získejte formát odstavce nejvyšší úrovně.
const paragraphFormat = presentation.getDefaultTextStyle().getLevel(0);
if (paragraphFormat !== null) {
paragraphFormat.getDefaultPortionFormat().setFontHeight(14);
paragraphFormat.getDefaultPortionFormat().setFontBold(java.newByte(aspose.slides.NullableBool.True));
}
presentation.save("default_text_style.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
Extrahování textu s efektem Všechna velká písmena
V PowerPointu aplikace efektu All Caps (všechna velká písmena) způsobí, že se text na snímku zobrazuje velkými písmeny, i když byl původně zadán malými písmeny. Když takový textový úsek načtete pomocí Aspose.Slides, knihovna vrátí text přesně tak, jak byl zadán. Pro sladění se zobrazeným textem zkontrolujte TextCapType a převod vráceného řetězce na velká písmena, pokud je hodnota All.
Předpokládejme, že na první snímek souboru sample2.pptx máme následující textový rámeček.

Ukázkový kód níže ukazuje, jak extrahovat text s aplikovaným efektem All Caps:
const presentation = new aspose.slides.Presentation("sample2.pptx");
try {
const autoShape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
const textPortion = autoShape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
console.log("Original text: " + textPortion.getText());
const textFormat = textPortion.getPortionFormat().getEffective();
if (textFormat.getTextCapType() === aspose.slides.TextCapType.All) {
const text = textPortion.getText().toUpperCase();
console.log("All-Caps effect: " + text);
}
} finally {
presentation.dispose();
}
Výstup:
Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!
Často kladené otázky
Jak upravit text v tabulce na snímku?
Pro úpravu textu v tabulce na snímku použijte Table. Procházejte buňky a aktualizujte každou buňku pomocí Cell.getTextFrame a formátování odstavců pomocí Paragraph.getParagraphFormat.
Jak použít gradientní barvu na text v PowerPoint snímku?
Pro aplikaci gradientní barvy na text použijte PortionFormat.getFillFormat. Nastavte FillFormat.setFillType na FillType.Gradient a nakonfigurujte gradientové zastavení, směr a průhlednost.