JavaScript로 PowerPoint 텍스트 단락 관리

개요

Aspose.Slides for Node.js via Java는 텍스트를 텍스트 프레임, 단락, 그리고 Portion의 계층 구조로 나타냅니다:

  • TextFrame은 모양 안의 텍스트 컨테이너를 나타내며 단락 컬렉션에 대한 접근을 제공합니다.
  • Paragraph은 텍스트 프레임 안의 하나의 단락을 나타내며 Portion 및 단락 수준 서식에 대한 접근을 제공합니다.
  • Portion은 단락 안의 텍스트 실행을 나타냅니다. 각 Portion은 자체 텍스트와 문자 수준 서식을 가질 수 있습니다.

따라서 단락은 여러 Portion을 사용하여 서로 다른 글꼴, 색상, 크기 및 기타 서식을 가진 텍스트를 포함할 수 있습니다.

단락 만들기 및 서식 지정

모양의 텍스트 프레임에서 단락을 만들고 Portion을 사용하여 개별 텍스트 실행에 서식을 지정합니다. 이 섹션에서는 각 단락 내에서 여러 Portion을 결합하고 서로 다른 색상, 글꼴 크기 및 스타일을 적용하는 방법을 보여 줍니다.

여러 Portion을 사용하여 단락 만들기

다음 JavaScript 예제는 Paragraph.getPortions이 반환하는 컬렉션에서 각각 세 개의 텍스트 실행을 포함하는 세 개의 단락을 생성합니다. 기본 단락과 빈 Portion을 재사용한 뒤 나머지 단락과 Portion을 추가합니다. 서식은 Portion.getPortionFormat을 통해 적용됩니다: 각 단락의 첫 번째 Portion은 빨간색, 굵게, 15pt; 두 번째는 파란색, 이탤릭, 18pt이며, 세 번째 Portion은 기본 서식을 유지합니다:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 50, 150, 300, 150);
    const textFrame = shape.getTextFrame();

    const firstParagraph = textFrame.getParagraphs().get_Item(0);
    firstParagraph.getPortions().add(new aspose.slides.Portion());
    firstParagraph.getPortions().add(new aspose.slides.Portion());

    const secondParagraph = new aspose.slides.Paragraph();
    secondParagraph.getPortions().add(new aspose.slides.Portion());
    secondParagraph.getPortions().add(new aspose.slides.Portion());
    secondParagraph.getPortions().add(new aspose.slides.Portion());
    textFrame.getParagraphs().add(secondParagraph);

    const thirdParagraph = new aspose.slides.Paragraph();
    thirdParagraph.getPortions().add(new aspose.slides.Portion());
    thirdParagraph.getPortions().add(new aspose.slides.Portion());
    thirdParagraph.getPortions().add(new aspose.slides.Portion());
    textFrame.getParagraphs().add(thirdParagraph);

    const paragraphCount = textFrame.getParagraphs().getCount();
    for (let paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++) {
        const paragraph = textFrame.getParagraphs().get_Item(paragraphIndex);
        const portionCount = paragraph.getPortions().getCount();
        for (let portionIndex = 0; portionIndex < portionCount; portionIndex++) {
            const portion = paragraph.getPortions().get_Item(portionIndex);
            portion.setText("Portion " + (paragraphIndex + 1) + "." + (portionIndex + 1));

            if (portionIndex === 0) {
                portion.getPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
                portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
                portion.getPortionFormat().setFontBold(java.newByte(aspose.slides.NullableBool.True));
                portion.getPortionFormat().setFontHeight(15);
            } else if (portionIndex === 1) {
                portion.getPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
                portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLUE"));
                portion.getPortionFormat().setFontItalic(java.newByte(aspose.slides.NullableBool.True));
                portion.getPortionFormat().setFontHeight(18);
            }
        }
    }

    presentation.save("paragraphs_with_portions.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

글머리표 및 번호 매기기 목록 만들기

기호, 숫자 또는 사용자 지정 이미지로 단락 텍스트를 목록으로 정리합니다. 이 섹션의 예제는 글머리표 서식, 다단계 목록 및 사용자 지정 시작 번호를 다룹니다.

글머리표 또는 번호 매기기 목록 만들기

글머리표와 번호 매기기를 사용하면 관련 항목을 더 쉽게 스캔할 수 있습니다. Aspose.Slides에서 목록 설정은 BulletFormat을 통해 정의됩니다.

이 JavaScript 예제는 같은 텍스트 프레임에 기호 글머리표와 번호 매기기 항목을 추가합니다. BulletFormat.setType은 글머리표 유형을 선택하고, BulletFormat.setChar은 기호를 제공하며, BulletFormat.setNumberedBulletStyle은 번호 매기기 스타일을 선택합니다. 두 항목 모두 높이가 100%이고 단락 들여쓰기가 25pt인 검은색 글머리표를 사용합니다:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 200, 200, 400, 200);
    const textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    const symbolParagraph = new aspose.slides.Paragraph();
    symbolParagraph.setText("Welcome to Aspose.Slides");
    symbolParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
    symbolParagraph.getParagraphFormat().getBullet().setChar(java.newChar(0x2022));
    symbolParagraph.getParagraphFormat().setIndent(25);
    symbolParagraph.getParagraphFormat().getBullet().getColor().setColorType(aspose.slides.ColorType.RGB);
    symbolParagraph.getParagraphFormat().getBullet().getColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    symbolParagraph.getParagraphFormat().getBullet().setBulletHardColor(java.newByte(aspose.slides.NullableBool.True));
    symbolParagraph.getParagraphFormat().getBullet().setHeight(100);
    textFrame.getParagraphs().add(symbolParagraph);

    const numberedParagraph = new aspose.slides.Paragraph();
    numberedParagraph.setText("This is a numbered item");
    numberedParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
    numberedParagraph.getParagraphFormat().getBullet().setNumberedBulletStyle(java.newByte(aspose.slides.NumberedBulletStyle.BulletCircleNumWDBlackPlain));
    numberedParagraph.getParagraphFormat().setIndent(25);
    numberedParagraph.getParagraphFormat().getBullet().getColor().setColorType(aspose.slides.ColorType.RGB);
    numberedParagraph.getParagraphFormat().getBullet().getColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    numberedParagraph.getParagraphFormat().getBullet().setBulletHardColor(java.newByte(aspose.slides.NullableBool.True));
    numberedParagraph.getParagraphFormat().getBullet().setHeight(100);
    textFrame.getParagraphs().add(numberedParagraph);

    presentation.save("bulleted_and_numbered_list.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

그림 글머리표 사용

그림 글머리표를 사용하면 기호나 숫자 대신 사용자 지정 이미지를 사용할 수 있습니다.

이 JavaScript 예제는 애플리케이션 작업 디렉터리에서 image.png를 로드하고, 프레젠테이션의 이미지 컬렉션에 PPImage으로 추가한 뒤 단락 글머리표로 사용합니다. BulletFormat.setType을 호출해 그림 글머리표를 사용하도록 하고, 이미지는 BulletFormat.getPicture를 통해 지정합니다. BulletFormat.setHeight을 100%로 설정해 단락 텍스트에 상대적인 크기로 글머리표를 조정합니다:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const bulletImage = aspose.slides.Images.fromFile("image.png");
    let presentationImage;
    try {
        presentationImage = presentation.getImages().addImage(bulletImage);
    } finally {
        bulletImage.dispose();
    }

    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 200, 200, 400, 200);
    const textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    const paragraph = new aspose.slides.Paragraph();
    paragraph.setText("Welcome to Aspose.Slides");
    paragraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Picture));
    paragraph.getParagraphFormat().getBullet().getPicture().setImage(presentationImage);
    paragraph.getParagraphFormat().getBullet().setHeight(100);
    textFrame.getParagraphs().add(paragraph);

    presentation.save("picture_bullet.pptx", aspose.slides.SaveFormat.Pptx);
    presentation.save("picture_bullet.ppt", aspose.slides.SaveFormat.Ppt);
} finally {
    presentation.dispose();
}

다단계 목록 만들기

ParagraphFormat.setDepth을 설정해 단락을 목록의 서로 다른 수준에 배치합니다. 최상위 수준은 깊이 0입니다.

이 JavaScript 예제는 ParagraphFormat.setDepth 값을 0부터 3까지 네 개의 단락에 할당해 하나의 텍스트 프레임 안에 계층을 만듭니다. 각 단락은 기호 글머리표를 사용하며, BulletFormat.setChar를 라운드 글머리표와 하이픈 사이에 번갈아 적용해 인접 수준을 구분합니다:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 200, 200, 400, 200);
    const textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    const firstParagraph = new aspose.slides.Paragraph();
    firstParagraph.setText("Content");
    firstParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
    firstParagraph.getParagraphFormat().getBullet().setChar(java.newChar(0x2022));
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    firstParagraph.getParagraphFormat().setDepth(java.newShort(0));

    const secondParagraph = new aspose.slides.Paragraph();
    secondParagraph.setText("Second level");
    secondParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
    secondParagraph.getParagraphFormat().getBullet().setChar(java.newChar(45));
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    secondParagraph.getParagraphFormat().setDepth(java.newShort(1));

    const thirdParagraph = new aspose.slides.Paragraph();
    thirdParagraph.setText("Third level");
    thirdParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
    thirdParagraph.getParagraphFormat().getBullet().setChar(java.newChar(0x2022));
    thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    thirdParagraph.getParagraphFormat().setDepth(java.newShort(2));

    const fourthParagraph = new aspose.slides.Paragraph();
    fourthParagraph.setText("Fourth level");
    fourthParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Symbol));
    fourthParagraph.getParagraphFormat().getBullet().setChar(java.newChar(45));
    fourthParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    fourthParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    fourthParagraph.getParagraphFormat().setDepth(java.newShort(3));

    textFrame.getParagraphs().add(firstParagraph);
    textFrame.getParagraphs().add(secondParagraph);
    textFrame.getParagraphs().add(thirdParagraph);
    textFrame.getParagraphs().add(fourthParagraph);

    presentation.save("multilevel_list.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

번호 매기기 목록 항목을 사용자 정의 값으로 시작

BulletFormat.setNumberedBulletStartWith을 사용해 번호 매기기 단락에 표시되는 초기 번호를 설정합니다.

이 JavaScript 예제는 세 개의 단락에 번호 매기기를 사용하도록 BulletFormat.setType을 설정하고, 각각 2, 3, 7 값을 갖는 BulletFormat.setNumberedBulletStartWith을 할당합니다. 시작 값은 각 단락마다 별도로 구성되어 표시되는 번호가 건너뛰도록 합니다:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 200, 200, 400, 200);
    const textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    const firstParagraph = new aspose.slides.Paragraph();
    firstParagraph.setText("Start at 2");
    firstParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
    firstParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(java.newShort(2));
    textFrame.getParagraphs().add(firstParagraph);

    const secondParagraph = new aspose.slides.Paragraph();
    secondParagraph.setText("Start at 3");
    secondParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
    secondParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(java.newShort(3));
    textFrame.getParagraphs().add(secondParagraph);

    const thirdParagraph = new aspose.slides.Paragraph();
    thirdParagraph.setText("Start at 7");
    thirdParagraph.getParagraphFormat().getBullet().setType(java.newByte(aspose.slides.BulletType.Numbered));
    thirdParagraph.getParagraphFormat().getBullet().setNumberedBulletStartWith(java.newShort(7));
    textFrame.getParagraphs().add(thirdParagraph);

    presentation.save("custom_numbered_list.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

단락 레이아웃 및 종료 속성 제어

첫 번째 줄 및 줄 바꿈 라인의 위치를 제어하기 위해 단락 여백과 들여쓰기를 조정합니다. 또한 보이는 텍스트 Portion과는 별도로 단락 종료 표시의 서식을 지정할 수 있습니다.

첫 번째 줄 들여쓰기 설정

ParagraphFormat.setIndent 메서드를 사용해 단락의 첫 번째 줄 들여쓰기를 제어합니다. 이 설정은 단락 왼쪽 여백에 상대적으로 첫 번째 줄만 이동시킵니다. 양수 값은 첫 번째 줄을 오른쪽으로 이동시키고, 나머지 라인은 단락 본문에 맞춥니다.

전체 단락을 이동해야 할 경우에는 ParagraphFormat.setMarginLeft를 사용하고, 첫 번째 줄만 이동하려면 ParagraphFormat.setIndent를 사용합니다.

다음 예제는 세 개의 단락에 동일한 ParagraphFormat.setMarginLeft 20pt와 ParagraphFormat.setIndent 값을 각각 0, 20, 40pt로 적용합니다. 첫 번째 줄은 점점 큰 오프셋에서 시작하고, 줄 바꿈 라인은 동일한 왼쪽 여백을 유지합니다:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 50, 50, 420, 220);
    shape.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
    shape.getLineFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "GRAY"));

    const textFrame = shape.getTextFrame();
    textFrame.getTextFrameFormat().setAutofitType(java.newByte(aspose.slides.TextAutofitType.Shape));
    textFrame.getParagraphs().clear();

    const firstParagraph = new aspose.slides.Paragraph();
    firstParagraph.setText("No first-line indent. Wrapped lines start at the same position as the first line.");
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    firstParagraph.getParagraphFormat().setMarginLeft(20);
    firstParagraph.getParagraphFormat().setIndent(0);

    const secondParagraph = new aspose.slides.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(java.newByte(aspose.slides.FillType.Solid));
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    secondParagraph.getParagraphFormat().setMarginLeft(20);
    secondParagraph.getParagraphFormat().setIndent(20);

    const thirdParagraph = new aspose.slides.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(java.newByte(aspose.slides.FillType.Solid));
    thirdParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    thirdParagraph.getParagraphFormat().setMarginLeft(20);
    thirdParagraph.getParagraphFormat().setIndent(40);

    textFrame.getParagraphs().add(firstParagraph);
    textFrame.getParagraphs().add(secondParagraph);
    textFrame.getParagraphs().add(thirdParagraph);

    presentation.save("paragraph_indent.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

결과:

단락의 첫 번째 줄 들여쓰기

걸린 들여쓰기 설정

걸린 들여쓰기는 첫 번째 줄이 나머지 라인보다 왼쪽에 시작되는 단락 레이아웃입니다. Aspose.Slides에서는 ParagraphFormat.setIndent 메서드에 음수 값을 지정해 첫 번째 줄을 단락 본문에 비해 왼쪽으로 이동합니다.

실제로는 ParagraphFormat.setMarginLeft이 단락 본문의 왼쪽 위치를 정의하고, ParagraphFormat.setIndent가 해당 여백에 대한 첫 번째 줄 위치를 정의합니다. 걸린 들여쓰기를 만들려면 왼쪽 여백은 양수로, 들여쓰기는 음수로 설정합니다.

이 서식은 참고문헌, 인용, 용어 설명 등 줄 바꿈 라인이 첫 번째 문자 아래가 아니라 단락 본문 아래에 정렬되어야 할 때 유용합니다.

다음 JavaScript 예제는 ParagraphFormat.setMarginLeft 값을 40pt와 60pt로, ParagraphFormat.setIndent 값을 각각 -20pt와 -30pt로 지정합니다. 각 단락에서 첫 번째 줄은 줄 바꿈 라인보다 왼쪽에 시작되고, 두 번째 단락은 더 깊은 걸린 들여쓰기를 가집니다:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 50, 50, 420, 220);
    shape.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
    shape.getLineFormat().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
    shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "GRAY"));

    const textFrame = shape.getTextFrame();
    textFrame.getTextFrameFormat().setAutofitType(java.newByte(aspose.slides.TextAutofitType.Shape));
    textFrame.getParagraphs().clear();

    const firstParagraph = new aspose.slides.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(java.newByte(aspose.slides.FillType.Solid));
    firstParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    firstParagraph.getParagraphFormat().setMarginLeft(40);
    firstParagraph.getParagraphFormat().setIndent(-20);

    const secondParagraph = new aspose.slides.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(java.newByte(aspose.slides.FillType.Solid));
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLACK"));
    secondParagraph.getParagraphFormat().setMarginLeft(60);
    secondParagraph.getParagraphFormat().setIndent(-30);

    textFrame.getParagraphs().add(firstParagraph);
    textFrame.getParagraphs().add(secondParagraph);

    presentation.save("hanging_indent.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

결과:

단락의 걸린 들여쓰기

단락 종료 실행 속성 설정

Paragraph.setEndParagraphPortionFormat 메서드는 단락 종료 표시의 서식을 제어합니다. 다음 예제는 PortionFormat을 만들고, BasePortionFormat.setFontHeight를 48pt, BasePortionFormat.setLatinFont를 Times New Roman으로 설정합니다. 이 서식을 두 번째 단락의 종료 표시에 할당하고, 보이는 텍스트 Portion은 변경하지 않습니다:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 10, 10, 200, 250);
    const textFrame = shape.getTextFrame();
    textFrame.getParagraphs().clear();

    const firstParagraph = new aspose.slides.Paragraph();
    firstParagraph.getPortions().add(new aspose.slides.Portion("Sample text"));

    const secondParagraph = new aspose.slides.Paragraph();
    secondParagraph.getPortions().add(new aspose.slides.Portion("Sample text 2"));

    const endParagraphFormat = new aspose.slides.PortionFormat();
    endParagraphFormat.setFontHeight(48);
    endParagraphFormat.setLatinFont(new aspose.slides.FontData("Times New Roman"));
    secondParagraph.setEndParagraphPortionFormat(endParagraphFormat);

    textFrame.getParagraphs().add(firstParagraph);
    textFrame.getParagraphs().add(secondParagraph);

    presentation.save("end_paragraph_format.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

렌더링된 줄 수 세기

줄 바꿈 및 구두점 제어와 같은 단락 규칙은 자동 줄 바꿈 및 줄 끝 구두점에 영향을 미칩니다. 자세히 보려면 줄 바꿈 제어 및 매달린 구두점 제어를 참고하십시오.

Paragraph.getLinesCount 메서드를 사용해 텍스트 레이아웃 후 단락이 차지하는 줄 수를 계산할 수 있습니다(자동 줄 바꿈 포함). 이는 프레젠테이션 템플릿에서 텍스트 길이와 레이아웃을 확인할 때 유용합니다.

단락은 TextFrame.getParagraphs의 한 항목이며, 여러 렌더링된 줄을 차지할 수 있습니다. 단락 내 명시적인 줄 바꿈은 새로운 줄을 강제하지만 새로운 단락을 만들지는 않습니다. 자동 줄 바꿈은 텍스트에 명시적인 줄 바꿈 문자를 삽입하지 않고 가용 폭에 따라 줄을 생성합니다. 따라서 단락 수나 줄 바꿈 문자 수를 세는 것만으로는 실제 렌더링된 줄 수를 알 수 없습니다.

다음 예제는 텍스트 도형을 만들고, 줄 수를 세고, 도형을 좁힌 뒤 텍스트를 짧은 문자열로 교체합니다. 예제는 TextFrameFormat.setWrapText으로 줄 바꿈을 활성화하고 TextFrameFormat.setAutofitType으로 자동 맞춤을 비활성화해 도형 너비가 줄 바꿈을 제어하도록 합니다. 각 측정은 Paragraph.getLinesCount으로 수행됩니다. 도형 크기는 포인트 단위입니다. 마지막으로 예제는 또 다른 단락을 추가하고 텍스트 프레임 전체의 줄 수를 합산합니다:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 50, 50, 400, 200);
    const textFrame = shape.getTextFrame();
    textFrame.getTextFrameFormat().setWrapText(java.newByte(aspose.slides.NullableBool.True));
    textFrame.getTextFrameFormat().setAutofitType(java.newByte(aspose.slides.TextAutofitType.None));

    const 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.");
    console.log("Original width: " + paragraph.getLinesCount());

    shape.setWidth(150);
    console.log("Narrower shape: " + paragraph.getLinesCount());

    paragraph.setText("Short text.");
    console.log("Shorter text: " + paragraph.getLinesCount());

    const secondParagraph = new aspose.slides.Paragraph();
    secondParagraph.setText("Another paragraph.");
    secondParagraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20);
    textFrame.getParagraphs().add(secondParagraph);

    let totalLineCount = 0;
    for (let i = 0; i < textFrame.getParagraphs().getCount(); i++) {
        const currentParagraph = textFrame.getParagraphs().get_Item(i);
        totalLineCount += currentParagraph.getLinesCount();
    }
    console.log("Total lines in the text frame: " + totalLineCount);
} finally {
    presentation.dispose();
}

이 텍스트와 이 크기로 도형을 좁히면 줄 수가 증가하고, 짧은 문자열로 교체하면 줄 수가 감소합니다. 정확한 줄 수는 사용 가능한 글꼴 및 대체, 글꼴 크기, 여백, 들여쓰기, 줄 바꿈 및 자동 맞춤 설정에 따라 달라질 수 있습니다. 템플릿을 확인할 때 대상 환경에 맞는 글꼴 및 레이아웃 설정을 사용하십시오.

줄 수만으로 텍스트가 컨테이너를 초과하는지를 판단할 수 없습니다. 사용 가능한 높이, 줄 높이, 단락 및 줄 간격, 자동 맞춤 동작도 중요합니다; 줄 바꿈이 비활성화된 경우 단일 줄이라도 가용 폭을 초과할 수 있습니다.

단락 내용 가져오기 및 내보내기

HTML을 사용해 텍스트를 프레젠테이션에 가져오거나 기존 단락을 외부에서 사용할 수 있도록 내보낼 수 있습니다. 이 섹션에서는 테이블 셀의 단락을 포함한 개별 단락을 이미지로 렌더링하는 방법도 보여 줍니다.

HTML 텍스트를 단락으로 가져오기

ParagraphCollection.addFromHtml을 사용해 HTML 마크업을 텍스트 프레임의 단락 및 Portion으로 변환합니다.

이 JavaScript 예제는 애플리케이션 작업 디렉터리의 file.html에서 HTML을 읽고, 마크업을 ParagraphCollection.addFromHtml에 전달합니다. 텍스트 프레임의 기본 단락을 지워 프레임에 가져온 내용만 포함되도록 합니다. 직사각형 도형은 슬라이드 각 측면에 10포인트 여백을 남깁니다:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const fs = require("fs");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const shapeWidth = presentation.getSlideSize().getSize().getWidth() - 20;
    const shapeHeight = presentation.getSlideSize().getSize().getHeight() - 20;
    const shape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 10, 10, shapeWidth, shapeHeight);
    shape.getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
    shape.getTextFrame().getParagraphs().clear();

    const html = fs.readFileSync("file.html", "utf8");
    shape.getTextFrame().getParagraphs().addFromHtml(html);
    presentation.save("html_text.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

단락 텍스트를 HTML로 내보내기

ParagraphCollection.exportToHtml을 사용해 선택된 단락 범위를 HTML로 내보냅니다. 프레젠테이션 텍스트를 웹사이트에 재사용할 때 폰트, 크기 또는 텍스트 상자 스타일을 가져오지 않고 로컬 강조만 유지하려면 TextToHtmlConversionOptions.setTextInheritanceLimit을 설정해 서식 상속을 어느 정도까지 따를지 제어합니다.

샘플 프레젠테이션을 다운로드하고 애플리케이션 작업 디렉터리에 배치하십시오. 첫 번째 슬라이드에는 텍스트 “Inherited text; explicit red 18pt"가 포함된 텍스트 도형 하나가 있습니다. 입력에는 여러 서식 레벨이 의도적으로 사용되었습니다:

  • 프레젠테이션의 기본 텍스트 스타일은 Calibri와 28pt 크기를 제공합니다.
  • 텍스트 프레임의 1단계 텍스트 스타일은 파란색 텍스트와 이탤릭을 제공합니다.
  • 단락은 명시적으로 굵은 텍스트를 설정합니다.
  • 두 번째 Portion인 “explicit red 18pt"는 명시적으로 빨간색 텍스트와 18pt 크기를 설정합니다.

다음 완전한 예제는 동일한 단락을 세 번 내보냅니다. TextToHtmlConversionOptions을 내보내기 메서드에 전달하고 각 결과를 별도의 UTF-8 HTML 파일로 저장합니다:

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");
const fs = require("fs");

const presentation = new aspose.slides.Presentation("text-inheritance.pptx");
try {
    const shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);

    if (java.instanceOf(shape, "com.aspose.slides.IAutoShape")) {
        const textFrame = shape.getTextFrame();
        if (textFrame !== null) {
            const paragraphs = textFrame.getParagraphs();

            const modes = [aspose.slides.TextInheritanceLimit.All, aspose.slides.TextInheritanceLimit.TextBox, aspose.slides.TextInheritanceLimit.ParagraphOnly];
            const fileNames = ["All.html", "TextBox.html", "ParagraphOnly.html"];

            for (let i = 0; i < modes.length; i++) {
                const options = new aspose.slides.TextToHtmlConversionOptions();
                options.setTextInheritanceLimit(modes[i]);

                const html = paragraphs.exportToHtml(0, paragraphs.getCount(), options);
                fs.writeFileSync(fileNames[i], html, "utf8");
            }
        } else {
            console.log("The first shape does not contain a text frame.");
        }
    } else {
        console.log("The first shape is not a text shape.");
    }
} finally {
    presentation.dispose();
}

생성된 HTML에는 첫 번째 Portion인 “Inherited text; “에 대한 다음 CSS가 포함됩니다. 표는 관련 선언만 나열하며, 다른 생성된 속성은 명확성을 위해 생략했습니다.

출력 첫 번째 Portion에 대한 관련 CSS 외관
All.html font-family:'Calibri'; font-size:28pt; color:#0000ff; font-style:italic; font-weight:bold Calibri가 사용 가능한 경우, 28pt의 파란색 굵은 이탤릭 텍스트.
TextBox.html color:#0000ff; font-style:italic; font-weight:bold 파란색 굵은 이탤릭 텍스트; 주변 페이지 또는 브라우저가 글꼴 패밀리와 크기를 제공합니다.
ParagraphOnly.html font-weight:bold 굵은 텍스트; 주변 페이지 또는 브라우저가 글꼴 패밀리, 크기, 색상 및 이탤릭 스타일을 제공합니다.

두 번째 Portion은 세 출력 모두에서 color:#ff0000, font-size:18pt, font-weight:bold를 유지합니다. 또한 All과 TextBox에서는 이탤릭을 유지하지만 ParagraphOnly에서는 유지하지 않습니다. 상속을 제한해도 단락이나 Portion에 명시적으로 지정된 서식은 제거되지 않습니다.

같은 브라우저에서 파일을 열어 비교하십시오. 제한된 두 모드에서는 누락된 CSS 속성이 대상 페이지의 스타일이나 브라우저 기본값에 의해 해석되므로 최종 외관은 HTML이 사용되는 위치에 따라 달라집니다.

HTML 클립보드 조각 생성

TextToHtmlConversionOptions.setAddClipboardFragmentHeader을 사용해 ParagraphCollection.exportToHtml의 출력 방식을 선택합니다. false이면 일반 HTML을 반환하고, true이면 Windows 클립보드 헤더가 포함된 문자열을 반환합니다. 반환된 문자열의 StartHTML 및 EndHTML 필드는 HTML의 UTF-8 바이트 오프셋을, StartFragment 및 EndFragment는 붙여넣을 내용을 식별합니다.

이 예제는 두 개의 서식이 적용된 단락을 일반 HTML(html)과 HTML 클립보드 조각(clipboardHtml)으로 내보냅니다. 애플리케이션은 이후 조각을 시스템 클립보드에 기록할 수 있습니다. 클립보드 API가 원시 바이트를 기대한다면, 바이트 순서 표시(BOM) 없이 UTF-8로 인코딩하십시오.

const slides = require("aspose.slides.via.java");

const presentation = new slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const shape = slide.getShapes().addAutoShape(slides.ShapeType.Rectangle, 0, 0, 400, 100);
    const paragraphs = shape.getTextFrame().getParagraphs();
    paragraphs.clear();
    paragraphs.addFromHtml("<p>Status: <b>Ready</b></p><p><i>Next steps</i></p>");

    const options = new slides.TextToHtmlConversionOptions();
    options.setAddClipboardFragmentHeader(false);
    const html = paragraphs.exportToHtml(0, paragraphs.getCount(), options);

    options.setAddClipboardFragmentHeader(true);
    const clipboardHtml = paragraphs.exportToHtml(0, paragraphs.getCount(), options);
} finally {
    presentation.dispose();
}

단락을 이미지로 렌더링

Paragraph.getImage 은 개별 단락을 직접 렌더링하고 IImage를 반환합니다. 결과를 IImage.save 로 파일이나 스트림에 저장하십시오. 포함된 도형을 렌더링하거나 비트맵을 수동으로 자를 필요가 없습니다.

Paragraph.getImage 은 단락을 상위 컬렉션에서 찾을 수 없거나 유효한 렌더링 경계가 없거나 렌더링이 불가능한 경우 null을 반환할 수 있습니다. 저장하기 전에 결과를 확인하고 사용 후 반환된 이미지를 해제하십시오.

기본 배율로 단락 렌더링

예제는 첫 번째 도형이 세 개의 단락을 포함한 텍스트 상자인 하나의 슬라이드를 만든 뒤 실행합니다.

세 단락이 포함된 텍스트 상자

다음 예제는 인덱스 1에 있는 두 번째 단락을 선택하고 배율 매개변수 없이 Paragraph.getImage를 호출합니다. 도형에 해당 단락이 포함되어 있는지 확인하고, 렌더링이 이미지로 반환되는지 확인한 뒤 IImage.save 로 PNG 형식으로 저장합니다. finally 블록은 사용 후 이미지를 해제합니다.

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);

    const sourceShape = slide.getShapes().addAutoShape(aspose.slides.ShapeType.Rectangle, 20, 20, 400, 100);
    const sourceTextFrame = sourceShape.getTextFrame();
    sourceTextFrame.getParagraphs().clear();
    for (const text of ["First paragraph", "Second paragraph", "Third paragraph"]) {
        const sourceParagraph = new aspose.slides.Paragraph();
        sourceParagraph.setText(text);
        sourceTextFrame.getParagraphs().add(sourceParagraph);
    }
    const shape = slide.getShapes().get_Item(0);

    if (java.instanceOf(shape, "com.aspose.slides.IAutoShape")) {
        const textFrame = shape.getTextFrame();
        if (textFrame !== null && textFrame.getParagraphs().getCount() > 1) {
            const paragraph = textFrame.getParagraphs().get_Item(1);
            const paragraphImage = paragraph.getImage();

            if (paragraphImage !== null) {
                try {
                    paragraphImage.save("paragraph.png", aspose.slides.ImageFormat.Png);
                } finally {
                    paragraphImage.dispose();
                }
            } else {
                console.log("The paragraph could not be rendered.");
            }
        } else {
            console.log("The expected paragraph was not found.");
        }
    } else {
        console.log("The first shape is not a text shape.");
    }
} finally {
    presentation.dispose();
}

결과:

단락 이미지

테이블 셀에서 스케일링을 이용한 단락 렌더링

scaleX와 scaleY 매개변수를 받아 가로 및 세로 스케일을 지정하는 Paragraph.getImage 오버로드를 사용합니다. 다음 예제는 테이블을 만든 뒤 첫 번째 셀의 단락을 기본 너비와 높이의 두 배로 렌더링하고 PNG 이미지로 저장합니다.

var aspose = aspose || {};
aspose.slides = require("aspose.slides.via.java");
const java = require("java");

const scaleX = 2;
const scaleY = 2;

const presentation = new aspose.slides.Presentation();
try {
    const slide = presentation.getSlides().get_Item(0);
    
    const columnWidths = java.newArray("double", [300]);
    const rowHeights = java.newArray("double", [80]);
    const table = slide.getShapes().addTable(50, 50, columnWidths, rowHeights);
    const paragraph = table.get_Item(0, 0).getTextFrame().getParagraphs().get_Item(0);
    paragraph.setText("Text in a table cell");

    const paragraphImage = paragraph.getImage(scaleX, scaleY);
    if (paragraphImage !== null) {
        try {
            paragraphImage.save("table_paragraph.png", aspose.slides.ImageFormat.Png);
        } finally {
            paragraphImage.dispose();
        }
    } else {
        console.log("The paragraph could not be rendered.");
    }
} finally {
    presentation.dispose();
}

스케일 팩터 1은 해당 축을 기본 픽셀 크기로 유지합니다. 예를 들어 두 축 모두 2이면 너비와 높이가 대략 두 배가 되어 픽셀 수가 네 배가 됩니다. 큰 팩터는 확대하거나 고해상도 출력 시 텍스트를 더 선명하게 만들지만 메모리 사용량과 파일 크기도 증가합니다. 1 이하의 팩터는 상세도가 낮은 작은 이미지를 생성합니다. 가로와 세로 팩터를 동일하게 설정하면 단락의 종횡비를 유지하고, 서로 다른 팩터를 사용하면 출력이 독립적으로 늘어나거나 줄어듭니다.

도형의 채우기, 테두리 또는 기타 시각적 컨텍스트가 포함된 전체 도형을 출력해야 할 경우 Shape.getImage를 사용하는 것이 유용합니다. 오직 단락만 이미지로 만들려면 Paragraph.getImage를 사용하십시오.

FAQ

텍스트 프레임 내부에서 줄 바꿈을 완전히 비활성화할 수 있나요?

예. TextFrameFormat.setWrapText을 설정해 줄 바꿈을 비활성화하면 줄이 텍스트 프레임 가장자리를 넘어 끊기지 않습니다.

특정 단락의 슬라이드 상 정확한 경계 사각형을 어떻게 얻나요?

Paragraph.getRect을 사용해 단락의 경계 사각형을 가져옵니다. Portion.getRect은 개별 Portion의 경계를 제공합니다.

단락 정렬(좌, 우, 가운데, 양쪽 맞춤)은 어디에서 제어되나요?

ParagraphFormat.setAlignment은 단락 수준 설정이며 개별 Portion 서식과 무관하게 전체 단락에 적용됩니다.

줄 내에서 서로 다른 글꼴 크기의 Portion을 수직으로 맞추려면 줄 내에서 글꼴 정렬을 참고하십시오.

단락의 일부에 교정 언어를 설정할 수 있나요?

예. 개별 Portion에 대해 BasePortionFormat.setLanguageId를 설정하면 하나의 단락에 여러 언어의 텍스트를 포함할 수 있습니다.