Word file generated by Java gets "hresult 0x80070057" error when an image is added

0

I'm generating a Word file with ApachePOI and I need to load an image on it.

I'm actually doing this :

XWPFDocument document = new XWPFDocument(new FileInputStream("myTemplate.docx")); 
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();  

String imgFile="myImage.jpg";
run.addPicture(new FileInputStream(imgFile), XWPFDocument.PICTURE_TYPE_JPEG, imgFile, Units.toEMU(50), Units.toEMU(50));

Unfortunately, I can't open the word file that is generated. I get "hresult 0x80070057" error as follows:

Image of the error

If I remove the piece of code, I can read my file that contains only text.

How can I add an image using ApachePOI in a Word file and be able to open it?

Full code :

public class FCGenerator {

  public static InputStreamResource createFC(CandidatDTO candidat) throws IOException, InvalidFormatException, XmlException {

      //CREATION DU DOCUMENT
      XWPFDocument document = new XWPFDocument(new FileInputStream("template.docx")); 
      List<FicheCompetenceDTO> competencesCandidat = candidat.getFichesCompetence();

      // PARAGRAPHE PRESENTATION
      XWPFParagraph presentation = document.createParagraph();

      // RUN NOM
      XWPFRun runNom = presentation.createRun();

      // NOM  
      runNom.setFontFamily("Verdana");
      runNom.setText(candidat.getNom());
      runNom.setColor("005091");
      runNom.setFontSize(22);
      runNom.setBold(true);
      presentation.setAlignment(ParagraphAlignment.RIGHT);
      runNom.addBreak();

      // STYLE TITRE COMP
      XWPFRun runCompTech = presentation.createRun();
      runCompTech.setFontFamily("Verdana");
      runCompTech.setColor("404040");
      runCompTech.setFontSize(20);
      runCompTech.setBold(true);
      runCompTech.setText(competencesCandidat.get(0).getDomaines());
      runCompTech.setText(competencesCandidat.get(0).getCompetences());
      runCompTech.addBreak();
      runCompTech.addBreak();

      // PARAGRAPHE COMPETENCE TECHNIQUE
      XWPFParagraph domComp = document.createParagraph();
      domComp.setAlignment(ParagraphAlignment.LEFT);

      // STYLE TITRE DOMAINE DE COMPETENCE
      XWPFRun styleTitredomComp = domComp.createRun();
      styleTitredomComp.setFontFamily("Verdana");
      styleTitredomComp.setColor("005091");
      styleTitredomComp.setFontSize(12);
      styleTitredomComp.setBold(true);
      styleTitredomComp.setUnderline(UnderlinePatterns.SINGLE);
      styleTitredomComp.setText("Domaines de compétences                                                                _");
      styleTitredomComp.addBreak();
      styleTitredomComp.addBreak();

      // DOMAINE DE COMPETENCES
      XWPFRun styleCompInfo = domComp.createRun();
      CTR styleCompInfoCtr = styleCompInfo.getCTR();
      styleCompInfo.setFontFamily("Verdana");
      styleCompInfo.setColor("005091");
      styleCompInfo.setFontSize(10);
      styleCompInfo.setBold(true);
      styleCompInfoCtr.addNewTab();
      styleCompInfo.setText("• Programmation : ");
      styleCompInfo.addBreak();
      styleCompInfoCtr.addNewTab();
      styleCompInfo.setText("• Framework : ");
      styleCompInfo.addBreak();
      styleCompInfoCtr.addNewTab();
      styleCompInfo.setText("• Base de données : ");
      styleCompInfo.addBreak();
      styleCompInfoCtr.addNewTab();
      styleCompInfo.setText("• Outils : ");
      styleCompInfo.addBreak();
      styleCompInfoCtr.addNewTab();
      styleCompInfo.setText("• Ide : ");
      styleCompInfo.addBreak();
      styleCompInfoCtr.addNewTab();
      styleCompInfo.setText("• Divers : ");
      styleCompInfo.addBreak();
      styleCompInfo.addBreak();

      // STYLE TITRE DOMAINE DE COMPETENCE
      XWPFRun styleTitre = domComp.createRun();
      styleTitre.setFontFamily("Verdana");
      styleTitre.setColor("005091");
      styleTitre.setFontSize(12);
      styleTitre.setBold(true);
      styleTitre.setUnderline(UnderlinePatterns.SINGLE);
      styleTitre.setText("Formation                                                                                           _");
      styleTitre.addBreak();

      // STYLE DES LIGNES DE FORMATIONS
      XWPFRun styleFormationInfo = domComp.createRun();
      styleFormationInfo.setFontFamily("Verdana");
      styleFormationInfo.setColor("000000");
      styleFormationInfo.setFontSize(10);
      styleFormationInfo.setBold(true);

      List<FormationDTO> formationCandidat = candidat.getFichesCompetence().get(0).getFormations();
      for (FormationDTO formation : formationCandidat) {

              XWPFTable tableFormation = document.createTable(1, 2);
              List<XWPFTableRow> rowsForm = tableFormation.getRows();
              tableFormation.setCellMargins(20,20, 0,300);


              CTTblPr tblproForm = tableFormation.getCTTbl().getTblPr();
              CTTblBorders borders = tblproForm.addNewTblBorders();
              borders.addNewBottom().setVal(STBorder.NONE); 
              borders.addNewLeft().setVal(STBorder.NONE);
              borders.addNewRight().setVal(STBorder.NONE);
              borders.addNewTop().setVal(STBorder.NONE);
              //also inner borders
              borders.addNewInsideH().setVal(STBorder.NONE);
              borders.addNewInsideV().setVal(STBorder.NONE);
          for (XWPFTableRow row : rowsForm) {
              int colCts = 0;
              int rowCts = 0;

              // get the cells in this row
              List<XWPFTableCell> cells = row.getTableCells();
              // add content to each cell
              for (XWPFTableCell cell : cells) {
                  // get a table cell properties element (tcPr)
                  CTTcPr tcpr = cell.getCTTc().addNewTcPr();
                  // set vertical alignment to "center"
                  CTVerticalJc va = tcpr.addNewVAlign();
                  va.setVal(STVerticalJc.TOP);

                  // get 1st paragraph in cell's paragraph list
                  XWPFParagraph paraInfo = cell.getParagraphs().get(0);
                  // create a run to contain the content
                  XWPFRun rhInfo = paraInfo.createRun();

                  // style cell as desired
                  if (colCts == 0) {
                      rhInfo.setText(formation.getLibelle());
                      rhInfo.setFontFamily("Verdana");
                      rhInfo.setColor("005091");
                      rhInfo.setFontSize(10);
                      rhInfo.setBold(true);
                      paraInfo.setAlignment(ParagraphAlignment.LEFT);
                  } else {                  
                      // header row
                      rhInfo.setText(formation.getAnnees());
                      rhInfo.setFontFamily("Verdana");
                      rhInfo.setFontSize(10);
                      rhInfo.setBold(true);
                      paraInfo.setAlignment(ParagraphAlignment.LEFT);

                  } 

                  colCts++;
              } // for cell
              colCts = 0;
              rowCts++;
          }
      }

      // STYLE TITRE EXPERIENCES PROFESSIONNELLES
      XWPFParagraph paraExpPro = document.createParagraph();
      XWPFRun styleTitreExpePro = paraExpPro.createRun();
      styleTitreExpePro.setFontFamily("Verdana");
      styleTitreExpePro.setColor("005091");
      styleTitreExpePro.setFontSize(12);
      styleTitreExpePro.setBold(true);
      styleTitreExpePro.setUnderline(UnderlinePatterns.SINGLE);
      styleTitreExpePro.setText("Experiences professionnelles                                                            _");
      styleTitreExpePro.addBreak();

      // STYLE DES LIGNES DE FORMATIONS
      XWPFRun styleFormationForm = paraExpPro.createRun();
      styleFormationForm.setFontFamily("Verdana");
      styleFormationForm.setColor("000000");
      styleFormationForm.setFontSize(10);
      styleFormationForm.setBold(true);

     int j = 0;

      List<ExperienceDTO> experiencesCandidat = candidat.getFichesCompetence().get(0).getExperiences();

      int nRows = 2;
      int nCols = 2;

      // Compteurs de lignes / colones
      int rowCt = 0;
      int colCt = 0;
      for (ExperienceDTO experience : experiencesCandidat) {
          XWPFTable table = document.createTable(nRows, nCols);
          List<XWPFTableRow> rows = table.getRows();
          table.setCellMargins(20,20, 0,300);
      for (XWPFTableRow row : rows) {

          // get the cells in this row
          List<XWPFTableCell> cells = row.getTableCells();
          // add content to each cell
          for (XWPFTableCell cell : cells) {
              // get a table cell properties element (tcPr)
              CTTcPr tcpr = cell.getCTTc().addNewTcPr();
              // set vertical alignment to "center"
              CTVerticalJc va = tcpr.addNewVAlign();
              va.setVal(STVerticalJc.TOP);

              // get 1st paragraph in cell's paragraph list
              XWPFParagraph para = cell.getParagraphs().get(0);
              // create a run to contain the content
              XWPFRun rh = para.createRun();

              // style cell as desired
              if (colCt == 0 && rowCt % 2 == 0) {
                  rh.setText(experience.getDateDebut() + " au " + experience.getDateFin());
                  rh.setFontFamily("Verdana");
                  rh.setColor("005091");
                  rh.setUnderline(UnderlinePatterns.SINGLE);
                  rh.setFontSize(10);
                  rh.setBold(true);
                  para.setAlignment(ParagraphAlignment.LEFT);
              }
              if (colCt != 0 && rowCt % 2 == 0) {
                  // header row
                  rh.setText(experience.getClient());
                  rh.setFontFamily("Verdana");
                  rh.setColor("005091");
                  rh.setUnderline(UnderlinePatterns.SINGLE);
                  rh.setFontSize(10);
                  rh.setBold(true);
                  para.setAlignment(ParagraphAlignment.LEFT);

              } 
              if (colCt == 0 && rowCt % 2 != 0){
                  // other rows
                  rh.setText("Poste : ");
                  rh.setText(experience.getPoste());
                  rh.setFontFamily("Verdana");
                  rh.setFontSize(10);
                  rh.setBold(true);
                  para.setAlignment(ParagraphAlignment.LEFT);
              }
              if (colCt != 0 && rowCt % 2 != 0){
                  XWPFRun proj = para.createRun();
                  XWPFRun projCont = para.createRun();
                  XWPFRun miss = para.createRun();
                  XWPFRun missCont = para.createRun();
                  XWPFRun envTech = para.createRun();
                  XWPFRun envTechCont = para.createRun();


                  proj.setFontFamily("Verdana");
                  proj.setFontSize(10);

                  proj.setBold(true);
                  proj.setText("Projet : ");
                  proj.addBreak();

                  projCont.setBold(false);
                  projCont.setText(experience.getProjet());
                  projCont.addBreak();
                  projCont.addBreak();

                  miss.setBold(true);
                  miss.setText("Missions : ");
                  miss.addBreak();

                  for (String  mission : experience.getMissions()) {

                      missCont.setText("    •  ");
                      missCont.setText(mission);
                      missCont.addBreak();

                  }

                  envTech.setBold(true);
                  envTech.setText("Environnement technique : ");
                  envTech.addBreak();
                  envTech.addBreak();

                  for (String  env : experience.getEnvironnements()) {

                      envTechCont.setText("    •  ");
                      envTechCont.setText(env);
                      envTechCont.addBreak();
                  }

                  para.setAlignment(ParagraphAlignment.LEFT);
              }
              colCt++;
          } // for cell
          colCt = 0;
          rowCt++;
      } // for row

      CTTblPr tblpro = table.getCTTbl().getTblPr();
      CTTblBorders borders = tblpro.addNewTblBorders();
      borders.addNewBottom().setVal(STBorder.NONE); 
      borders.addNewLeft().setVal(STBorder.NONE);
      borders.addNewRight().setVal(STBorder.NONE);
      borders.addNewTop().setVal(STBorder.NONE);
      //also inner borders
      borders.addNewInsideH().setVal(STBorder.NONE);
      borders.addNewInsideV().setVal(STBorder.NONE);

      XWPFParagraph paragraph = document.createParagraph();
      XWPFRun run=paragraph.createRun();  

      String imgFile="myImage.jpg";
      run.addPicture(new FileInputStream(imgFile), XWPFDocument.PICTURE_TYPE_JPEG, imgFile, Units.toEMU(50), Units.toEMU(50));


      }

      //Write the Document in file system
      ByteArrayOutputStream out = new ByteArrayOutputStream();    
      document.write(out);
      out.close();
      System.out.println("create document written successully");
      InputStreamResource inputStream = new InputStreamResource(new ByteArrayInputStream(out.toByteArray()));
      return inputStream;   
      }
} 
java
apache-poi
xwpf
asked on Stack Overflow Sep 6, 2019 by Doby • edited Sep 6, 2019 by Doby

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0