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 90 91 92
|
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigInteger;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFHeader;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
public class CreerDocument
{
public static void main(String[] args)throws Exception
{
////////////////////////////////////////////////////////////////////////////////////////////////// créer un nouveau document
XWPFDocument document= new XWPFDocument();
//créer un flux d'écriture pour enregistrer contenu dans nouveaudoc.docx
FileOutputStream out = new FileOutputStream(
new File("nouveaudoc.docx"));
///////////////////////////////////////////////////////////////////////////////////////////// on s'occupe des marges d'impression
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr(); // visiblement je créée une section dans mon document????
CTPageMar pageMar = sectPr.addNewPgMar(); // je créé des marges pour ma section
pageMar.setLeft(BigInteger.valueOf(360));
pageMar.setTop(BigInteger.valueOf(720));
pageMar.setRight(BigInteger.valueOf(720));
pageMar.setBottom(BigInteger.valueOf(720));
////////////////////////////////////////////////////////////////////////////////////////////////// création de l'entête
// la classe HeaderFooterPolicy est celle qui me permet de créer une entête et un pied de page
XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr);
// on créé notre entete
XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT); //il y a FIRST et EVEN mais qu'est ce que ça veut dire?
//je créé un paragraphe dans mon entête
XWPFParagraph paragraphe = header.createParagraph();
paragraphe.setAlignment(ParagraphAlignment.LEFT); // je l'aligne à gauche
// un XWPFRun c'est un flux de texte qui va etre écrit à l'exécution. Sur ce run (cette exécution) propre au paragraphe on va appliquer des règles
// éditer du texte, le formater etc.
XWPFRun run = paragraphe.createRun();
run.setText("Monsieur X \n \r Aspirant developeur \r 1, Rue du Code 00404 Error Cedex \r");
run.setBold(true);
run.setFontFamily("Arial");
run.setFontSize(9);
// sur ce run j'aimerai qu'il y ait une image d'insérée
String monLogo ="logo.png";
// la méthode addPicture comprends 4 paramètres => un flux d'entrée, le type d'image, l'adresse de mon image, la largeur, la hauteur)
run.addPicture (new FileInputStream((monLogo)),XWPFDocument.PICTURE_TYPE_PNG, monLogo,Units.toEMU (101),Units.toEMU (60));
XWPFTable tableau = document.createTable(70, 5);
//première ligne
XWPFTableRow tableRowOne = tableau.getRow(0);
tableRowOne.getCell(0).setText("un");
tableRowOne.getCell(1).setText("deux");
tableRowOne.getCell(2).setText("trois");
tableRowOne.getCell(3).setText("quatre");
tableRowOne.getCell(4).setText("cinq");
XWPFTableRow tableRowTwo = tableau.getRow(1);
tableRowTwo.getCell(0).setText("1");
tableRowTwo.getCell(1).setText("2");
tableRowTwo.getCell(2).setText("3");
tableRowTwo.getCell(3).setText("4");
tableRowTwo.getCell(4).setText("5");
//mettre à jour le fichier nouveaudoc.docx
document.write(out);
//fermer le flux d'écriture
out.close();
System.out.println("les modifications ont été faites avec succès");
}
} |
Partager