PHP에서 프레젠테이션 텍스트 서식 지정
개요
이 문서에서는 Java를 통해 PHP용 Aspose.Slides를 사용하여 PowerPoint 및 OpenDocument 프레젠테이션의 텍스트 서식을 지정하는 방법을 보여줍니다. 배경 색, 투명도, 문자 간격, 글꼴 속성, 회전, 단락 간격, 자동 맞춤 동작, 텍스트 고정, 탭 정지 및 언어 설정을 다룹니다.
아래 예제에서는 첫 번째 슬라이드에 단일 텍스트 상자가 포함된 “sample.pptx” 파일을 사용합니다.

리터럴 텍스트나 정규식 일치를 찾아 강조 표시하려면 텍스트 검색 및 교체를 확인하세요.
텍스트 배경 색 설정
ParagraphFormat::getDefaultPortionFormat 을 사용하여 단락의 기본 강조 색을 설정하거나, 개별 텍스트 부분에 대해서는 BasePortionFormat::getHighlightColor 을 사용합니다.
다음 코드 예제는 전체 단락에 배경 색을 설정하는 방법을 보여줍니다:
$presentation = new Presentation("sample.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
$highlightColor = java("java.awt.Color")->LIGHT_GRAY;
// 전체 단락에 대한 강조 색을 설정합니다.
$paragraph->getParagraphFormat()->getDefaultPortionFormat()->getHighlightColor()->setColor($highlightColor);
$presentation->save("gray_paragraph.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
결과:

다음 코드 예제는 굵은 글꼴이 적용된 텍스트 부분에 배경 색을 설정하는 방법을 보여줍니다:
$presentation = new Presentation("sample.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
$highlightColor = java("java.awt.Color")->LIGHT_GRAY;
$portionCount = java_values($paragraph->getPortions()->getCount());
for ($portionIndex = 0; $portionIndex < $portionCount; $portionIndex++) {
$portion = $paragraph->getPortions()->get_Item($portionIndex);
if (java_values($portion->getPortionFormat()->getEffective()->getFontBold()) === NullableBool::True) {
// 텍스트 부분에 대한 강조 색을 설정합니다.
$portion->getPortionFormat()->getHighlightColor()->setColor($highlightColor);
}
}
$presentation->save("gray_text_portions.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
결과:

텍스트 단락 정렬
ParagraphFormat::setAlignment 을 사용하여 텍스트 프레임 내 단락 정렬을 설정합니다. 값은 가운데, 왼쪽 정렬, 오른쪽 정렬, 양쪽 맞춤 등으로 지정할 수 있습니다.
다음 코드 예제는 단락을 가운데에 정렬하는 방법을 보여줍니다:
$presentation = new Presentation("sample.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
// 단락의 정렬을 가운데로 설정합니다.
$paragraph->getParagraphFormat()->setAlignment(TextAlignment::Center);
$presentation->save("aligned_paragraph.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
결과:

텍스트 투명도 설정
텍스트 투명도는 BasePortionFormat::getFillFormat 에 할당된 색상의 알파 구성 요소를 통해 제어됩니다. 아래 예제에서 alpha = 50 은 0–255 스케일의 ARGB 알파 채널 값이며, 투명도 백분율이 아닙니다.
다음 코드 예제는 전체 단락에 투명도를 적용하는 방법을 보여줍니다:
$alpha = 50;
$presentation = new Presentation("sample.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
$fillFormat = $paragraph->getParagraphFormat()->getDefaultPortionFormat()->getFillFormat();
// 텍스트의 채우기 색을 투명 색으로 설정합니다.
$fillFormat->setFillType(FillType::Solid);
$transparentColor = new Java("java.awt.Color", 0, 0, 0, $alpha);
$fillFormat->getSolidFillColor()->setColor($transparentColor);
$presentation->save("transparent_paragraph.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
결과:

다음 코드 예제는 굵은 글꼴이 적용된 텍스트 부분에 투명도를 적용하는 방법을 보여줍니다:
$alpha = 50;
$presentation = new Presentation("sample.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
$transparentColor = new Java("java.awt.Color", 0, 0, 0, $alpha);
$portionCount = java_values($paragraph->getPortions()->getCount());
for ($portionIndex = 0; $portionIndex < $portionCount; $portionIndex++) {
$portion = $paragraph->getPortions()->get_Item($portionIndex);
if (java_values($portion->getPortionFormat()->getEffective()->getFontBold()) === NullableBool::True) {
// 텍스트 부분의 투명도를 설정합니다.
$fillFormat = $portion->getPortionFormat()->getFillFormat();
$fillFormat->setFillType(FillType::Solid);
$fillFormat->getSolidFillColor()->setColor($transparentColor);
}
}
$presentation->save("transparent_text_portions.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
결과:

텍스트 문자 간격 설정
BasePortionFormat::setSpacing 을 사용하여 텍스트 상자 내 문자 간격을 늘리거나 줄일 수 있습니다.
다음 PHP 코드는 전체 단락의 문자 간격을 확장하는 방법을 보여줍니다:
$presentation = new Presentation("sample.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
// 참고: 문자 간격을 압축하려면 음수 값을 사용합니다.
$paragraph->getParagraphFormat()->getDefaultPortionFormat()->setSpacing(3); // 문자 간격을 확장합니다.
$presentation->save("character_spacing_in_paragraph.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
결과:

다음 코드 예제는 굵은 글꼴이 적용된 텍스트 부분의 문자 간격을 확장하는 방법을 보여줍니다:
$presentation = new Presentation("sample.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
$portionCount = java_values($paragraph->getPortions()->getCount());
for ($portionIndex = 0; $portionIndex < $portionCount; $portionIndex++) {
$portion = $paragraph->getPortions()->get_Item($portionIndex);
if (java_values($portion->getPortionFormat()->getEffective()->getFontBold()) === NullableBool::True) {
// 참고: 문자 간격을 압축하려면 음수 값을 사용합니다.
$portion->getPortionFormat()->setSpacing(3); // 문자 간격을 확장합니다.
}
}
$presentation->save("character_spacing_in_text_portions.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
결과:

특정 글꼴에 대한 커닝 비활성화
일부 경우 Aspose.Slides가 렌더링한 텍스트가 PowerPoint에서 표시되는 동일한 텍스트보다 약간 더 촘촘해 보일 수 있습니다. 이는 PowerPoint가 해당 글꼴에 대한 커닝 데이터를 무시할 수 있기 때문이며, 글꼴에 유효한 커닝 정보가 포함되어 있고 PowerPoint 설정에서 커닝이 활성화돼 있어도 발생합니다.
이러한 경우 렌더링 결과를 PowerPoint와 가깝게 만들려면 영향을 받는 글꼴을 사용하는 텍스트 부분에 대해 커닝을 비활성화할 수 있습니다. BasePortionFormat::setKerningMinimalSize 을 실제 글꼴 크기보다 훨씬 큰 값으로 설정하세요:
$presentation = new Presentation("presentation.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$targetFont = "Roboto";
$paragraphCount = java_values($autoShape->getTextFrame()->getParagraphs()->getCount());
for ($paragraphIndex = 0; $paragraphIndex < $paragraphCount; $paragraphIndex++) {
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item($paragraphIndex);
$portionCount = java_values($paragraph->getPortions()->getCount());
for ($portionIndex = 0; $portionIndex < $portionCount; $portionIndex++) {
$portion = $paragraph->getPortions()->get_Item($portionIndex);
$portionFormat = $portion->getPortionFormat();
$latinFont = $portionFormat->getLatinFont();
$eastAsianFont = $portionFormat->getEastAsianFont();
$complexScriptFont = $portionFormat->getComplexScriptFont();
if ((!java_is_null($latinFont) && $latinFont->getFontName() == $targetFont) ||
(!java_is_null($eastAsianFont) && $eastAsianFont->getFontName() == $targetFont) ||
(!java_is_null($complexScriptFont) && $complexScriptFont->getFontName() == $targetFont)) {
$portionFormat->setKerningMinimalSize(100);
}
}
}
$presentation->save("output.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
이 설정은 일치하는 텍스트 부분에 커닝이 적용되는 것을 방지하며, PowerPoint 고유 동작으로 인해 영향을 받는 글꼴의 시각적 출력을 Aspose.Slides 렌더링과 맞추는 데 도움이 됩니다.
텍스트 글꼴 속성 관리
글꼴 속성은 ParagraphFormat::getDefaultPortionFormat 을 통해 단락 수준에서 설정하거나, 개별 부분에 대해서는 PortionFormat 을 통해 설정할 수 있습니다.
다음 코드는 전체 단락에 대해 글꼴 및 텍스트 스타일을 설정합니다. 여기서는 글꼴 크기, 굵게, 기울임꼴, 점선 밑줄 및 Times New Roman 글꼴을 단락의 모든 부분에 적용합니다.
$presentation = new Presentation("sample.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
$defaultPortionFormat = $paragraph->getParagraphFormat()->getDefaultPortionFormat();
$font = new FontData("Times New Roman");
// 단락에 대한 글꼴 속성을 설정합니다.
$defaultPortionFormat->setFontHeight(12);
$defaultPortionFormat->setFontBold(NullableBool::True);
$defaultPortionFormat->setFontItalic(NullableBool::True);
$defaultPortionFormat->setFontUnderline(TextUnderlineType::Dotted);
$defaultPortionFormat->setLatinFont($font);
$presentation->save("font_properties_for_paragraph.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
결과:

다음 코드 예제는 굵은 글꼴이 적용된 텍스트 부분에 유사한 속성을 적용합니다:
$presentation = new Presentation("sample.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
$font = new FontData("Times New Roman");
$portionCount = java_values($paragraph->getPortions()->getCount());
for ($portionIndex = 0; $portionIndex < $portionCount; $portionIndex++) {
$portion = $paragraph->getPortions()->get_Item($portionIndex);
if (java_values($portion->getPortionFormat()->getEffective()->getFontBold()) === NullableBool::True) {
// 텍스트 부분에 대한 글꼴 속성을 설정합니다.
$portionFormat = $portion->getPortionFormat();
$portionFormat->setFontHeight(13);
$portionFormat->setFontItalic(NullableBool::True);
$portionFormat->setFontUnderline(TextUnderlineType::Dotted);
$portionFormat->setLatinFont($font);
}
}
$presentation->save("font_properties_for_text_portions.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
결과:

텍스트 회전 설정
TextFrameFormat::setTextVerticalType 을 사용하여 도형 내 텍스트의 사전 정의된 방향을 설정합니다.
다음 코드 예제는 텍스트 방향을 Vertical270 으로 설정하여 텍스트를 시계 반대 방향으로 90도 회전시킵니다:
$presentation = new Presentation("sample.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$autoShape->getTextFrame()->getTextFrameFormat()->setTextVerticalType(TextVerticalType::Vertical270);
$presentation->save("text_rotation.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
결과:

텍스트 프레임에 대한 사용자 정의 회전 설정
TextFrameFormat::setRotationAngle 을 사용하여 TextFrame 에 대한 사용자 정의 회전 각도를 설정합니다.
다음 코드 예제는 도형 내 텍스트 프레임을 시계 방향으로 3도 회전시킵니다:
$presentation = new Presentation("sample.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$autoShape->getTextFrame()->getTextFrameFormat()->setRotationAngle(3);
$presentation->save("custom_text_rotation.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
결과:

단락의 행 간격 설정
Aspose.Slides는 ParagraphFormat::setSpaceAfter, ParagraphFormat::setSpaceBefore 및 ParagraphFormat::setSpaceWithin 을 제공하여 단락 간격을 제어합니다. 이러한 속성은 다음과 같이 사용할 수 있습니다:
- 양수 값을 사용하면 행 간격을 행 높이의 백분율로 지정합니다.
- 음수 값을 사용하면 행 간격을 포인트 단위로 지정합니다.
다음 코드 예제는 단락 내 행 간격을 지정하는 방법을 보여줍니다:
$presentation = new Presentation("sample.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
$paragraph->getParagraphFormat()->setSpaceWithin(200);
$presentation->save("line_spacing.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
결과:

텍스트 프레임 자동 맞춤 유형 설정
TextFrameFormat::setAutofitType 은 텍스트가 컨테이너 경계를 초과할 때 텍스트가 어떻게 동작하는지를 결정합니다. 텍스트를 축소, 넘침, 또는 도형을 자동으로 크기 조정하도록 제어하는 데 사용합니다.
$presentation = new Presentation("sample.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$autoShape->getTextFrame()->getTextFrameFormat()->setAutofitType(TextAutofitType::Shape);
$presentation->save("autofit_type.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
텍스트 프레임 앵커 설정
TextFrameFormat::setAnchoringType 은 텍스트가 도형 내부에서 수직으로 어떻게 배치되는지를 정의합니다(예: 위쪽, 가운데, 아래쪽).
$presentation = new Presentation("sample.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$autoShape->getTextFrame()->getTextFrameFormat()->setAnchoringType(TextAnchorType::Bottom);
$presentation->save("text_anchor.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
텍스트 탭 설정
ParagraphFormat::setDefaultTabSize 과 ParagraphFormat::getTabs 을 사용하여 단락의 탭 정지를 구성합니다.
$presentation = new Presentation("sample.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$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();
}
결과:

교정 언어 설정
Aspose.Slides는 BasePortionFormat::setLanguageId 을 제공하여 텍스트 부분의 교정 언어를 설정할 수 있게 합니다. 교정 언어는 PowerPoint에서 맞춤법 및 문법 검사를 수행할 때 사용되는 언어를 결정합니다.
다음 코드 예제는 텍스트 부분의 교정 언어를 설정하는 방법을 보여줍니다:
$presentation = new Presentation("presentation.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$paragraph = $autoShape->getTextFrame()->getParagraphs()->get_Item(0);
$paragraph->getPortions()->clear();
$font = new FontData("SimSun");
$textPortion = new Portion();
$textPortion->getPortionFormat()->setComplexScriptFont($font);
$textPortion->getPortionFormat()->setEastAsianFont($font);
$textPortion->getPortionFormat()->setLatinFont($font);
// 교정 언어의 Id를 설정합니다.
$textPortion->getPortionFormat()->setLanguageId("zh-CN");
$textPortion->setText("1。");
$paragraph->getPortions()->add($textPortion);
$presentation->save("proofing_language.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
기본 언어 설정
LoadOptions::setDefaultTextLanguage 을 사용하여 프레젠테이션을 로드하거나 생성할 때 생성되는 텍스트의 기본 언어를 정의합니다.
$loadOptions = new LoadOptions();
$loadOptions->setDefaultTextLanguage("en-US");
$presentation = new Presentation($loadOptions);
try {
$slide = $presentation->getSlides()->get_Item(0);
// 새 사각형 도형을 텍스트와 함께 추가합니다.
$shape = $slide->getShapes()->addAutoShape(ShapeType::Rectangle, 20, 20, 150, 50);
$shape->getTextFrame()->setText("Sample text");
// 첫 번째 부분의 언어를 확인합니다.
$portion = $shape->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
echo $portion->getPortionFormat()->getLanguageId();
} finally {
$presentation->dispose();
}
기본 텍스트 스타일 설정
프레젠테이션 수준에서 기본 텍스트 서식을 적용하려면 Presentation::getDefaultTextStyle 을 사용합니다.
다음 코드 예제는 새 프레젠테이션의 모든 슬라이드에서 기본 굵은 14pt 글꼴을 설정하는 방법을 보여줍니다.
$presentation = new Presentation();
try {
// 최상위 레벨 단락 형식을 가져옵니다.
$paragraphFormat = $presentation->getDefaultTextStyle()->getLevel(0);
if (!java_is_null($paragraphFormat)) {
$paragraphFormat->getDefaultPortionFormat()->setFontHeight(14);
$paragraphFormat->getDefaultPortionFormat()->setFontBold(NullableBool::True);
}
$presentation->save("default_text_style.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
전체 대문자 효과가 적용된 텍스트 추출
PowerPoint에서 All Caps 글꼴 효과를 적용하면 원래 소문자로 입력했더라도 슬라이드에 대문자로 표시됩니다. Aspose.Slides를 사용해 해당 텍스트 부분을 가져오면 라이브러리는 입력된 그대로의 텍스트를 반환합니다. 표시된 텍스트와 일치시키려면 TextCapType 을 확인하고 값이 All 인 경우 반환 문자열을 대문자로 변환하십시오.
예를 들어 sample2.pptx 파일의 첫 번째 슬라이드에 다음과 같은 텍스트 상자가 있다고 가정합니다.

다음 코드 예제는 All Caps 효과가 적용된 텍스트를 추출하는 방법을 보여줍니다:
$presentation = new Presentation("sample2.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$autoShape = $slide->getShapes()->get_Item(0);
$textPortion = $autoShape->getTextFrame()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
$originalText = $textPortion->getText();
echo "Original text: ", $originalText, "\n";
$textFormat = $textPortion->getPortionFormat()->getEffective();
if (java_values($textFormat->getTextCapType()) === TextCapType::All) {
$text = strtoupper($originalText);
echo "All-Caps effect: ", $text, "\n";
}
} finally {
$presentation->dispose();
}
출력:
Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!
FAQ
슬라이드에 있는 표의 텍스트를 수정하려면 어떻게 해야 하나요?
슬라이드에 있는 표의 텍스트를 수정하려면 Table 을 사용하십시오. 셀을 반복하면서 Cell::getTextFrame 으로 각 셀을 업데이트하고, Paragraph::getParagraphFormat 으로 단락 서식을 업데이트합니다.
PowerPoint 슬라이드의 텍스트에 그라디언트 색을 적용하려면 어떻게 해야 하나요?
그라디언트 색을 적용하려면 BasePortionFormat::getFillFormat 을 사용하십시오. FillFormat::setFillType 을 FillType::Gradient 로 설정하고, 그라디언트 정지점, 방향 및 투명도를 구성합니다.