Example usage for org.apache.poi.xwpf.usermodel XWPFParagraph getRuns

List of usage examples for org.apache.poi.xwpf.usermodel XWPFParagraph getRuns

Introduction

In this page you can find the example usage for org.apache.poi.xwpf.usermodel XWPFParagraph getRuns.

Prototype

public List<XWPFRun> getRuns() 

Source Link

Usage

From source file:service.GenerationLettres.CreerPiecesManquantes.java

/**
* 
* @param filename - Nom du fichier modle de demande des pices manquantes.
* @param idDossier - Identifiant du dossier pour lequel l est cr
* @throws InvalidFormatException/*from  w w  w . jav  a2 s  .com*/
* @throws IOException 
*/
public void replacePiecesManquantes(String filename, Formation formation, String sexe, String nom,
        String prenom, String adresse, Adresse adresseEntite, List<Justificatif> justificatifsOk)
        throws InvalidFormatException, IOException {
    List<Justificatif> lesJustificatifs = formation.getLesJustificatifs();

    Date dateActuelle = new Date();
    DateFormat dateForm = new SimpleDateFormat("dd MMMM yyyy", Locale.FRANCE);
    String date = dateForm.format(dateActuelle);
    String codePostal = adresseEntite.getCodePostal();
    String ville = adresseEntite.getVille();
    String civilite = "";
    if (sexe.equals("Masculin"))
        civilite = "Monsieur";
    if (sexe.equals("Feminin"))
        civilite = "Madame";

    String intitule = formation.getIntitule();

    if (justificatifsOk != null) {
        for (Justificatif just : justificatifsOk) {
            lesJustificatifs.remove(just);
        }
    }

    String newFileName = nom + prenom + " Lettre piecesManquantes.docx";

    File file = new File(PATH_MODELS + "/" + filename);
    FileInputStream fis = new FileInputStream(file.getAbsolutePath());
    XWPFDocument doc = new XWPFDocument(fis);
    doc.write(new FileOutputStream(PATH_TARGET + "/" + newFileName));
    doc.close();

    doc = new XWPFDocument(OPCPackage.open(PATH_TARGET + "/" + newFileName));

    for (XWPFParagraph p : doc.getParagraphs()) {
        int numberOfRuns = p.getRuns().size();
        StringBuilder sb = new StringBuilder();
        for (XWPFRun r : p.getRuns()) {
            int pos = r.getTextPosition();
            if (r.getText(pos) != null) {
                sb.append(r.getText(pos));
            }
        }
        if (sb.length() > 0 && sb.toString().contains("$formation")) {
            for (int i = numberOfRuns - 1; i > 0; i--) {
                p.removeRun(i);
            }
            String text = sb.toString().replace("$formation", intitule);
            XWPFRun run = p.getRuns().get(0);
            run.setText(text, 0);
            System.out.println("Changement de la formation effectue");
        }
    }
    for (XWPFParagraph p : doc.getParagraphs()) {
        int numberOfRuns = p.getRuns().size();
        StringBuilder sb = new StringBuilder();
        for (XWPFRun r : p.getRuns()) {
            int pos = r.getTextPosition();
            if (r.getText(pos) != null) {
                sb.append(r.getText(pos));
            }
        }
        if (sb.length() > 0 && sb.toString().contains("$date")) {
            for (int i = numberOfRuns - 1; i > 0; i--) {
                p.removeRun(i);
            }
            String text = sb.toString().replace("$date", date);
            XWPFRun run = p.getRuns().get(0);
            run.setText(text, 0);
            System.out.println("Changement de la date effectue");
        }
    }
    for (XWPFParagraph p : doc.getParagraphs()) {
        int numberOfRuns = p.getRuns().size();
        StringBuilder sb = new StringBuilder();
        for (XWPFRun r : p.getRuns()) {
            int pos = r.getTextPosition();
            if (r.getText(pos) != null) {
                sb.append(r.getText(pos));
            }
        }
        if (sb.length() > 0 && sb.toString().contains("$civilite")) {
            for (int i = numberOfRuns - 1; i > 0; i--) {
                p.removeRun(i);
            }
            String text = sb.toString().replace("$civilite", civilite);
            XWPFRun run = p.getRuns().get(0);
            run.setText(text, 0);
            System.out.println("Changement de la civilite effectue");
        }
    }
    for (XWPFParagraph p : doc.getParagraphs()) {
        int numberOfRuns = p.getRuns().size();
        StringBuilder sb = new StringBuilder();
        for (XWPFRun r : p.getRuns()) {
            int pos = r.getTextPosition();
            if (r.getText(pos) != null) {
                sb.append(r.getText(pos));
            }
        }
        if (sb.length() > 0 && sb.toString().contains("$prenom")) {
            for (int i = numberOfRuns - 1; i > 0; i--) {
                p.removeRun(i);
            }
            String text = sb.toString().replace("$prenom", prenom);
            XWPFRun run = p.getRuns().get(0);
            run.setText(text, 0);
            System.out.println("Changement du prenom effectue");
        }
    }
    for (XWPFParagraph p : doc.getParagraphs()) {
        int numberOfRuns = p.getRuns().size();
        StringBuilder sb = new StringBuilder();
        for (XWPFRun r : p.getRuns()) {
            int pos = r.getTextPosition();
            if (r.getText(pos) != null) {
                sb.append(r.getText(pos));
            }
        }
        if (sb.length() > 0 && sb.toString().contains("$nom")) {
            for (int i = numberOfRuns - 1; i > 0; i--) {
                p.removeRun(i);
            }
            String text = sb.toString().replace("$nom", nom);
            XWPFRun run = p.getRuns().get(0);
            run.setText(text, 0);
            System.out.println("Changement du nom effectue");
        }
    }
    for (XWPFParagraph p : doc.getParagraphs()) {
        int numberOfRuns = p.getRuns().size();
        StringBuilder sb = new StringBuilder();
        for (XWPFRun r : p.getRuns()) {
            int pos = r.getTextPosition();
            if (r.getText(pos) != null) {
                sb.append(r.getText(pos));
            }
        }
        if (sb.length() > 0 && sb.toString().contains("$adresse")) {
            for (int i = numberOfRuns - 1; i > 0; i--) {
                p.removeRun(i);
            }
            String text = sb.toString().replace("$adresse", adresse);
            XWPFRun run = p.getRuns().get(0);
            run.setText(text, 0);
            System.out.println("Changement de l'adresse effectue");
        }
    }
    for (XWPFParagraph p : doc.getParagraphs()) {
        int numberOfRuns = p.getRuns().size();
        StringBuilder sb = new StringBuilder();
        for (XWPFRun r : p.getRuns()) {
            int pos = r.getTextPosition();
            if (r.getText(pos) != null) {
                sb.append(r.getText(pos));
            }
        }
        if (sb.length() > 0 && sb.toString().contains("$codePostal")) {
            for (int i = numberOfRuns - 1; i > 0; i--) {
                p.removeRun(i);
            }
            String text = sb.toString().replace("$codePostal", codePostal);
            XWPFRun run = p.getRuns().get(0);
            run.setText(text, 0);
            System.out.println("Changement du code postal effectue");
        }
    }
    for (XWPFParagraph p : doc.getParagraphs()) {
        int numberOfRuns = p.getRuns().size();
        StringBuilder sb = new StringBuilder();
        for (XWPFRun r : p.getRuns()) {
            int pos = r.getTextPosition();
            if (r.getText(pos) != null) {
                sb.append(r.getText(pos));
            }
        }
        if (sb.length() > 0 && sb.toString().contains("$ville")) {
            for (int i = numberOfRuns - 1; i > 0; i--) {
                p.removeRun(i);
            }
            String text = sb.toString().replace("$ville", ville);
            XWPFRun run = p.getRuns().get(0);
            run.setText(text, 0);
            System.out.println("Changement de la ville effectue");
        }
    }

    XWPFTable table = doc.createTable(lesJustificatifs.size(), 2);
    table.setCellMargins(200, 250, 0, 250);
    int i = 0;
    for (XWPFTableRow r : table.getRows()) {
        XWPFTableCell cell = r.getCell(0);
        cell.setText(lesJustificatifs.get(i).getTitre());
        cell = r.getCell(1);
        cell.setText(lesJustificatifs.get(i).getDescription());
        i++;
    }

    doc.write(new FileOutputStream(PATH_TARGET + "/temp.docx"));
    new File(PATH_TARGET + "/temp.docx").delete();
    doc.close();
    //copyTempToFile(filename);
    System.out.println("replaceLettrePiecesManquantes DONE");
}