Apache-poi-ppt-formatting-text

提供:Dev Guides
移動先:案内検索

Apache POI PPT-テキストのフォーマット

プレゼンテーションのテキストは、 XSLFTextRun クラスのメソッドを使用してフォーマットできます。 そのためには、以下に示すようにスライドレイアウトのいずれかを選択して XSLFTextRun クラスオブジェクトを作成する必要があります-

//create the empty presentation
XMLSlideShow ppt = new XMLSlideShow();

//getting the slide master object
XSLFSlideMaster slideMaster = ppt.getSlideMasters()[0];

//select a layout from specified list
XSLFSlideLayout slidelayout = slideMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);

//creating a slide with title and content layout
XSLFSlide slide = ppt.createSlide(slidelayout);

//selection of title place holder
XSLFTextShape body = slide.getPlaceholder(1);

//clear the existing text in the slide
body.clearText();

//adding new paragraph
XSLFTextParagraph paragraph = body.addNewTextParagraph();

//creating text run object
XSLFTextRun run = paragraph.addNewTextRun();
  • setFontSize()*を使用して、プレゼンテーション内のテキストのフォントサイズを設定できます。
run.setFontColor(java.awt.Color.red);
run.setFontSize(24);

次のコードスニペットは、さまざまな書式設定スタイル(太字、斜体、下線、取り消し線)をプレゼンテーションのテキストに適用する方法を示しています。

//change the text into bold format
run.setBold(true);

//change the text it to italic format
run.setItalic(true)

//strike through the text
run.setStrikethrough(true);

//underline the text
run.setUnderline(true);

段落間で改行するには、以下に示すように XSLFTextParagraph クラスの* addLineBreak()*を使用します-

paragraph.addLineBreak();

上記のすべての方法を使用してテキストをフォーマットする完全なプログラムを以下に示します-

import java.io.FileOutputStream;
import java.io.IOException;

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 TextFormating {

   public static void main(String args[]) throws IOException {

     //creating an empty presentation
      XMLSlideShow ppt = new XMLSlideShow();

     //getting the slide master object
      XSLFSlideMaster slideMaster = ppt.getSlideMasters()[0];

     //select a layout from specified list
      XSLFSlideLayout slidelayout = slideMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);

     //creating a slide with title and content layout
      XSLFSlide slide = ppt.createSlide(slidelayout);

     //selection of title place holder
      XSLFTextShape body = slide.getPlaceholder(1);

     //clear the existing text in the slide
      body.clearText();

     //adding new paragraph
      XSLFTextParagraph paragraph = body.addNewTextParagraph();

     //formatting line 1

      XSLFTextRun run1 = paragraph.addNewTextRun();
      run1.setText("This is a colored line");

     //setting color to the text
      run1.setFontColor(java.awt.Color.red);

     //setting font size to the text
      run1.setFontSize(24);

     //moving to the next line
      paragraph.addLineBreak();

     //formatting line 2

      XSLFTextRun run2 = paragraph.addNewTextRun();
      run2.setText("This is a bold line");
      run2.setFontColor(java.awt.Color.CYAN);

     //making the text bold
      run2.setBold(true);
      paragraph.addLineBreak();

     //formatting line 3

      XSLFTextRun run3 = paragraph.addNewTextRun();
      run3.setText(" This is a striked line");
      run3.setFontSize(12);

     //making the text italic
      run3.setItalic(true);

     //strike through the text
      run3.setStrikethrough(true);
      paragraph.addLineBreak();

     //formatting line 4

      XSLFTextRun run4 = paragraph.addNewTextRun();
      run4.setText(" This an underlined line");
      run4.setUnderline(true);

     //underlining the text
      paragraph.addLineBreak();

     //creating a file object
      File file = new File(“TextFormat.pptx”);
      FileOutputStream out = new FileOutputStream(file);

     //saving the changes to a file
      ppt.write(out);
      out.close();
   }
}

上記のコードを TextFormating.java として保存し、コマンドプロンプトから次のようにコンパイルして実行します-

$javac TextFormating.java
$java TextFormating

次の出力を生成するためにコンパイルおよび実行されます-

Formatting completed successfully

フォーマットされたテキストを含むスライドは次のように表示されます-

テキストのフォーマット