قالببندی متن ارائه در جاوااسکریپت
مروری کلی
این مقاله نشان میدهد چگونه متن را در ارائههای PowerPoint و OpenDocument با استفاده از Aspose.Slides برای Node.js از طریق Java قالببندی کنید. موضوعات شامل رنگ پسزمینه، شفافیت، فاصله کاراکترها، خصوصیات قلم، چرخش، فاصلهبندی پاراگراف، رفتار Autofit، لنگر متن، ایستگاههای تب و تنظیمات زبان میشود.
در مثالهای زیر، فایلی به نام «sample.pptx» استفاده میکنیم که یک جعبه متن در اسلاید اول شامل متن زیر دارد:

برای یافتن و برجستهسازی متن دقیق یا مطابقتهای عبارت منظم، به جستجو و جایگزینی متن مراجعه کنید.
تنظیم رنگ پسزمینه متن
از ParagraphFormat.getDefaultPortionFormat برای تنظیم رنگ برجسته پیشفرض یک پاراگراف استفاده کنید یا از BasePortionFormat.getHighlightColor برای بخشهای متن جداگانه.
کد زیر نشان میدهد چگونه رنگ پسزمینه را برای تمام پاراگراف تنظیم کنید:
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// رنگ برجسته را برای تمام پاراگراف تنظیم کنید.
paragraph.getParagraphFormat().getDefaultPortionFormat().getHighlightColor().setColor(java.getStaticFieldValue("java.awt.Color", "LIGHT_GRAY"));
presentation.save("gray_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
نتیجه:

کد زیر نحوه تنظیم رنگ پسزمینه برای بخشهای متنی با قلم پررنگ را نشان میدهد:
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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()) {
// رنگ برجسته را برای بخش متن تنظیم کنید.
portion.getPortionFormat().getHighlightColor().setColor(java.getStaticFieldValue("java.awt.Color", "LIGHT_GRAY"));
}
}
presentation.save("gray_text_portions.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
نتیجه:

ترازبندی پاراگرافهای متن
از ParagraphFormat.setAlignment برای تنظیم ترازبندی پاراگراف درون یک قاب متن استفاده کنید. مقدار میتواند وسطچین، چپچین، راستچین، توجیهشده و … باشد.
کد زیر نشان میدهد چگونه پاراگراف را به مرکز ترازبندی کنید:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// تنظیم ترازبندی پاراگراف به وسط.
paragraph.getParagraphFormat().setAlignment(aspose.slides.TextAlignment.Center);
presentation.save("aligned_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
نتیجه:

تنظیم شفافیت برای متن
شفافیت متن از طریق مؤلفه آلفای رنگی که به BasePortionFormat.getFillFormat اختصاص داده میشود کنترل میشود. در مثالهای زیر، alpha = 50 مقدار آلفای ARGB در مقیاس ۰‑۲۵۵ است، نه درصد شفافیت.
کد زیر نشان میدهد چگونه شفافیت را برای کل پاراگراف اعمال کنید:
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const alpha = 50;
const transparentBlack = java.newInstanceSync("java.awt.Color", 0, 0, 0, alpha);
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const fillFormat = paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat();
// رنگ پر کردن متن را به رنگ شفاف تنظیم کنید.
fillFormat.setFillType(java.newByte(aspose.slides.FillType.Solid));
fillFormat.getSolidFillColor().setColor(transparentBlack);
presentation.save("transparent_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
نتیجه:

کد زیر نحوه اعمال شفافیت برای بخشهای متنی با قلم پررنگ را نشان میدهد:
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const alpha = 50;
const transparentBlack = java.newInstanceSync("java.awt.Color", 0, 0, 0, alpha);
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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();
// شفافت بخش متن را تنظیم کنید.
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();
}
نتیجه:

تنظیم فاصله کاراکتر برای متن
از BasePortionFormat.setSpacing برای گسترش یا جمع کردن فاصله بین کاراکترهای یک جعبه متن استفاده کنید.
کد زیر نشان میدهد چگونه فاصله کاراکترها را در کل پاراگراف افزایش دهید:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
// نکته: برای فشردهکردن فاصله کاراکتر مقادیر منفی استفاده کنید.
paragraph.getParagraphFormat().getDefaultPortionFormat().setSpacing(3); // فاصله کاراکتر را گسترش دهید.
presentation.save("character_spacing_in_paragraph.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
نتیجه:

کد زیر نحوه افزایش فاصله کاراکترها در بخشهای متنی با قلم پررنگ را نشان میدهد:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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()) {
// نکته: برای فشردهکردن فاصله کاراکتر از مقادیر منفی استفاده کنید.
portion.getPortionFormat().setSpacing(3); // فاصله کاراکتر را گسترش دهید.
}
}
presentation.save("character_spacing_in_text_portions.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
نتیجه:

غیرفعالسازی کرنینگ برای قلمهای خاص
در برخی موارد، متن رندر شده توسط Aspose.Slides ممکن است نسبت به همان متن در PowerPoint اندکی فشردهتر به نظر برسد. این میتواند به دلیل این باشد که PowerPoint دادههای کرنینگ برخی قلمها را نادیده میگیرد، حتی زمانی که قلم شامل اطلاعات کرنینگ معتبر باشد و این گزینه در تنظیمات PowerPoint فعال باشد.
برای نزدیکتر کردن خروجی رندر به PowerPoint، میتوانید کرنینگ را برای بخشهای متنی که از قلم موردنظر استفاده میکنند، غیرفعال کنید. مقدار BasePortionFormat.setKerningMinimalSize را به مقدار خیلی بزرگتر از اندازه واقعی قلم تنظیم کنید:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("presentation.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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();
}
این تنظیم مانع اعمال کرنینگ بر روی بخشهای متن مطابق میشود و میتواند رندر Aspose.Slides را با خروجی بصری PowerPoint برای قلمهای تحت تأثیر این رفتار خاص هماهنگ کند.
مدیریت خصوصیات قلم متن
خصوصیات قلم میتوانند در سطح پاراگراف از طریق ParagraphFormat.getDefaultPortionFormat یا در بخشهای جداگانه از طریق PortionFormat تنظیم شوند.
کد زیر قلم و سبک متن را برای تمام پاراگراف تنظیم میکند: اندازه قلم، پررنگ، ایتالیک، زیرخط نقطهدار و قلم Times New Roman به همه بخشها اعمال میشود.
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
const paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
const defaultPortionFormat = paragraph.getParagraphFormat().getDefaultPortionFormat();
// تنظیم خصوصیات قلم برای پاراگراف.
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();
}
نتیجه:

کد زیر خصوصیات مشابه را برای بخشهای متنی با قلم پررنگ اعمال میکند:
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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();
// تنظیم خصوصیات قلم برای بخش متن.
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();
}
نتیجه:

تنظیم چرخش متن
از TextFrameFormat.setTextVerticalType برای تنظیم جهتگیری پیشتعریفشده متن درون یک شکل استفاده کنید.
کد زیر جهتگیری متن در شکل را به Vertical270 تنظیم میکند که متن را ۹۰ درجه خلافساعت میچرخاند:
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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();
}
نتیجه:

تنظیم چرخش سفارشی برای فریمهای متنی
از TextFrameFormat.setRotationAngle برای تنظیم زاویه چرخش سفارشی برای یک TextFrame استفاده کنید.
کد زیر فریم متن را به میزان ۳ درجه ساعتگرد درون شکل میچرخاند:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.getShapes().get_Item(0);
autoShape.getTextFrame().getTextFrameFormat().setRotationAngle(3);
presentation.save("custom_text_rotation.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
نتیجه:

تنظیم فاصلهخط پاراگرافها
Aspose.Slides توابع ParagraphFormat.setSpaceAfter، ParagraphFormat.setSpaceBefore و ParagraphFormat.setSpaceWithin را برای کنترل فاصلهگذاری پاراگراف ارائه میدهد. این خصوصیات به صورت زیر استفاده میشوند:
- برای تعیین فاصلهخط بهصورت درصدی از ارتفاع خط، مقدار مثبت استفاده کنید.
- برای تعیین فاصلهخط بهصورت نقاط، مقدار منفی استفاده کنید.
کد زیر نشان میدهد چگونه فاصلهخط را درون پاراگراف مشخص کنید:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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();
}
نتیجه:

تنظیم نوع Autofit برای فریمهای متنی
TextFrameFormat.setAutofitType تعیین میکند متن هنگام عبور از مرزهای کانتینر چگونه رفتار کند. از آن برای کنترل اینکه متن کوچک شود، سرریز شود یا شکل بهصورت خودکار تغییر اندازه دهد استفاده کنید.
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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();
}
تنظیم نقطه لنگر فریمهای متنی
TextFrameFormat.setAnchoringType نحوه قرارگیری عمودی متن داخل شکل را تعریف میکند؛ به عنوان مثال در سمت بالا، میانه یا پایین.
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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();
}
تنظیم تبهای متن
از ParagraphFormat.setDefaultTabSize و ParagraphFormat.getTabs برای پیکربندی ایستگاههای تب در یک پاراگراف استفاده کنید.
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation("sample.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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();
}
نتیجه:

تنظیم زبان بازبینی
Aspose.Slides متد BasePortionFormat.setLanguageId را ارائه میدهد که به شما اجازه میدهد زبان بازبینی را برای یک بخش متنی تنظیم کنید. این زبان بازبینی تعیین میکند در PowerPoint برای بررسی املا و دستور زبان از چه زبانی استفاده شود.
کد زیر نشان میدهد چگونه زبان بازبینی را برای یک بخش متنی تنظیم کنید:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("presentation.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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);
// تنظیم شناسه زبان بازبینی.
textPortion.getPortionFormat().setLanguageId("zh-CN");
textPortion.setText("1。");
paragraph.getPortions().add(textPortion);
presentation.save("proofing_language.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
تنظیم زبان پیشفرض
از LoadOptions.setDefaultTextLanguage برای تعریف زبان پیشفرض متنهای ایجادشده هنگام بارگذاری یا ساخت یک ارائه استفاده کنید.
const aspose = { slides: require("aspose.slides.via.java") };
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);
// یک شکل مستطیلی جدید با متن اضافه کنید.
const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 20, 20, 150, 50);
shape.getTextFrame().setText("Sample text");
// زبان بخش اول را بررسی کنید.
const portion = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0);
console.log(portion.getPortionFormat().getLanguageId());
} finally {
presentation.dispose();
}
تنظیم سبک متن پیشفرض
برای اعمال قالببندی پیشفرض متن در سطح ارائه، از Presentation.getDefaultTextStyle استفاده کنید.
کد زیر نشان میدهد چگونه یک قلم پررنگ پیشفرض با اندازه ۱۴ pt برای تمام متنهای اسلایدها در یک ارائه جدید تنظیم کنید.
const aspose = { slides: require("aspose.slides.via.java") };
const java = require("java");
const presentation = new aspose.slides.Presentation();
try {
// دریافت قالب پاراگراف سطح بالا.
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();
}
استخراج متن با اثر تمام حروف بزرگ (All‑Caps)
در PowerPoint، اعمال اثر All Caps باعث میشود متن روی اسلاید بهصورت حروف بزرگ نشان داده شود حتی اگر بهصورت حروف کوچک وارد شده باشد. هنگام استخراج چنین بخشی از متن با Aspose.Slides، کتابخانه دقیقاً همان متن وارد شده را برمیگرداند. برای مطابقت با متن نمایش داده شده، TextCapType را بررسی کنید و وقتی مقدار All باشد، رشته بازگشتی را به حروف بزرگ تبدیل کنید.
فرض کنید جعبه متن زیر در اسلاید اول فایل sample2.pptx وجود دارد.

کد زیر نشان میدهد چگونه متن را با اثر All Caps استخراج کنید:
const aspose = { slides: require("aspose.slides.via.java") };
const presentation = new aspose.slides.Presentation("sample2.pptx");
try {
const slide = presentation.getSlides().get_Item(0);
const autoShape = slide.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();
}
خروجی:
Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!
سؤالات متداول
چگونه متن را در جدول یک اسلاید اصلاح کنیم؟
برای اصلاح متن در جدول یک اسلاید، از Table استفاده کنید. سلولها را پیمایش کنید و هر سلول را از طریق Cell.getTextFrame و قالببندی پاراگراف را از طریق Paragraph.getParagraphFormat بهروز کنید.
چگونه رنگ گرادیان را به متن در یک اسلاید PowerPoint اعمال کنیم؟
برای اعمال رنگ گرادیان به متن، از BasePortionFormat.getFillFormat استفاده کنید. FillFormat.setFillType را به FillType.Gradient تنظیم کنید و نقاط توقف گرادیان، جهت و شفافیت را پیکربندی کنید.