การจัดรูปแบบให้ข้อความใน PowerPoint ด้วย Apache POI

ต่อไปนี้เป็นตัวอย่างการจัดรูปแบบให้ข้อความ ไม่ว่าทำให้ข้อความเป็น ตัวหนา,ตัวเอียง,ใส่สีข้อความ,ขีดเส้นใต้ และอื่นๆ

ตัวอย่างโคด
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.java.poi.powerpoint;
 
import java.io.FileOutputStream;
 
import org.apache.poi.xslf.usermodel.SlideLayout;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
import org.apache.poi.xslf.usermodel.XSLFSlideMaster;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xslf.usermodel.XSLFTextShape;
 
public class TextFormatPowerPoint {
    public static void main(String[] args) {
        try {
            // path ของไฟล์
            String fileName = "C:\\poi\\TextFormatPowerPoint.pptx";
            // บันทึกเอกสารไว้ในระบบตาม path ที่ระบุ
            FileOutputStream out = new FileOutputStream(fileName);
            // สร้างภาพนิ่งเปล่าๆ
            XMLSlideShow ppt = new XMLSlideShow();
            // ดึงค่า object ของ slide master
            XSLFSlideMaster slideMaster = ppt.getSlideMasters()[0];
            // ดึงค่า slide layout
            XSLFSlideLayout slidelayout = slideMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);
            // สร้าง  slide แรก
            XSLFSlide slide = ppt.createSlide(slidelayout);
            // ดึงค่า  place holder คือการดึงค่าตำแหน่งของข้อความที่จะใส่ตาม layout นั้นๆ
            XSLFTextShape title = slide.getPlaceholder(0);
            // ใส่ เนื้อหาใน title
            title.setText("Text Formating PowerPoint");
             
            // ดึงค่า  place holder ตัวที่สอง
            XSLFTextShape body = slide.getPlaceholder(1);
            body.clearText();
            // ใส่ เนื้อหาใน body
            XSLFTextParagraph paragraph=body.addNewTextParagraph();
            // ใส่สี
            XSLFTextRun textColor = paragraph.addNewTextRun();
            textColor.setText("Text Color.");
            textColor.setFontColor(java.awt.Color.red);
            paragraph.addLineBreak();
             
            // กำหนดขนาด text
            XSLFTextRun textSize = paragraph.addNewTextRun();
            textSize.setText("Text Size.");
            textSize.setFontSize(50);
            paragraph.addLineBreak();
             
            // ตัวหนา
            XSLFTextRun textBold = paragraph.addNewTextRun();
            textBold.setText("Text Bold.");
            textBold.setBold(true);
            paragraph.addLineBreak();
             
            // ตัวเอียง
            XSLFTextRun textItalic = paragraph.addNewTextRun();
            textItalic.setText("Text Italic.");
            textItalic.setItalic(true);
            paragraph.addLineBreak();
             
            // ตัวหนาและอียง
            XSLFTextRun textBoldAndItalic = paragraph.addNewTextRun();
            textBoldAndItalic.setText("Text Bold And Italic.");
            textBoldAndItalic.setBold(true);
            textBoldAndItalic.setItalic(true);
            paragraph.addLineBreak();
             
            // ตัวตัด
            XSLFTextRun textStrikethrough = paragraph.addNewTextRun();
            textStrikethrough.setText("Text Strikethrough.");
            textStrikethrough.setStrikethrough(true);
            paragraph.addLineBreak();
             
            // ขีดเส้นใต้
            XSLFTextRun textUnderline = paragraph.addNewTextRun();
            textUnderline.setText("Text underlined line.");
            textUnderline.setUnderline(true);
            paragraph.addLineBreak();
             
            ppt.write(out);
            out.close();
            System.out.println("slide created successfully");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

ไฟล์ที่ถูกสร้าง

 ผลลัพธ์ที่ได้ 

About Nop

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment