.NET에서 텍스트를 서식 지정하는 방법

.NET에서 텍스트를 서식 지정하는 방법

Aspose.Slides FOSS for .NET provides fine-grained text formatting through the PortionFormat 클래스. A Portion 텍스트의 가장 작은 독립 단위이며; 단락 내에서 단일 서식 실행에 매핑됩니다. 이 가이드는 프레젠테이션에서 텍스트에 굵게, 기울임꼴, 글꼴 크기 및 색상 서식을 적용하는 방법을 보여줍니다.

단계별 가이드

단계 1: 패키지 설치

dotnet add package Aspose.Slides.Foss

단계 2: 텍스트 프레임이 있는 도형 추가

텍스트를 서식 지정하기 전에, 도형은 a 를 포함해야 합니다. TextFrame.사용 shape.AddTextFrame() 하나를 만들기 위해.

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 500, 150);
var tf = shape.AddTextFrame("Default text: will be formatted");
prs.Save("output.pptx", SaveFormat.Pptx);

단계 3: TextFrame에 접근하기

shape.AddTextFrame() 반환합니다 TextFrame 객체. 나중에 다음을 통해서도 가져올 수 있습니다 shape.TextFrame.

var tf = shape.TextFrame;          // if the frame already exists
var tf = shape.AddTextFrame("");   // creates a new frame

A TextFrame 목록을 포함합니다 Paragraph 객체 (tf.Paragraphs). 각 Paragraph 포함합니다 Portion 객체 (paragraph.Portions).


단계 4: 굵게 및 기울임 서식 적용

사용 PortionFormat.FontBoldPortionFormat.FontItalic. 이러한 속성은 받아들입니다 NullableBool.True, NullableBool.False, 또는 NullableBool.NotDefined (master에서 상속).

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 500, 150);
var tf = shape.AddTextFrame("Bold and italic text");

var fmt = tf.Paragraphs[0].Portions[0].PortionFormat;
fmt.FontBold = NullableBool.True;
fmt.FontItalic = NullableBool.True;

prs.Save("bold-italic.pptx", SaveFormat.Pptx);

단계 5: 글꼴 크기 및 색상 설정

설정 PortionFormat.FontHeight 크기 (포인트 단위) 및 사용 FillFormat 색상용.

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Drawing;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 500, 150);
var tf = shape.AddTextFrame("Large corporate-blue heading");

var fmt = tf.Paragraphs[0].Portions[0].PortionFormat;
fmt.FontHeight = 32;                          // 32pt font
fmt.FontBold = NullableBool.True;
fmt.FillFormat.FillType = FillType.Solid;
fmt.FillFormat.SolidFillColor.Color = Color.FromArgb(255, 0, 70, 127);

prs.Save("colored-text.pptx", SaveFormat.Pptx);

Color.FromArgb(alpha, red, green, blue) 각 채널에 대해 0-255 값을 허용합니다.


단계 6: 하나의 단락에 여러 Portion 사용

단일 단락은 서로 다른 서식을 가진 여러 부분을 포함할 수 있습니다. 새 항목을 추가합니다 Portion 단락의 Portions 컬렉션:

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Drawing;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var slide = prs.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 50, 50, 600, 100);
var tf = shape.AddTextFrame("");  // start with empty frame

var paragraph = tf.Paragraphs[0];

// First portion: normal text
var portion1 = paragraph.Portions[0];
portion1.Text = "Normal text followed by ";
portion1.PortionFormat.FontHeight = 20;

// Second portion: bold red text
var portion2 = new Portion();
portion2.Text = "bold red text";
portion2.PortionFormat.FontHeight = 20;
portion2.PortionFormat.FontBold = NullableBool.True;
portion2.PortionFormat.FillFormat.FillType = FillType.Solid;
portion2.PortionFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(255, 200, 0, 0);
paragraph.Portions.Add(portion2);

prs.Save("mixed-format.pptx", SaveFormat.Pptx);

일반적인 문제 및 해결책

색상을 설정한 후에도 텍스트가 검게 표시됩니다

확인하십시오 FillFormat.FillType = FillType.Solid 색상을 할당하기 전에 설정되어야 합니다. 채우기 유형을 설정하지 않으면 색상 변경이 효과가 없을 수 있습니다.

NullableBool.True vs true

PortionFormat.FontBold 예상합니다 NullableBool.True, C#이 아니라 true. C#을 할당하는 true 유형이 호환되지 않기 때문에 컴파일되지 않습니다.

저장된 파일에 글꼴이 표시되지 않습니다.

LatinFont 속성은 라틴 글꼴 패밀리를 설정합니다. 설정되지 않으면 프레젠테이션 테마 글꼴이 사용됩니다. 사용자 정의 글꼴은 포함되거나 뷰어 머신에 있어야 합니다.


자주 묻는 질문

글꼴 패밀리를 어떻게 변경하나요?

설정 PortionFormat.LatinFont:

fmt.LatinFont = new FontData("Arial");

FontData 글꼴 패밀리 이름을 문자열로 허용합니다.

단락 정렬을 어떻게 설정하나요?

사용 ParagraphFormat.Alignment:

tf.Paragraphs[0].ParagraphFormat.Alignment = TextAlignment.Center;

지원되는 값: Left, Center, Right, Justify.

줄 간격을 어떻게 설정하나요?

사용 ParagraphFormat.SpaceBefore (문단 앞에 점) 또는 ParagraphFormat.SpaceAfter (문단 뒤의 점):

tf.Paragraphs[0].ParagraphFormat.SpaceBefore = 12;   // 12pt before
tf.Paragraphs[0].ParagraphFormat.SpaceAfter = 6;     // 6pt after

또 보기

 한국어