Quản lý Hộp Văn Bản trong Bài Thuyết Trình trên Android

Giới thiệu

Văn bản trên các slide thường tồn tại trong các hộp văn bản hoặc hình dạng. Do đó, để thêm văn bản vào một slide, bạn phải thêm một hộp văn bản và sau đó đặt một số văn bản bên trong hộp văn bản. Aspose.Slides cho Android qua Java cung cấp giao diện IAutoShape cho phép bạn thêm một hình dạng chứa một số văn bản.

Tạo Hộp Văn Bản trên Slide

Để tạo một hộp văn bản trên slide, thực hiện các bước sau:

  1. Tạo một thể hiện của lớp Presentation.
  2. Lấy tham chiếu tới slide đầu tiên trong bản trình bày mới được tạo.
  3. Thêm một đối tượng IAutoShape với ShapeType được đặt là Rectangle tại vị trí xác định trên slide và lấy tham chiếu tới đối tượng IAutoShape mới được thêm.
  4. Thêm thuộc tính TextFrame vào đối tượng IAutoShape sẽ chứa một văn bản. Trong ví dụ dưới, chúng tôi đã thêm văn bản này: Aspose TextBox
  5. Cuối cùng, ghi tệp PPTX thông qua đối tượng Presentation.

Mã Java này — một triển khai các bước trên — cho bạn thấy cách thêm văn bản vào slide:

// Khởi tạo Presentation
Presentation pres = new Presentation();
try {
    // Lấy slide đầu tiên trong bản trình bày
    ISlide sld = pres.getSlides().get_Item(0);

    // Thêm một AutoShape với loại được đặt là Rectangle
    IAutoShape ashp = sld.getShapes().addAutoShape(ShapeType.Rectangle, 150, 75, 150, 50);

    // Thêm TextFrame vào hình chữ nhật
    ashp.addTextFrame(" ");

    // Truy cập khung văn bản
    ITextFrame txtFrame = ashp.getTextFrame();

    // Tạo đối tượng Paragraph cho khung văn bản
    IParagraph para = txtFrame.getParagraphs().get_Item(0);

    // Tạo đối tượng Portion cho đoạn văn
    IPortion portion = para.getPortions().get_Item(0);

    // Đặt văn bản
    portion.setText("Aspose TextBox");

    // Lưu bản trình bày vào đĩa
    pres.save("TextBox_out.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

Kiểm Tra Hình Dạng Hộp Văn Bản

Aspose.Slides cung cấp phương thức isTextBox từ giao diện IAutoShape , cho phép bạn kiểm tra các hình dạng và xác định các hộp văn bản.

Hộp văn bản và hình dạng

Mã Java này cho bạn thấy cách kiểm tra xem một hình dạng có được tạo thành hộp văn bản hay không:

Presentation presentation = new Presentation("sample.pptx");
try {
    ForEach.shape(presentation, (shape, slide, index) -> {
        if (shape instanceof IAutoShape) {
            IAutoShape autoShape = (IAutoShape) shape;
            System.out.println(autoShape.isTextBox() ? "shape is a text box" : "shape is not a text box");
        }
    });
} finally {
    presentation.dispose();
}

Lưu ý rằng nếu bạn chỉ thêm một autoshape bằng phương thức addAutoShape từ giao diện IShapeCollection , phương thức isTextBox của autoshape sẽ trả về false. Tuy nhiên, sau khi bạn thêm văn bản vào autoshape bằng phương thức addTextFrame hoặc setText, thuộc tính isTextBox sẽ trả về true.

Presentation presentation = new Presentation();
ISlide slide = presentation.getSlides().get_Item(0);

IAutoShape shape1 = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 10, 100, 40);
// shape1.isTextBox() trả về false
shape1.addTextFrame("shape 1");
// shape1.isTextBox() trả về true

IAutoShape shape2 = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 110, 100, 40);
// shape2.isTextBox() trả về false
shape2.getTextFrame().setText("shape 2");
// shape2.isTextBox() trả về true

IAutoShape shape3 = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 210, 100, 40);
// shape3.isTextBox() trả về false
shape3.addTextFrame("");
// shape3.isTextBox() trả về false

IAutoShape shape4 = slide.getShapes().addAutoShape(ShapeType.Rectangle, 10, 310, 100, 40);
// shape4.isTextBox() trả về false
shape4.getTextFrame().setText("");
// shape4.isTextBox() trả về false

Thêm Cột vào Hộp Văn Bản

Aspose.Slides cung cấp các thuộc tính ColumnCountColumnSpacing (từ giao diện ITextFrameFormat và lớp TextFrameFormat) cho phép bạn thêm cột vào hộp văn bản. Bạn có thể chỉ định số cột trong một hộp văn bản và đặt khoảng cách (đơn vị điểm) giữa các cột.

Mã Java này minh họa hoạt động được mô tả:

Presentation pres = new Presentation();
try {
    // Lấy slide đầu tiên trong bản trình bày
    ISlide slide = pres.getSlides().get_Item(0);

    // Thêm một AutoShape với loại được đặt là Rectangle
    IAutoShape aShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 300, 300);

    // Thêm TextFrame vào hình chữ nhật
    aShape.addTextFrame("All these columns are limited to be within a single text container -- " +
            "you can add or delete text and the new or remaining text automatically adjusts " +
            "itself to flow within the container. You cannot have text flow from one container " +
            "to other though -- we told you PowerPoint's column options for text are limited!");

    // Lấy định dạng văn bản của TextFrame
    ITextFrameFormat format = aShape.getTextFrame().getTextFrameFormat();

    // Xác định số cột trong TextFrame
    format.setColumnCount(3);

    // Xác định khoảng cách giữa các cột
    format.setColumnSpacing(10);

    // Lưu bản trình bày
    pres.save("ColumnCount.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

Thêm Cột vào Khung Văn Bản

Aspose.Slides cho Android qua Java cung cấp thuộc tính ColumnCount (từ giao diện ITextFrameFormat) cho phép bạn thêm cột trong khung văn bản. Thông qua thuộc tính này, bạn có thể chỉ định số cột mong muốn trong một khung văn bản.

Mã Java này cho bạn thấy cách thêm một cột vào trong khung văn bản:

String outPptxFileName = "ColumnsTest.pptx";
Presentation pres = new Presentation();
try {
    IAutoShape shape1 = pres.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 100, 100, 300, 300);
    TextFrameFormat format = (TextFrameFormat)shape1.getTextFrame().getTextFrameFormat();

    format.setColumnCount(2);
    shape1.getTextFrame().setText("All these columns are forced to stay within a single text container -- " +
            "you can add or delete text - and the new or remaining text automatically adjusts " +
            "itself to stay within the container. You cannot have text spill over from one container " +
            "to other, though -- because PowerPoint's column options for text are limited!");
    pres.save(outPptxFileName, SaveFormat.Pptx);

    Presentation test = new Presentation(outPptxFileName);
    try {
        IAutoShape autoShape = ((AutoShape)test.getSlides().get_Item(0).getShapes().get_Item(0));
        Assert.assertTrue(2 == autoShape.getTextFrame().getTextFrameFormat().getColumnCount());
        Assert.assertTrue(Double.NaN == autoShape.getTextFrame().getTextFrameFormat().getColumnSpacing());
    } finally {
        if (test != null) test.dispose();
    }

    format.setColumnSpacing(20);
    pres.save(outPptxFileName, SaveFormat.Pptx);

    Presentation test1 = new Presentation(outPptxFileName);
    try {
        IAutoShape autoShape = ((AutoShape)test1.getSlides().get_Item(0).getShapes().get_Item(0));
        Assert.assertTrue(2 == autoShape.getTextFrame().getTextFrameFormat().getColumnCount());
        Assert.assertTrue(20 == autoShape.getTextFrame().getTextFrameFormat().getColumnSpacing());
    } finally {
        if (test1 != null) test1.dispose();
    }

    format.setColumnCount(3);
    format.setColumnSpacing(15);
    pres.save(outPptxFileName, SaveFormat.Pptx);

    Presentation test2 = new Presentation(outPptxFileName);
    try {
        IAutoShape autoShape = ((AutoShape)test2.getSlides().get_Item(0).getShapes().get_Item(0));
        Assert.assertTrue(3 == autoShape.getTextFrame().getTextFrameFormat().getColumnCount());
        Assert.assertTrue(15 == autoShape.getTextFrame().getTextFrameFormat().getColumnSpacing());
    } finally {
        if (test2 != null) test2.dispose();
    }
} finally {
    if (pres != null) pres.dispose();
}

Cập Nhật Văn Bản

Aspose.Slides cho phép bạn thay đổi hoặc cập nhật văn bản chứa trong một hộp văn bản hoặc tất cả các văn bản trong một bản trình bày.

Mã Java này minh họa một thao tác mà trong đó tất cả các văn bản trong bản trình bày được cập nhật hoặc thay đổi:

Presentation pres = new Presentation("text.pptx");
try {
    for (ISlide slide : pres.getSlides())
    {
        for (IShape shape : slide.getShapes())
        {
            if (shape instanceof IAutoShape) //Kiểm tra xem hình dạng có hỗ trợ khung văn bản (IAutoShape) hay không.
            {
                IAutoShape autoShape = (IAutoShape)shape; 
                for (IParagraph paragraph : autoShape.getTextFrame().getParagraphs()) //Duyệt qua các đoạn trong khung văn bản
                {
                    for (IPortion portion : paragraph.getPortions()) //Duyệt qua mỗi phần trong đoạn
                    {
                        portion.setText(portion.getText().replace("years", "months")); //Thay đổi văn bản
                        portion.getPortionFormat().setFontBold(NullableBool.True); //Thay đổi định dạng
                    }
                }
            }
        }
    }

    //Lưu bản trình bày đã sửa đổi
    pres.save("text-changed.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

Thêm Hộp Văn Bản với Siêu Liên Kết

Bạn có thể chèn một liên kết bên trong hộp văn bản. Khi hộp văn bản được nhấp, người dùng sẽ được chuyển đến mở liên kết.

Để thêm một hộp văn bản chứa liên kết, thực hiện các bước sau:

  1. Tạo một thể hiện của lớp Presentation.
  2. Lấy tham chiếu tới slide đầu tiên trong bản trình bày mới tạo.
  3. Thêm một đối tượng AutoShape với ShapeType được đặt là Rectangle tại vị trí xác định trên slide và lấy tham chiếu của đối tượng AutoShape mới được thêm.
  4. Thêm một TextFrame vào đối tượng AutoShape chứa Aspose TextBox làm văn bản mặc định.
  5. Tạo một thể hiện của lớp IHyperlinkManager.
  6. Gán đối tượng IHyperlinkManager vào thuộc tính HyperlinkClick liên quan đến phần bạn muốn trong TextFrame.
  7. Cuối cùng, ghi tệp PPTX thông qua đối tượng Presentation.

Mã Java này — một triển khai các bước trên — cho bạn thấy cách thêm một hộp văn bản có siêu liên kết vào slide:

// Tạo một đối tượng Presentation đại diện cho file PPTX
Presentation pres = new Presentation();
try {
    // Lấy slide đầu tiên trong bản trình bày
    ISlide slide = pres.getSlides().get_Item(0);

    // Thêm một đối tượng AutoShape với loại được đặt là Rectangle
    IShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 150, 150, 150, 50);

    // Ép kiểu hình dạng sang AutoShape
    IAutoShape pptxAutoShape = (IAutoShape)shape;

    // Truy cập thuộc tính ITextFrame liên kết với AutoShape
    pptxAutoShape.addTextFrame("");

    ITextFrame textFrame = pptxAutoShape.getTextFrame();

    // Thêm một số văn bản vào khung
    textFrame.getParagraphs().get_Item(0).getPortions().get_Item(0).setText("Aspose.Slides");

    // Đặt Hyperlink cho văn bản phần
    IHyperlinkManager hyperlinkManager = textFrame.getParagraphs().get_Item(0).getPortions().get_Item(0).
            getPortionFormat().getHyperlinkManager();
    hyperlinkManager.setExternalHyperlinkClick("http://www.aspose.com");

    // Lưu bản trình chiếu PPTX
    pres.save("hLink_out.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

CÂU HỎI THƯỜNG GẶP

Sự khác nhau giữa hộp văn bản và trình giữ chỗ văn bản khi làm việc với các slide master là gì?

Một placeholder thừa hưởng kiểu dáng/vị trí từ master và có thể được ghi đè trên layouts, trong khi một hộp văn bản thông thường là một đối tượng độc lập trên một slide cụ thể và không thay đổi khi bạn chuyển đổi giữa các layout.

Làm sao tôi có thể thực hiện việc thay thế văn bản hàng loạt trên toàn bộ bản trình bày mà không ảnh hưởng tới văn bản trong biểu đồ, bảng và SmartArt?

Giới hạn việc lặp lại của bạn chỉ đối với các auto-shape có khung văn bản và loại trừ các đối tượng nhúng (biểu đồ, bảng, SmartArt) bằng cách duyệt các bộ sưu tập của chúng riêng biệt hoặc bỏ qua các loại đối tượng đó.