3

I am working on a program that requires writing results to a .docx in a table format. I need to write images from a folder in some of the table cells, which I have been unable to figure out how to do. The solutions I have seen are to insert the image in the document.

Student student = new Student ();
XWPFDocument Document = new XWPFDocument ();
XWPFTable table = document.createTable(20,2);
XWPFTableRow rows =null;
XWPFTableCell cell = null;

for (int i=0; i<20; i++) {
   rows = table.getRow(i)
   student = studentList.get(i);

   //first column
   cell = rows.getCell(0);
   //add image to this cell. The path to the image can be gotten from student.getImagePath and the image itself in BufferedImage can be gotten from student.getImage

   //second column
   cell = rows.getCell(1);
   cell.setText(student.getDetails);
}
Hajo
  • 121
  • 2
  • 14
  • You question is very vague. You said "I am working on a program ... "; providing the details of that program might help answer your question. – o-90 Oct 07 '15 at 07:14
  • Post some code of what you have done so far and you are more likely to attract good answers. Also, please read tag descriptions before you add tags. There might be important information on how they should be used (or not used). – Anders Oct 07 '15 at 07:17
  • Create the Table Cell, create a Paragraph in there, create a Run in that, then add the Picture to that Run just as you would any other? What's special about this being a table that's causing problems? – Gagravarr Oct 07 '15 at 08:27
  • @Gagravarr that worked fine. Thanks a lot. However, the image inserted gets automatically increased in size. How do i prevent that from happening because i want the image dimensions to be unchanged – Hajo Oct 07 '15 at 14:43
  • @Gagravarr i was able to figure out what i did wrong. Thanks – Hajo Oct 07 '15 at 16:35
  • You should post your working code as an answer then, to help others facing the same problem – Gagravarr Oct 07 '15 at 16:54

1 Answers1

4

I was able to sort it out. Below is the working code:

Student student = new Student ();
String [] details = {"Detail1: ", "Detail2: ", "Detail3: ", "Detail4: ", "Detail5: "};
FileInputStream fis;
String imageName;
int index;
File file;

XWPFDocument document = new XWPFDocument(); //create table
XWPFTable table;
XWPFParagraph paragraph;
XWPFRun run;
XWPFTableRow rows = null;
XWPFTableCell cell = null;

//Write Hall Name at the top of the word document
paragraph = document.createParagraph();
run = paragraph.createRun();
run.setUnderline(UnderlinePatterns.WORDS);
run.setText("Hall Name: " + hallName);
run.addBreak();

table = document.createTable(rw, cl);
table.getCTTbl().getTblPr().unsetTblBorders();

for (int i=0; i<rw; i++) {
    rows = table.getRow(i);
    student = studentList.get(i);

    //First column
    cell = rows.getCell(0);
    paragraph = cell.addParagraph();
    run = paragraph.createRun();
    if (student.getImagePath() == null) {
        run.setText("Image Unavailable");
    } 
    else {
        fis = new FileInputStream(student.getImagePath());
        index = student.getImagePath().lastIndexOf('\\') + 1;
        imageName = student.getImagePath().substring(index);
        run.addPicture(fis, XWPFDocument.PICTURE_TYPE_JPEG, imageName, Units.toEMU(100), Units.toEMU(100));
    }

    //Second column
    cell = rows.getCell(1);
    paragraph = cell.addParagraph();
    run = paragraph.createRun();
    run.setText(details[0] + student.getRegNumber());
    run.addBreak();
    run.setText(details[1] + student.getName());
    run.addBreak();
    run.setText(details[2] + student.getExamNumber());
    run.addBreak();
    run.setText(details[3] + student.getTableNumber());
    run.addBreak();
    run.setText(details[4] + student.getLevel());
}
Hajo
  • 121
  • 2
  • 14