Apache POI Word - Police et alignement

Ce chapitre explique comment appliquer différents styles de police et alignements dans un document Word à l'aide de Java. Généralement, le style de police contient: la taille de la police, le type, le gras, l'italique et le soulignement. Et l'alignement est catégorisé en gauche, centre, droite et justifier.

Le style de police

Le code suivant est utilisé pour définir différents styles de police -

import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.VerticalAlign;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class FontStyle {

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

      //Blank Document
      XWPFDocument document = new XWPFDocument(); 
        
      //Write the Document in file system
      FileOutputStream out = new FileOutputStream(new File("fontstyle.docx"));
        
      //create paragraph
      XWPFParagraph paragraph = document.createParagraph();
        
      //Set Bold an Italic
      XWPFRun paragraphOneRunOne = paragraph.createRun();
      paragraphOneRunOne.setBold(true);
      paragraphOneRunOne.setItalic(true);
      paragraphOneRunOne.setText("Font Style");
      paragraphOneRunOne.addBreak();
        
      //Set text Position
      XWPFRun paragraphOneRunTwo = paragraph.createRun();
      paragraphOneRunTwo.setText("Font Style two");
      paragraphOneRunTwo.setTextPosition(100);
 
      //Set Strike through and Font Size and Subscript
      XWPFRun paragraphOneRunThree = paragraph.createRun();
      paragraphOneRunThree.setStrike(true);
      paragraphOneRunThree.setFontSize(20);
      paragraphOneRunThree.setSubscript(VerticalAlign.SUBSCRIPT);
      paragraphOneRunThree.setText(" Different Font Styles");
        
      document.write(out);
      out.close();
      System.out.println("fontstyle.docx written successully");
   }
}

Enregistrez le code ci-dessus sous FontStyle.java puis compilez et exécutez-le à partir de l'invite de commande comme suit -

$javac FontStyle.java
$java FontStyle

Il générera un fichier Word nommé fontstyle.docx dans votre répertoire actuel et affichez la sortie suivante sur l'invite de commande -

fontstyle.docx written successfully

le fontstyle.docx Le fichier se présente comme suit.

Alignement

Le code suivant est utilisé pour définir l'alignement sur le texte du paragraphe -

import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class AlignParagraph {

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

      //Blank Document
      XWPFDocument document = new XWPFDocument(); 
        
      //Write the Document in file system
      FileOutputStream out = new FileOutputStream(
      new File("alignparagraph.docx"));
        
      //create paragraph
      XWPFParagraph paragraph = document.createParagraph();
        
      //Set alignment paragraph to RIGHT
      paragraph.setAlignment(ParagraphAlignment.RIGHT);
      XWPFRun run = paragraph.createRun();
      run.setText("At tutorialspoint.com, we strive hard to " +
         "provide quality tutorials for self-learning " +
         "purpose in the domains of Academics, Information " +
         "Technology, Management and Computer Programming " +
         "Languages.");
        
      //Create Another paragraph
      paragraph = document.createParagraph();
        
      //Set alignment paragraph to CENTER
      paragraph.setAlignment(ParagraphAlignment.CENTER);
      run = paragraph.createRun();
      run.setText("The endeavour started by Mohtashim, an AMU " +
         "alumni, who is the founder and the managing director " +
         "of Tutorials Point (I) Pvt. Ltd. He came up with the " +
         "website tutorialspoint.com in year 2006 with the help" +
         "of handpicked freelancers, with an array of tutorials" +
         " for computer programming languages. ");
			
      document.write(out);
      out.close();
      System.out.println("alignparagraph.docx written successfully");
   }
}

Enregistrez le code ci-dessus sous AlignParagraph.java puis compilez et exécutez-le à partir de l'invite de commande comme suit -

$javac AlignParagraph.java
$java AlignParagraph

Il générera un fichier Word nommé alignparagraph.docx dans votre répertoire actuel et affichez la sortie suivante dans l'invite de commande -

alignparagraph.docx written successfully

le alignparagraph.docx le fichier ressemble à ceci -