إدارة فقرات نص PowerPoint في جافا
نظرة عامة
يمثل Aspose.Slides for Java النص كهرمية من إطارات النص والفقرات والأجزاء:
- ITextFrame يمثل حاوية النص داخل الشكل ويوفر وصولًا إلى مجموعة فقراته.
- IParagraph يمثل فقرة واحدة في إطار نص ويوفر وصولًا إلى أجزائه وتنسيق الفقرة.
- IPortion يمثل تشغيل نص داخل الفقرة. يمكن لكل جزء أن يكون له نصه وتنسيقه على مستوى الأحرف الخاص.
وبالتالي يمكن للفقرة أن تحتوي على نص بخطوط وألوان وأحجام وتنسيقات أخرى مختلفة باستخدام عدة أجزاء.
إنشاء وتنسيق الفقرات
إنشاء فقرات مع عدة أجزاء
الخطوات التالية تنشئ إطار نص يحتوي على ثلاث فقرات، كل منها يحتوي على ثلاث أجزاء:
- إنشاء مثيل من الفئة Presentation.
- الوصول إلى الشريحة ذات الصلة عبر فهرسها.
- إضافة IAutoShape مستطلة إلى الشريحة.
- الوصول إلى ITextFrame الخاص بالشكل.
- استخدام الفقرة الافتراضية وإضافة كائنين آخرين من نوع IParagraph إلى إطار النص.
- إضافة عدد كافٍ من كائنات IPortion لكل فقرة لتحتوي على ثلاث أجزاء. الفقرة الافتراضية تحتوي بالفعل على جزء فارغ واحد.
- تعيين نص كل جزء.
- تطبيق تنسيق على مستوى الأحرف عبر IPortion.getPortionFormat.
- حفظ العرض التقديمي المعدل.
يوضح مثال Java التالي تنفيذ هذه الخطوات:
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, 150, 300, 150);
ITextFrame textFrame = shape.getTextFrame();
IParagraph firstParagraph = textFrame.getParagraphs().get_Item(0);
firstParagraph.getPortions().add(new Portion());
firstParagraph.getPortions().add(new Portion());
IParagraph secondParagraph = new Paragraph();
secondParagraph.getPortions().add(new Portion());
secondParagraph.getPortions().add(new Portion());
secondParagraph.getPortions().add(new Portion());
textFrame.getParagraphs().add(secondParagraph);
IParagraph thirdParagraph = new Paragraph();
thirdParagraph.getPortions().add(new Portion());
thirdParagraph.getPortions().add(new Portion());
thirdParagraph.getPortions().add(new Portion());
textFrame.getParagraphs().add(thirdParagraph);
int paragraphCount = textFrame.getParagraphs().getCount();
for (int paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++) {
IParagraph paragraph = textFrame.getParagraphs().get_Item(paragraphIndex);
int portionCount = paragraph.getPortions().getCount();
for (int portionIndex = 0; portionIndex < portionCount; portionIndex++) {
IPortion portion = paragraph.getPortions().get_Item(portionIndex);
portion.setText("Portion " + (paragraphIndex + 1) + "." + (portionIndex + 1));
if (portionIndex == 0) {
portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.RED);
portion.getPortionFormat().setFontBold(NullableBool.True);
portion.getPortionFormat().setFontHeight(15);
} else if (portionIndex == 1) {
portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE);
portion.getPortionFormat().setFontItalic(NullableBool.True);
portion.getPortionFormat().setFontHeight(18);
}
}
}
presentation.save("paragraphs_with_portions.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
إنشاء قوائم نقطية ومرقمة
إنشاء قائمة نقطية أو مرقمة
تُسهل النقاط والترقيم قراءة العناصر ذات الصلة. في Aspose.Slides، يتم تعريف إعدادات القوائم عبر IBulletFormat.
- إنشاء مثيل من الفئة Presentation.
- الوصول إلى الشريحة ذات الصلة عبر فهرسها.
- إضافة IAutoShape إلى الشريحة المحددة.
- الوصول إلى ITextFrame الخاص بالشكل.
- إزالة الفقرة الافتراضية من إطار النص.
- إنشاء Paragraph لرمز نقطة.
- تعيين IBulletFormat.setType إلى BulletType.Symbol وتحديد حرف النقطة.
- تعيين نص الفقرة والمسافة البادئة ولون النقطة وارتفاعها.
- إضافة الفقرة إلى إطار النص.
- إنشاء فقرة ثانية وتعيين IBulletFormat.setType إلى BulletType.Numbered.
- تهيئة نمط النقطة المرقمة وإضافة الفقرة إلى إطار النص.
- حفظ العرض التقديمي.
يُظهر مثال Java التالي إنشاء نقطة رمزية ونقطة مرقمة:
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, 200, 200, 400, 200);
ITextFrame textFrame = shape.getTextFrame();
textFrame.getParagraphs().clear();
Paragraph symbolParagraph = new Paragraph();
symbolParagraph.setText("Welcome to Aspose.Slides");
symbolParagraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
symbolParagraph.getParagraphFormat().getBullet().setChar((char) 0x2022);
symbolParagraph.getParagraphFormat().setIndent(25);
symbolParagraph.getParagraphFormat().getBullet().getColor().setColorType(ColorType.RGB);
symbolParagraph.getParagraphFormat().getBullet().getColor().setColor(Color.BLACK);
symbolParagraph.getParagraphFormat().getBullet().setBulletHardColor(NullableBool.True);
symbolParagraph.getParagraphFormat().getBullet().setHeight(100);
textFrame.getParagraphs().add(symbolParagraph);
Paragraph numberedParagraph = new Paragraph();
numberedParagraph.setText("This is a numbered item");
numberedParagraph.getParagraphFormat().getBullet().setType(BulletType.Numbered);
numberedParagraph.getParagraphFormat().getBullet().setNumberedBulletStyle(NumberedBulletStyle.BulletCircleNumWDBlackPlain);
numberedParagraph.getParagraphFormat().setIndent(25);
numberedParagraph.getParagraphFormat().getBullet().getColor().setColorType(ColorType.RGB);
numberedParagraph.getParagraphFormat().getBullet().getColor().setColor(Color.BLACK);
numberedParagraph.getParagraphFormat().getBullet().setBulletHardColor(NullableBool.True);
numberedParagraph.getParagraphFormat().getBullet().setHeight(100);
textFrame.getParagraphs().add(numberedParagraph);
presentation.save("bulleted_and_numbered_list.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
استخدام نقاط صورة
تتيح لك نقاط الصورة استخدام صورة مخصصة بدلاً من رمز أو رقم.
- إنشاء مثيل من الفئة Presentation.
- الوصول إلى الشريحة ذات الصلة عبر فهرسها.
- إضافة IAutoShape والوصول إلى ITextFrame.
- إزالة الفقرة الافتراضية من إطار النص.
- تحميل صورة النقطة وإضافتها إلى مجموعة صور العرض التقديمي كـ IPPImage.
- إنشاء Paragraph وتعيين نصه.
- تعيين IBulletFormat.setType إلى BulletType.Picture.
- تعيين الصورة عبر IBulletFormat.getPicture وتعيين ارتفاع النقطة.
- إضافة الفقرة إلى إطار النص.
- حفظ العرض التقديمي المعدل.
يوضح مثال Java التالي إنشاء نقطة صورة:
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IImage bulletImage = Images.fromFile("bullets.png");
IPPImage presentationImage;
try {
presentationImage = presentation.getImages().addImage(bulletImage);
} finally {
bulletImage.dispose();
}
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
ITextFrame textFrame = shape.getTextFrame();
textFrame.getParagraphs().clear();
Paragraph paragraph = new Paragraph();
paragraph.setText("Welcome to Aspose.Slides");
paragraph.getParagraphFormat().getBullet().setType(BulletType.Picture);
paragraph.getParagraphFormat().getBullet().getPicture().setImage(presentationImage);
paragraph.getParagraphFormat().getBullet().setHeight(100);
textFrame.getParagraphs().add(paragraph);
presentation.save("picture_bullet.pptx", SaveFormat.Pptx);
presentation.save("picture_bullet.ppt", SaveFormat.Ppt);
} finally {
presentation.dispose();
}
إنشاء قائمة متعددة المستويات
تعيين IParagraphFormat.setDepth لتحديد فقرات في مستويات مختلفة من القائمة. المستوى الأعلى له عمق 0.
- إنشاء Presentation والوصول إلى شريحة.
- إضافة IAutoShape وإزالة الفقرة الافتراضية من إطار النص الخاص بها.
- إنشاء أربع فقرات وتكوين رموز النقطة الخاصة بها.
- تعيين قيم IParagraphFormat.setDepth إلى
0،1،2، و3. - إضافة الفقرات إلى إطار النص وحفظ العرض التقديمي.
يوضح مثال Java التالي إنشاء قائمة نقطية بأربع مستويات:
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, 200, 200, 400, 200);
ITextFrame textFrame = shape.getTextFrame();
textFrame.getParagraphs().clear();
IParagraph firstParagraph = new Paragraph();
firstParagraph.setText("Content");
firstParagraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
firstParagraph.getParagraphFormat().getBullet().setChar((char) 0x2022);
firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
firstParagraph.getParagraphFormat().setDepth((short) 0);
IParagraph secondParagraph = new Paragraph();
secondParagraph.setText("Second level");
secondParagraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
secondParagraph.getParagraphFormat().getBullet().setChar('-');
secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
secondParagraph.getParagraphFormat().setDepth((short) 1);
IParagraph thirdParagraph = new Paragraph();
thirdParagraph.setText("Third level");
thirdParagraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
thirdParagraph.getParagraphFormat().getBullet().setChar((char) 0x2022);
thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
thirdParagraph.getParagraphFormat().setDepth((short) 2);
IParagraph fourthParagraph = new Paragraph();
fourthParagraph.setText("Fourth level");
fourthParagraph.getParagraphFormat().getBullet().setType(BulletType.Symbol);
fourthParagraph.getParagraphFormat().getBullet().setChar('-');
fourthParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
fourthParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
fourthParagraph.getParagraphFormat().setDepth((short) 3);
textFrame.getParagraphs().add(firstParagraph);
textFrame.getParagraphs().add(secondParagraph);
textFrame.getParagraphs().add(thirdParagraph);
textFrame.getParagraphs().add(fourthParagraph);
presentation.save("multilevel_list.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
بدء عناصر القائمة المرقمة بقيم مخصصة
استخدام IBulletFormat.setNumberedBulletStartWith لتعيين الرقم الابتدائي المعروض لفقرة مرقمة.
- إنشاء Presentation وإضافة IAutoShape إلى شريحة.
- إزالة الفقرة الافتراضية من إطار النص الخاص بالشكل.
- إنشاء ثلاث فقرات مرقمة.
- تعيين IBulletFormat.setNumberedBulletStartWith إلى
2،3و7لكل فقرة على التوالي. - إضافة الفقرات إلى إطار النص وحفظ العرض التقديمي.
يوضح مثال Java التالي تعيين رقم بدء مخصص لكل فقرة:
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 200, 200, 400, 200);
ITextFrame textFrame = shape.getTextFrame();
textFrame.getParagraphs().clear();
Paragraph firstParagraph = new Paragraph();
firstParagraph.setText("Start at 2");
firstParagraph.getParagraphFormat().getBullet().setType(BulletType.Numbered);
firstParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith((short) 2);
textFrame.getParagraphs().add(firstParagraph);
Paragraph secondParagraph = new Paragraph();
secondParagraph.setText("Start at 3");
secondParagraph.getParagraphFormat().getBullet().setType(BulletType.Numbered);
secondParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith((short) 3);
textFrame.getParagraphs().add(secondParagraph);
Paragraph thirdParagraph = new Paragraph();
thirdParagraph.setText("Start at 7");
thirdParagraph.getParagraphFormat().getBullet().setType(BulletType.Numbered);
thirdParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith((short) 7);
textFrame.getParagraphs().add(thirdParagraph);
presentation.save("custom_numbered_list.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
التحكم في تخطيط الفقرة وخصائص النهاية
تعيين مسافة بادئة السطر الأول
استخدام IParagraphFormat.setIndent للتحكم في مسافة بادئة السطر الأول للفقرة. تقوم هذه الطريقة بتحريك السطر الأول فقط بالنسبة لهامش الفقرة الأيسر. القيمة الموجبة تحرك السطر الأول إلى اليمين، بينما تظل الخطوط المتبقية محاذاة إلى جسم الفقرة.
استخدام IParagraphFormat.setMarginLeft عندما تحتاج إلى تحريك الفقرة بأكملها. استخدم IParagraphFormat.setIndent عندما تحتاج إلى تحريك السطر الأول فقط.
المثال أدناه ينشئ عدة فقرات ويطبق قيم مختلفة لـ IParagraphFormat.setIndent لتوضيح كيفية تأثير مسافة بادئة السطر الأول على تخطيط الفقرة.
- إنشاء مثيل من الفئة Presentation.
- الوصول إلى الشريحة المستهدفة.
- إضافة IAutoShape مستطلة إلى الشريحة.
- الوصول إلى ITextFrame الخاص بالشكل وإزالة الفقرة الافتراضية.
- إنشاء عدة فقرات وتعيين قيم مختلفة لـ IParagraphFormat.setIndent لها.
- إضافة الفقرات إلى إطار النص.
- حفظ العرض التقديمي المعدل.
يعرض هذا الكود كيفية تعيين مسافة بادئة للفقرة:
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, 420, 220);
shape.getFillFormat().setFillType(FillType.NoFill);
shape.getLineFormat().getFillFormat().setFillType(FillType.Solid);
shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY);
ITextFrame textFrame = shape.getTextFrame();
textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.Shape);
textFrame.getParagraphs().clear();
Paragraph firstParagraph = new Paragraph();
firstParagraph.setText("No first-line indent. Wrapped lines start at the same position as the first line.");
firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
firstParagraph.getParagraphFormat().setMarginLeft(20f);
firstParagraph.getParagraphFormat().setIndent(0f);
Paragraph secondParagraph = new Paragraph();
secondParagraph.setText("First-line indent of 20 points. The first line moves to the right, while wrapped lines remain aligned to the paragraph body.");
secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
secondParagraph.getParagraphFormat().setMarginLeft(20f);
secondParagraph.getParagraphFormat().setIndent(20f);
Paragraph thirdParagraph = new Paragraph();
thirdParagraph.setText("First-line indent of 40 points. This paragraph shows a larger first-line offset to make the effect easier to see.");
thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
thirdParagraph.getParagraphFormat().setMarginLeft(20f);
thirdParagraph.getParagraphFormat().setIndent(40f);
textFrame.getParagraphs().add(firstParagraph);
textFrame.getParagraphs().add(secondParagraph);
textFrame.getParagraphs().add(thirdParagraph);
presentation.save("paragraph_indent.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
النتيجة:

تعيين مسافة بادئة معلقة
مسافة البادئة المعلقة هي تخطيط فقرة يبدأ فيه السطر الأول إلى اليسار من بقية الأسطر. في Aspose.Slides، يمكنك إنشاء هذا التأثير باستخدام IParagraphFormat.setIndent. مرر قيمة سلبية لتحريك السطر الأول إلى اليسار بالنسبة إلى جسم الفقرة.
عمليًا، يحدد IParagraphFormat.setMarginLeft الموضع الأيسر لجسم الفقرة، ويحدد IParagraphFormat.setIndent موضع السطر الأول بالنسبة لذلك الهامش. لإنشاء مسافة بادئة معلقة، مرر قيمة موجبة إلى setMarginLeft وقيمة سلبية إلى setIndent.
هذا التنسيق مفيد للمراجع، الاقتباسات، مدخلات القاموس، وغيرها من الفقرات التي يجب أن تكون الأسطر المتقّسمة محاذية تحت جسم الفقرة بدلاً من تحت الحرف الأول للسطر الأول.
- إنشاء مثيل من الفئة Presentation.
- الوصول إلى الشريحة المستهدفة.
- إضافة IAutoShape مستطلة إلى الشريحة.
- الوصول إلى ITextFrame الخاص بالشكل وإزالة الفقرة الافتراضية.
- إنشاء فقرات وتمرير قيمة موجبة إلى IParagraphFormat.setMarginLeft لكل فقرة.
- تمرير قيمة سلبية إلى IParagraphFormat.setIndent لإنشاء تأثير المسافة البادئة المعلقة.
- إضافة الفقرات إلى إطار النص.
- حفظ العرض التقديمي المعدل.
يعرض هذا الكود كيفية تعيين مسافة بادئة معلقة لفقرة:
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, 420, 220);
shape.getFillFormat().setFillType(FillType.NoFill);
shape.getLineFormat().getFillFormat().setFillType(FillType.Solid);
shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY);
ITextFrame textFrame = shape.getTextFrame();
textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.Shape);
textFrame.getParagraphs().clear();
Paragraph firstParagraph = new Paragraph();
firstParagraph.setText("A hanging indent is created by combining a positive left margin with a negative indent. The first line starts to the left, while wrapped lines align with the paragraph body.");
firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
firstParagraph.getParagraphFormat().setMarginLeft(40f);
firstParagraph.getParagraphFormat().setIndent(-20f);
Paragraph secondParagraph = new Paragraph();
secondParagraph.setText("This second example uses a deeper hanging indent so the difference between the first line and the wrapped lines is easier to compare.");
secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid);
secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
secondParagraph.getParagraphFormat().setMarginLeft(60f);
secondParagraph.getParagraphFormat().setIndent(-30f);
textFrame.getParagraphs().add(firstParagraph);
textFrame.getParagraphs().add(secondParagraph);
presentation.save("hanging_indent.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
النتيجة:

تعيين خصائص تشغيل نهاية الفقرة
IParagraph.setEndParagraphPortionFormat يتحكم في تنسيق علامة نهاية الفقرة. يوضح المثال التالي تعيين حجم الخط وخط لاتيني لعلامة النهاية للفقرة الثانية:
- تحميل Presentation والوصول إلى شريحة.
- إضافة IAutoShape وإزالة الفقرة الافتراضية.
- إنشاء فقرتين وإضافة أجزاء نصية إليهما.
- إنشاء PortionFormat لعلامة نهاية الفقرة الثانية.
- تعيين IBasePortionFormat.setFontHeight وIBasePortionFormat.setLatinFont.
- تعيين التنسيق باستخدام IParagraph.setEndParagraphPortionFormat وحفظ العرض التقديمي.
import com.aspose.slides.*;
Presentation presentation = new Presentation("Test.pptx");
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 10, 200, 250);
ITextFrame textFrame = shape.getTextFrame();
textFrame.getParagraphs().clear();
Paragraph firstParagraph = new Paragraph();
firstParagraph.getPortions().add(new Portion("Sample text"));
Paragraph secondParagraph = new Paragraph();
secondParagraph.getPortions().add(new Portion("Sample text 2"));
PortionFormat endParagraphFormat = new PortionFormat();
endParagraphFormat.setFontHeight(48);
endParagraphFormat.setLatinFont(new FontData("Times New Roman"));
secondParagraph.setEndParagraphPortionFormat(endParagraphFormat);
textFrame.getParagraphs().add(firstParagraph);
textFrame.getParagraphs().add(secondParagraph);
presentation.save("end_paragraph_format.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
عدّ الأسطر المرسومة
للقواعد المتعلقة بالفقرات والتي تؤثر على الالتفاف التلقائي وعلامات الترقيم في نهاية الأسطر، راجع التحكم في كسر السطر والتحكم في علامات الترقيم المعلقة.
استخدام IParagraph.getLinesCount لحساب عدد الأسطر التي يشغلها نص الفقرة بعد تخطيط النص، بما في ذلك الالتفاف التلقائي. هذا مفيد عند التحقق من طول النص وتخطيطه في قوالب العروض التقديمية.
الفقرة هي عنصر واحد في ITextFrame.getParagraphs، ويمكن أن تشغل عدة أسطر مرسومة. كسر السطر الصريح داخل الفقرة يجبر على سطر جديد دون إنشاء فقرة أخرى. الالتفاف التلقائي يُنشئ أسطرًا بناءً على العرض المتاح دون إدراج كسر سطر صريح في النص. لذلك لا يُعطي عدّ الفقرات أو أحرف كسر السطر عدد الأسطر المرسومة.
المثال التالي ينشئ شكل نص، يحسب عدد أسطره، يضيق الشكل، ثم يستبدل النص بسلسلة أقصر. يتم تمكين الالتفاف وتعطيل الملاءمة التلقائية بحيث يتحكم عرض الشكل في الالتفاف دون تصغير النص أو تغيير حجم الشكل تلقائيًا. أبعاد الشكل بالنقاط. أخيرًا، يضيف المثال فقرة أخرى ويجمع عدد الأسطر عبر إطار النص.
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 400, 200);
ITextFrame textFrame = shape.getTextFrame();
textFrame.getTextFrameFormat().setWrapText(NullableBool.True);
textFrame.getTextFrameFormat().setAutofitType(TextAutofitType.None);
IParagraph paragraph = textFrame.getParagraphs().get_Item(0);
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20);
paragraph.setText("This text demonstrates how automatic wrapping changes the number of rendered lines.");
System.out.println("Original width: " + paragraph.getLinesCount());
shape.setWidth(150);
System.out.println("Narrower shape: " + paragraph.getLinesCount());
paragraph.setText("Short text.");
System.out.println("Shorter text: " + paragraph.getLinesCount());
Paragraph secondParagraph = new Paragraph();
secondParagraph.setText("Another paragraph.");
secondParagraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20);
textFrame.getParagraphs().add(secondParagraph);
int totalLineCount = 0;
for (IParagraph currentParagraph : textFrame.getParagraphs()) {
totalLineCount += currentParagraph.getLinesCount();
}
System.out.println("Total lines in the text frame: " + totalLineCount);
} finally {
presentation.dispose();
}
مع هذا النص وهذه الأبعاد، يؤدي تضييق الشكل إلى زيادة عدد الأسطر، بينما استبدال النص بالسلسلة القصيرة يقلل منه. قد تختلف الأعداد الدقيقة بناءً على توفر الخط واستبداله، حجم الخط، الهوامش، الإزاحة، الالتفاف، وإعدادات الملاءمة التلقائية. استخدم الخطوط وإعدادات التخطيط المخصصة للبيئة المستهدفة عند فحص القالب.
عدد الأسطر نفسه لا يحدد ما إذا كان النص يتجاوز حاويته. الارتفاع المتاح، ارتفاعات الأسطر، تباعد الفقرات والأسطر، وسلوك الملاءمة التلقائية مهمة أيضًا؛ حتى سطر واحد يمكن أن يتجاوز العرض المتاح عندما يكون الالتفاف معطلاً.
استيراد وتصدير محتوى الفقرة
استيراد نص HTML إلى الفقرات
استخدام ParagraphCollection.addFromHtml لتحويل ترميز HTML إلى فقرات وأجزاء داخل إطار النص.
- إنشاء مثيل من الفئة Presentation.
- الوصول إلى شريحة وإضافة IAutoShape.
- الوصول إلى ITextFrame الخاص بالشكل وإزالة الفقرة الافتراضية.
- قراءة ملف HTML المصدر.
- تمرير سلسلة HTML إلى ParagraphCollection.addFromHtml.
- حفظ العرض التقديمي المعدل.
يوضح مثال Java التالي استيراد HTML إلى إطار النص:
import com.aspose.slides.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
float shapeWidth = (float) presentation.getSlideSize().getSize().getWidth() - 20;
float shapeHeight = (float) presentation.getSlideSize().getSize().getHeight() - 20;
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 10, shapeWidth, shapeHeight);
shape.getFillFormat().setFillType(FillType.NoFill);
shape.getTextFrame().getParagraphs().clear();
try {
byte[] htmlBytes = Files.readAllBytes(Paths.get("file.html"));
String html = new String(htmlBytes, StandardCharsets.UTF_8);
shape.getTextFrame().getParagraphs().addFromHtml(html);
presentation.save("html_text.pptx", SaveFormat.Pptx);
} catch (IOException exception) {
System.out.println("The HTML file could not be read: " + exception.getMessage());
}
} finally {
presentation.dispose();
}
تصدير نص الفقرة إلى HTML
استخدام ParagraphCollection.exportToHtml لتصدير نطاق مختار من الفقرات كـ HTML.
- إنشاء مثيل من الفئة Presentation وتحميل العرض التقديمي المطلوب.
- الوصول إلى الشريحة والعثور على IAutoShape التي تحتوي على النص.
- الوصول إلى ITextFrame الخاص بالشكل.
- استدعاء ParagraphCollection.exportToHtml مع فهرس الفقرة الابتدائية وعدد الفقرات التي تريد تصديرها.
- كتابة سلسلة HTML المسترجعة إلى ملف.
يوضح مثال Java التالي تصدير جميع الفقرات من أول شكل نصي:
import com.aspose.slides.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
Presentation presentation = new Presentation("ExportingHTMLText.pptx");
try {
IShape shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
if (shape instanceof IAutoShape) {
IAutoShape textShape = (IAutoShape) shape;
ITextFrame textFrame = textShape.getTextFrame();
if (textFrame != null) {
IParagraphCollection paragraphs = textFrame.getParagraphs();
String html = paragraphs.exportToHtml(0, paragraphs.getCount(), null);
try {
Files.write(Paths.get("paragraphs.html"), html.getBytes(StandardCharsets.UTF_8));
} catch (IOException exception) {
System.out.println("The HTML file could not be written: " + exception.getMessage());
}
} else {
System.out.println("The first shape does not contain a text frame.");
}
} else {
System.out.println("The first shape is not a text shape.");
}
} finally {
presentation.dispose();
}
عرض الفقرة كصورة
IParagraph.getImage يعرض فقرة فردية مباشرة ويعيد كائن IImage. احفظ النتيجة إلى ملف أو تدفق باستخدام IImage.save. لا تحتاج إلى عرض الشكل المحتوي أو قص صورة bitmap يدويًا.
IParagraph.getImage يمكن أن يُعيد null إذا لم تُعثر على الفقرة في مجموعتها الأب، أو لا تملك حدود عرض صالحة، أو لا يمكن عرضها. تحقق من النتيجة قبل حفظها وحرّر الصورة المسترجعة بعد الاستخدام.
عرض فقرة بالمقياس الافتراضي
لنفترض أن لدينا ملف عرض تقديمي باسم sample.pptx يحتوي على شريحة واحدة، حيث الشكل الأول هو مربع نص يحتوي على ثلاث فقرات.

المثال التالي يعرض الفقرة الثانية في شكل نص عادي بالمقياس الافتراضي ويحفظ الصورة المسترجعة بصيغة PNG. يضمن قسم finally تحرير الصورة بصورة صحيحة.
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
IShape shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
if (shape instanceof IAutoShape) {
IAutoShape textShape = (IAutoShape) shape;
ITextFrame textFrame = textShape.getTextFrame();
if (textFrame != null && textFrame.getParagraphs().getCount() > 1) {
IParagraph paragraph = textFrame.getParagraphs().get_Item(1);
IImage paragraphImage = paragraph.getImage();
if (paragraphImage != null) {
try {
paragraphImage.save("paragraph.png", ImageFormat.Png);
} finally {
paragraphImage.dispose();
}
} else {
System.out.println("The paragraph could not be rendered.");
}
} else {
System.out.println("The expected paragraph was not found.");
}
} else {
System.out.println("The first shape is not a text shape.");
}
} finally {
presentation.dispose();
}
النتيجة:

عرض فقرة داخل خلية جدول مع التحجيم
استخدام نسخة IParagraph.getImage التي تقبل معلمتي float scaleX و float scaleY لتحديد عوامل التحجيم الأفقي والعمودي. المثال التالي ينشئ جدولًا، يعرض الفقرة في خليةه الأولى بضعف العرض والارتفاع الافتراضيين، ويحفظ النتيجة كصورة PNG.
import com.aspose.slides.*;
float scaleX = 2f;
float scaleY = 2f;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
ITable table = slide.getShapes().addTable(50, 50, new double[] { 300 }, new double[] { 80 });
IParagraph paragraph = table.get_Item(0, 0).getTextFrame().getParagraphs().get_Item(0);
paragraph.setText("Text in a table cell");
IImage paragraphImage = paragraph.getImage(scaleX, scaleY);
if (paragraphImage != null) {
try {
paragraphImage.save("table_paragraph.png", ImageFormat.Png);
} finally {
paragraphImage.dispose();
}
} else {
System.out.println("The paragraph could not be rendered.");
}
} finally {
presentation.dispose();
}
عامل التحجيم 1 يحافظ على ذلك المحور بحجمه البكسلي الافتراضي. على سبيل المثال، 2 لكلا العاملين ينتج صورة يكون عرضها وارتفاعها حوالي ضعف الأبعاد الافتراضية، مما ينتج أربعة أضعاف عدد البكسلات. العوامل الأكبر عادةً تُنتج نصًا أكثر حدة للتكبير أو الإخراج عالي الدقة، لكنها أيضًا تزيد من استهلاك الذاكرة وحجم الملف. العوامل الأقل من 1 تُنتج صورًا أصغر مع تفاصيل أقل. استخدم عوامل متساوية للحفاظ على نسبة العرض إلى الارتفاع للفقرة؛ العوامل الأفقية والعمودية المختلفة تُمدّد الناتج بشكل مستقل.
لا يزال عرض شكل كامل باستخدام IShape.getImage مفيدًا عندما يتوجب أن يتضمن الناتج تعبئة الشكل أو حدوده أو أي سياق بصري آخر. للحصول على صورة للفقرة فقط، استخدم IParagraph.getImage.
الأسئلة الشائعة
هل يمكنني إلغاء الالتفاف داخل إطار النص تمامًا؟
نعم. قم بتعيين ITextFrameFormat.setWrapText لتعطيل الالتفاف بحيث لا تنكسر الأسطر عند حواف إطار النص.
كيف يمكنني الحصول على حدود الفقرة المحددة على الشريحة بدقة؟
استخدم IParagraph.getRect لاسترجاع مستطيل حدود الفقرة. يوفر IPortion.getRect حدود الجزء الفردي.
أين يتم التحكم في محاذاة الفقرة (يسار، يمين، وسط، أو ضبط)؟
IParagraphFormat.setAlignment هو إعداد على مستوى الفقرة ويُطبق على الفقرة بأكملها بغض النظر عن تنسيق الأجزاء الفردية. لتمحور الأجزاء ذات الأحجام الخطية المختلفة عموديًا داخل كل سطر، راجع Align Fonts Within a Line.
هل يمكنني ضبط لغة التدقيق لجزء من الفقرة؟
نعم. قم بتعيين IBasePortionFormat.setLanguageId للأجزاء الفردية، بحيث يمكن لفقرة واحدة أن تحتوي على نصوص بلغات متعددة.