Example usage for org.apache.poi.xwpf.usermodel XWPFRun addPicture

List of usage examples for org.apache.poi.xwpf.usermodel XWPFRun addPicture

Introduction

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

Prototype

public XWPFPicture addPicture(InputStream pictureData, int pictureType, String filename, int width, int height)
        throws InvalidFormatException, IOException 

Source Link

Document

Adds a picture to the run.

Usage

From source file:SimpleImages.java

License:Apache License

public static void main(String[] args) throws Exception {
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p = doc.createParagraph();

    XWPFRun r = p.createRun();

    for (String imgFile : args) {
        int format;

        if (imgFile.endsWith(".emf"))
            format = XWPFDocument.PICTURE_TYPE_EMF;
        else if (imgFile.endsWith(".wmf"))
            format = XWPFDocument.PICTURE_TYPE_WMF;
        else if (imgFile.endsWith(".pict"))
            format = XWPFDocument.PICTURE_TYPE_PICT;
        else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg"))
            format = XWPFDocument.PICTURE_TYPE_JPEG;
        else if (imgFile.endsWith(".png"))
            format = XWPFDocument.PICTURE_TYPE_PNG;
        else if (imgFile.endsWith(".dib"))
            format = XWPFDocument.PICTURE_TYPE_DIB;
        else if (imgFile.endsWith(".gif"))
            format = XWPFDocument.PICTURE_TYPE_GIF;
        else if (imgFile.endsWith(".tiff"))
            format = XWPFDocument.PICTURE_TYPE_TIFF;
        else if (imgFile.endsWith(".eps"))
            format = XWPFDocument.PICTURE_TYPE_EPS;
        else if (imgFile.endsWith(".bmp"))
            format = XWPFDocument.PICTURE_TYPE_BMP;
        else if (imgFile.endsWith(".wpg"))
            format = XWPFDocument.PICTURE_TYPE_WPG;
        else {//from w w  w  . ja  v a2 s.  c  om
            System.err.println("Unsupported picture: " + imgFile
                    + ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
            continue;
        }

        r.setText(imgFile);
        r.addBreak();
        r.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels
    }

    FileOutputStream out = new FileOutputStream("Word.docx");
    doc.write(out);
    out.close();
}

From source file:com.Base.JavaApplication1.java

public static void save(String path) {

    try {//from   w  ww . jav  a  2  s .c om

        // JOptionPane.showMessageDialog(null, "Start ImageDoc","Error",JOptionPane.INFORMATION_MESSAGE);
        //String comment=comment;
        XWPFDocument docx = new XWPFDocument();

        //  JOptionPane.showMessageDialog(null, "create ImageDoc","Error",JOptionPane.INFORMATION_MESSAGE);

        XWPFParagraph par = docx.createParagraph();
        XWPFRun run = par.createRun();
        run.setText(comment);
        run.setFontSize(13);

        FileOutputStream out = new FileOutputStream(path);

        //JOptionPane.showMessageDialog(null, "Dir"+dir+"\\"+name,"Error",JOptionPane.INFORMATION_MESSAGE);
        // byte [] picbytes = IOUtils.toByteArray(pic);

        // run.setText("Software Genrated File");

        File f = new File("C:\\images\\*.*");
        String str = f.getParent();
        File k = new File(str);
        String list[] = k.list();

        JOptionPane.showMessageDialog(null, "Reading Files", "Error", JOptionPane.INFORMATION_MESSAGE);
        for (int y = 0; y < list.length; y++) {
            InputStream pic = new FileInputStream("C:\\images\\" + list[y]);
            XWPFRun run2 = par.createRun();
            // run2.setText("Screen Shot No."+(y+1));

            run2.addPicture(pic, Document.PICTURE_TYPE_JPEG, "C:\\images\\" + list[y], Units.toEMU(500),
                    Units.toEMU(350));
            // XWPFRun run2 = title.createRun();
            run2.setText("ScreenShot :" + (y + 1));
            pic.close();
            System.gc();
        }

        //run.setText("Hello, World. This is my first java generated docx-file. Have fun2shhhh.");

        // FileInputStream pic2 = new FileInputStream("D:\\i.png");
        //byte [] picbytes2 = IOUtils.toByteArray(pic);
        //docx.addPictureData(IOUtils.toByteArray(pic2), Document.PICTURE_TYPE_PNG);

        //  docx.createParagraph().createRun().addPicture(pic, Document.PICTURE_TYPE_JPEG, "D:\\images\\image0.jpg", Units.toEMU(500), Units.toEMU(500));

        XWPFParagraph title = docx.createParagraph();
        XWPFRun run2 = title.createRun();
        run2.setText(" ");
        run2.setBold(true);
        title.setAlignment(ParagraphAlignment.CENTER);

        JOptionPane.showMessageDialog(null, "Writing File", "Error", JOptionPane.INFORMATION_MESSAGE);

        System.out.println(dir + name);

        docx.write(out);

        out.close();
        //  pic.close();
        JOptionPane.showMessageDialog(null, "Operation Complete", "Error", JOptionPane.INFORMATION_MESSAGE);

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "No Image to save!" + e, "Error", JOptionPane.ERROR_MESSAGE);
    }

}

From source file:com.crimelab.service.GalleryServiceImpl.java

@Override
public XWPFDocument create(GalleryResults galleryResults, HttpSession session) {
    XWPFDocument document = null;/*ww w . j a  v  a  2 s  . co  m*/

    //Insert into database
    galleryDAO.insertResults(galleryResults);

    try {
        //Retrieving Template
        InputStream inpDocx = session.getServletContext()
                .getResourceAsStream("/WEB-INF/templates/CompositeSketch.docx");
        document = new XWPFDocument(inpDocx);

        //Adding the picture
        XWPFParagraph pictureHolder = document.createParagraph();
        XWPFRun pictureRun = pictureHolder.createRun();

        FileInputStream getPhoto = new FileInputStream(galleryResults.getPhotoLocation());
        FileInputStream getImage = new FileInputStream(galleryResults.getPhotoLocation());
        ImageInputStream imageInput = ImageIO.createImageInputStream(getPhoto);
        BufferedImage bi = ImageIO.read(imageInput);
        int width = bi.getWidth() - 100;
        int height = bi.getHeight() - 100;

        pictureRun.addBreak();
        pictureRun.addPicture(getImage, XWPFDocument.PICTURE_TYPE_PNG, null, Units.toEMU(width),
                Units.toEMU(height));

        pictureHolder.setBorderBottom(Borders.BASIC_BLACK_DASHES);
        pictureHolder.setBorderTop(Borders.BASIC_BLACK_DASHES);
        pictureHolder.setBorderLeft(Borders.BASIC_BLACK_DASHES);
        pictureHolder.setBorderRight(Borders.BASIC_BLACK_DASHES);
        pictureHolder.setAlignment(ParagraphAlignment.CENTER);
        //            pictureRowHolder.getCell(0).setText("IMAGE PLACER");

        //Case number and Date            
        XWPFParagraph info = document.createParagraph();
        XWPFRun caseNoAndDate = info.createRun();
        caseNoAndDate.setText("Case Number: " + galleryResults.getCaseNo());
        caseNoAndDate.addTab();
        caseNoAndDate.addTab();
        caseNoAndDate.addTab();
        caseNoAndDate.addTab();
        caseNoAndDate.setText(galleryResults.getDate());
        caseNoAndDate.setFontSize(16);

        //Sketch Details
        XWPFParagraph caseDetails = document.createParagraph();
        XWPFRun detailsParagraph = caseDetails.createRun();
        detailsParagraph.setText("Offense/Incident: " + galleryResults.getOffenseIncident());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Name/AKA: " + galleryResults.getNameAKA());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Sex: " + galleryResults.getSex());
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.setText("Age: " + galleryResults.getAge());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Height: " + galleryResults.getHeight());
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.setText("Weight: " + galleryResults.getWeight());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Built: " + galleryResults.getBuilt());
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.addTab();
        detailsParagraph.setText("Complexion: " + galleryResults.getComplexion());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Other Information: " + galleryResults.getOtherInfo());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Described by: " + galleryResults.getDescribedBy());
        detailsParagraph.addBreak();
        detailsParagraph.setText("Requesting party: " + galleryResults.getRequestingParty());

        //Details Borders
        caseDetails.setBorderBottom(Borders.BASIC_BLACK_DASHES);
        caseDetails.setBorderTop(Borders.BASIC_BLACK_DASHES);
        caseDetails.setBorderLeft(Borders.BASIC_BLACK_DASHES);
        caseDetails.setBorderRight(Borders.BASIC_BLACK_DASHES);
        caseDetails.setAlignment(ParagraphAlignment.LEFT);

        //Reference Paragraph
        XWPFParagraph outsideDetails = document.createParagraph();
        XWPFRun outsideDetailsRun = outsideDetails.createRun();
        outsideDetailsRun.addBreak();
        outsideDetailsRun.setText("Note: For reference");
        outsideDetailsRun.addBreak();
        outsideDetailsRun.setText("The witness indicates that this image is: " + galleryResults.getRating());
        getPhoto.close();
        getImage.close();
        imageInput.close();
        document.getXWPFDocument();
    } catch (IOException | InvalidFormatException e) {
        e.printStackTrace();
    }
    return document;
}

From source file:com.crimelab.service.PolygraphServiceImpl.java

@Override
public XWPFDocument create(Polygraph polygraph, HttpSession session) {
    XWPFDocument document = null;/*from  w w  w. j a v  a 2  s. c  om*/

    //Insert into dbase
    //        polygraphDAO.polygraphInfo(polygraph);
    try {
        //Blank Document
        InputStream inpDocx = session.getServletContext()
                .getResourceAsStream("/WEB-INF/templates/Polygraph.docx");
        document = new XWPFDocument(inpDocx);

        //create Paragraph
        XWPFParagraph subjectNo = document.createParagraph();
        XWPFRun r1 = subjectNo.createRun();
        r1.setText("POLYGRAPH SUBJECT NO: " + polygraph.getSubjectNo());
        subjectNo.setAlignment(ParagraphAlignment.CENTER);
        r1.setBold(true);
        ;

        //create table
        XWPFTable table = document.createTable();
        //width 
        CTTbl tableFix = table.getCTTbl();
        CTTblPr pr = tableFix.getTblPr();
        CTTblWidth tblW = pr.getTblW();
        tblW.setW(BigInteger.valueOf(4800));
        tblW.setType(STTblWidth.PCT);
        pr.setTblW(tblW);
        tableFix.setTblPr(pr);

        //create first row
        XWPFTableRow tableRowOne = table.getRow(0);
        XWPFTableCell headerCell = tableRowOne.getCell(0);
        XWPFParagraph headerParagraph = headerCell.getParagraphs().get(0);
        XWPFRun hRun = headerParagraph.createRun();
        headerCell.setColor("CDCDB4");
        hRun.setText("PERSONAL INFORMATION");
        tableRowOne.addNewTableCell().setText(null);

        headerParagraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFTableCell photoHeaderCell = tableRowOne.getCell(1);
        XWPFParagraph photoHeaderParagraph = photoHeaderCell.getParagraphs().get(0);
        XWPFRun pRun = photoHeaderParagraph.createRun();
        photoHeaderCell.setColor("CDCDB4");
        pRun.setText("PHOTO");
        photoHeaderParagraph.setAlignment(ParagraphAlignment.CENTER);

        XWPFTableRow tableRowTwo = table.createRow();
        XWPFTableCell cell = tableRowTwo.getCell(0);
        XWPFParagraph personalInfo = cell.getParagraphs().get(0);
        XWPFRun r2 = personalInfo.createRun();
        r2.setText("Name");
        r2.addTab();
        r2.addTab();
        r2.setText(": " + polygraph.getName());
        r2.addBreak();
        r2.setText("Gender");
        r2.addTab();
        r2.addTab();
        r2.setText(": " + polygraph.getGender());
        r2.addBreak();
        r2.setText("Age");
        r2.addTab();
        r2.addTab();
        r2.setText(": " + polygraph.getAge());
        r2.addBreak();
        r2.setText("Date of Birth");
        r2.addTab();
        r2.setText(": " + polygraph.getBirthdate());
        r2.addBreak();
        r2.setText("Civil Status");
        r2.addTab();
        r2.setText(": " + polygraph.getCivilStatus());
        r2.addBreak();
        r2.setText("ID Presented");
        r2.addTab();
        r2.setText(": " + polygraph.getIdPresented());
        r2.addBreak();
        r2.setText("Address");
        r2.addTab();
        r2.setText(": " + polygraph.getAddress());

        //Adding the picture
        XWPFTableCell pictureCell = tableRowTwo.getCell(1);
        XWPFParagraph pictureHolder = pictureCell.getParagraphs().get(0);
        XWPFRun pictureRun = pictureHolder.createRun();
        FileInputStream getPhoto = new FileInputStream(polygraph.getPhotoLocation());
        FileInputStream getImage = new FileInputStream(polygraph.getPhotoLocation());
        ImageInputStream imageInput = ImageIO.createImageInputStream(getPhoto);
        BufferedImage bi = ImageIO.read(imageInput);
        pictureHolder.setAlignment(ParagraphAlignment.RIGHT);
        pictureRun.addPicture(getImage, XWPFDocument.PICTURE_TYPE_JPEG, null, Units.toEMU(120),
                Units.toEMU(120));

        XWPFParagraph spacing = document.createParagraph();
        XWPFRun spacingRun = spacing.createRun();

        //create table
        XWPFTable otherTable = document.createTable();
        //width 
        CTTbl tableFixTwo = otherTable.getCTTbl();
        CTTblPr prTwo = tableFixTwo.getTblPr();
        CTTblWidth tblWTwo = prTwo.getTblW();
        tblWTwo.setW(BigInteger.valueOf(4800));
        tblWTwo.setType(STTblWidth.PCT);
        prTwo.setTblW(tblWTwo);
        tableFixTwo.setTblPr(prTwo);

        XWPFTableRow examInfoHeader = otherTable.createRow();
        XWPFTableCell cellInfo = examInfoHeader.getCell(0);
        XWPFParagraph examInfo = cellInfo.getParagraphs().get(0);
        XWPFRun r3 = examInfo.createRun();
        cellInfo.setColor("CDCDB4");
        r3.setText("EXAM INFORMATION");
        examInfo.setAlignment(ParagraphAlignment.CENTER);

        XWPFTableRow examInfoRow = otherTable.createRow();
        XWPFTableCell cellRowInfo = examInfoRow.getCell(0);
        XWPFParagraph examInfoRowP = cellRowInfo.getParagraphs().get(0);
        XWPFRun examRun = examInfoRowP.createRun();
        examRun.setText("Case Number");
        examRun.addTab();
        examRun.addTab();
        examRun.setText(": " + polygraph.getCaseNo());
        examRun.addBreak();
        examRun.setText("Requesting Party");
        examRun.addTab();
        examRun.setText(": " + polygraph.getRequestingParty());
        examRun.addBreak();
        examRun.setText("Time/Date Received");
        examRun.addTab();
        examRun.setText(": " + polygraph.getTimeDateReceived());
        examRun.addBreak();
        examRun.setText("Nature of Case");
        examRun.addTab();
        examRun.addTab();
        examRun.setText(": " + polygraph.getNatureOfCase());
        examRun.addBreak();
        examRun.setText("Exam Location");
        examRun.addTab();
        examRun.addTab();
        examRun.setText(": " + polygraph.getExamLocation());
        examRun.addBreak();
        examRun.setText("Exam Date");
        examRun.addTab();
        examRun.addTab();
        examRun.setText(": " + polygraph.getExamDate());

        otherTable.removeRow(0);

        XWPFParagraph purposeOfExamination = document.createParagraph();
        XWPFRun r4 = purposeOfExamination.createRun();
        r4.setUnderline(UnderlinePatterns.SINGLE);
        r4.addBreak();
        r4.setText("SECTION 1: PURPOSE OF EXAMINATION");
        r4.addTab();
        r4.addTab();
        r4.addTab();
        r4.addTab();
        r4.addTab();
        r4.addTab();
        r4.addTab();
        r4.addTab();
        r4.addTab();

        XWPFParagraph purposeOfExaminationContents = document.createParagraph();
        XWPFRun r4Contents = purposeOfExaminationContents.createRun();
        r4Contents.setText(polygraph.getPurpose());

        XWPFParagraph preTestInterview = document.createParagraph();
        XWPFRun r5 = preTestInterview.createRun();
        r5.setUnderline(UnderlinePatterns.SINGLE);
        r5.setText("SECTION 2: PRE-TEST INTERVIEW");
        r5.addTab();
        r5.addTab();
        r5.addTab();
        r5.addTab();
        r5.addTab();
        r5.addTab();
        r5.addTab();
        r5.addTab();
        r5.addTab();

        XWPFParagraph preTestInterviewContents = document.createParagraph();
        XWPFRun r5Contents = preTestInterviewContents.createRun();
        r5Contents.setText(polygraph.getPreTest());

        XWPFParagraph inTestPhase = document.createParagraph();
        XWPFRun r6 = inTestPhase.createRun();
        r6.setUnderline(UnderlinePatterns.SINGLE);
        r6.setText("SECTION 3: IN-TEST PHASE");
        r6.addTab();
        r6.addTab();
        r6.addTab();
        r6.addTab();
        r6.addTab();
        r6.addTab();
        r6.addTab();
        r6.addTab();
        r6.addTab();
        r6.addTab();

        XWPFParagraph inTestPhaseContents = document.createParagraph();
        XWPFRun r6Contents = inTestPhaseContents.createRun();
        r6Contents.setText(polygraph.getInTest());

        XWPFParagraph result = document.createParagraph();
        XWPFRun r7 = result.createRun();
        r7.setUnderline(UnderlinePatterns.SINGLE);
        r7.setText("SECTION 4: RESULT");
        r7.addTab();
        r7.addTab();
        r7.addTab();
        r7.addTab();
        r7.addTab();
        r7.addTab();
        r7.addTab();
        r7.addTab();
        r7.addTab();
        r7.addTab();
        r7.addTab();

        XWPFParagraph resultContents = document.createParagraph();
        XWPFRun r7Contents = resultContents.createRun();
        r7Contents.setText(polygraph.getResult());

        XWPFParagraph postTestInterview = document.createParagraph();
        XWPFRun r8 = postTestInterview.createRun();
        r8.setUnderline(UnderlinePatterns.SINGLE);
        r8.setText("SECTION 5: POST-TEST INTERVIEW");
        r8.addTab();
        r8.addTab();
        r8.addTab();
        r8.addTab();
        r8.addTab();
        r8.addTab();
        r8.addTab();
        r8.addTab();
        r8.addTab();

        XWPFParagraph postTestInterviewContents = document.createParagraph();
        XWPFRun r8Contents = postTestInterviewContents.createRun();
        r8Contents.setText(polygraph.getPostTest());

        XWPFParagraph remarks = document.createParagraph();
        XWPFRun r9 = remarks.createRun();
        r9.setUnderline(UnderlinePatterns.SINGLE);
        r9.setText("REMARKS:");
        r9.addTab();
        r9.addTab();
        r9.addTab();
        r9.addTab();
        r9.addTab();
        r9.addTab();
        r9.addTab();
        r9.addTab();
        r9.addTab();
        r9.addTab();
        r9.addTab();
        r9.addTab();

        XWPFParagraph remarksContents = document.createParagraph();
        XWPFRun r9Contents = remarksContents.createRun();
        r9Contents.setText(polygraph.getRemarks());

        XWPFParagraph timeDateCompleted = document.createParagraph();
        XWPFRun r10 = timeDateCompleted.createRun();
        r10.setUnderline(UnderlinePatterns.SINGLE);
        r10.setText("TIME AND DATE COMPLETED:");
        r10.addTab();
        r10.addTab();
        r10.addTab();
        r10.addTab();
        r10.addTab();
        r10.addTab();
        r10.addTab();
        r10.addTab();
        r10.addTab();
        r10.addTab();

        XWPFParagraph timeDateCompletedContents = document.createParagraph();
        XWPFRun r10Contents = timeDateCompletedContents.createRun();
        r10Contents.setText(polygraph.getTimeDateCompleted());

        XWPFParagraph examinedBy = document.createParagraph();
        XWPFRun r11 = examinedBy.createRun();
        r11.setUnderline(UnderlinePatterns.SINGLE);
        r11.setText("EXAMINED BY:");
        r11.addTab();
        r11.addTab();
        r11.addTab();
        r11.addTab();
        r11.addTab();
        r11.addTab();
        r11.addTab();
        r11.addTab();
        r11.addTab();
        r11.addTab();
        r11.addTab();
        r11.addTab();

        XWPFParagraph examinedByContents = document.createParagraph();
        XWPFRun r11Contents = examinedByContents.createRun();
        r11Contents.setText(polygraph.getExaminerName());
        r11Contents.addBreak();
        r11Contents.setText(polygraph.getExaminerRank());
        r11Contents.addBreak();
        r11Contents.setText(polygraph.getExaminerPosition());

        XWPFParagraph approvedBy = document.createParagraph();
        XWPFRun r12 = approvedBy.createRun();
        r12.setUnderline(UnderlinePatterns.SINGLE);
        r12.setText("APPROVED BY:");
        r12.addTab();
        r12.addTab();
        r12.addTab();
        r12.addTab();
        r12.addTab();
        r12.addTab();
        r12.addTab();
        r12.addTab();
        r12.addTab();
        r12.addTab();
        r12.addTab();
        r12.addTab();

        XWPFParagraph approvedByContents = document.createParagraph();
        XWPFRun r12Contents = approvedByContents.createRun();
        //            r12Contents.setText(polygraph.getApprovedName());
        //            r12Contents.addBreak();
        //            r12Contents.setText(polygraph.getApprovedRank());
        //            r12Contents.addBreak();
        //            r12Contents.setText(polygraph.getApprovedPosition());
        //            r12Contents.addBreak();

        XWPFParagraph notedBy = document.createParagraph();
        XWPFRun r13 = notedBy.createRun();
        r13.setUnderline(UnderlinePatterns.SINGLE);
        r13.setText("NOTED BY:");
        r13.addTab();
        r13.addTab();
        r13.addTab();
        r13.addTab();
        r13.addTab();
        r13.addTab();
        r13.addTab();
        r13.addTab();
        r13.addTab();
        r13.addTab();
        r13.addTab();
        r13.addTab();

        XWPFParagraph notedByContents = document.createParagraph();
        XWPFRun r13Contents = notedByContents.createRun();
        r13Contents.setText(polygraph.getNotedName());
        r13Contents.addBreak();
        r13Contents.setText(polygraph.getNotedRank());
        r13Contents.addBreak();
        r13Contents.setText(polygraph.getNotedPosition());
        r13Contents.addBreak();
        table.setInsideVBorder(XWPFTable.XWPFBorderType.NIL, 0, 0, "white");

        document.getXWPFDocument();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return document;
}

From source file:com.ecm.poi.SimpleImages.java

License:Apache License

public static void main(String[] args) throws Exception {
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p = doc.createParagraph();

    XWPFRun r = p.createRun();
    //        args[0] = "d:\\temp\\Koala.jpg";
    String imgFile = "d:\\temp\\img\\Koala.jpg";
    if (null != imgFile) {
        int format = 0;

        if (imgFile.endsWith(".emf"))
            format = XWPFDocument.PICTURE_TYPE_EMF;
        else if (imgFile.endsWith(".wmf"))
            format = XWPFDocument.PICTURE_TYPE_WMF;
        else if (imgFile.endsWith(".pict"))
            format = XWPFDocument.PICTURE_TYPE_PICT;
        else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg"))
            format = XWPFDocument.PICTURE_TYPE_JPEG;
        else if (imgFile.endsWith(".png"))
            format = XWPFDocument.PICTURE_TYPE_PNG;
        else if (imgFile.endsWith(".dib"))
            format = XWPFDocument.PICTURE_TYPE_DIB;
        else if (imgFile.endsWith(".gif"))
            format = XWPFDocument.PICTURE_TYPE_GIF;
        else if (imgFile.endsWith(".tiff"))
            format = XWPFDocument.PICTURE_TYPE_TIFF;
        else if (imgFile.endsWith(".eps"))
            format = XWPFDocument.PICTURE_TYPE_EPS;
        else if (imgFile.endsWith(".bmp"))
            format = XWPFDocument.PICTURE_TYPE_BMP;
        else if (imgFile.endsWith(".wpg"))
            format = XWPFDocument.PICTURE_TYPE_WPG;
        else {/*from  w  ww.j  av  a 2s .  co m*/
            System.err.println("Unsupported picture: " + imgFile
                    + ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
        }

        r.setText(imgFile);
        r.addBreak();
        r.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels
        r.addBreak(BreakType.PAGE);
    }

    FileOutputStream out = new FileOutputStream("d:\\temp\\images.docx");
    doc.write(out);
    out.close();
}

From source file:com.gezipu360.cashier.bean.word.SimpleImages.java

License:Apache License

public static void main(String[] args) throws Exception {
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p = doc.createParagraph();

    XWPFRun r = p.createRun();

    for (String imgFile : args) {
        int format;

        if (imgFile.endsWith(".emf"))
            format = XWPFDocument.PICTURE_TYPE_EMF;
        else if (imgFile.endsWith(".wmf"))
            format = XWPFDocument.PICTURE_TYPE_WMF;
        else if (imgFile.endsWith(".pict"))
            format = XWPFDocument.PICTURE_TYPE_PICT;
        else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg"))
            format = XWPFDocument.PICTURE_TYPE_JPEG;
        else if (imgFile.endsWith(".png"))
            format = XWPFDocument.PICTURE_TYPE_PNG;
        else if (imgFile.endsWith(".dib"))
            format = XWPFDocument.PICTURE_TYPE_DIB;
        else if (imgFile.endsWith(".gif"))
            format = XWPFDocument.PICTURE_TYPE_GIF;
        else if (imgFile.endsWith(".tiff"))
            format = XWPFDocument.PICTURE_TYPE_TIFF;
        else if (imgFile.endsWith(".eps"))
            format = XWPFDocument.PICTURE_TYPE_EPS;
        else if (imgFile.endsWith(".bmp"))
            format = XWPFDocument.PICTURE_TYPE_BMP;
        else if (imgFile.endsWith(".wpg"))
            format = XWPFDocument.PICTURE_TYPE_WPG;
        else {/*from  w ww  . j  av a2  s  .c  o m*/
            System.err.println("Unsupported picture: " + imgFile
                    + ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
            continue;
        }

        r.setText(imgFile);
        r.addBreak();
        r.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels
        r.addBreak(BreakType.PAGE);
    }

    FileOutputStream out = new FileOutputStream("images.docx");
    doc.write(out);
    out.close();
}

From source file:com.glodon.tika.SimpleImages.java

License:Apache License

public static void main(String[] args) throws IOException, InvalidFormatException {
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p = doc.createParagraph();

    XWPFRun r = p.createRun();

    for (String imgFile : args) {
        int format;

        if (imgFile.endsWith(".emf"))
            format = XWPFDocument.PICTURE_TYPE_EMF;
        else if (imgFile.endsWith(".wmf"))
            format = XWPFDocument.PICTURE_TYPE_WMF;
        else if (imgFile.endsWith(".pict"))
            format = XWPFDocument.PICTURE_TYPE_PICT;
        else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg"))
            format = XWPFDocument.PICTURE_TYPE_JPEG;
        else if (imgFile.endsWith(".png"))
            format = XWPFDocument.PICTURE_TYPE_PNG;
        else if (imgFile.endsWith(".dib"))
            format = XWPFDocument.PICTURE_TYPE_DIB;
        else if (imgFile.endsWith(".gif"))
            format = XWPFDocument.PICTURE_TYPE_GIF;
        else if (imgFile.endsWith(".tiff"))
            format = XWPFDocument.PICTURE_TYPE_TIFF;
        else if (imgFile.endsWith(".eps"))
            format = XWPFDocument.PICTURE_TYPE_EPS;
        else if (imgFile.endsWith(".bmp"))
            format = XWPFDocument.PICTURE_TYPE_BMP;
        else if (imgFile.endsWith(".wpg"))
            format = XWPFDocument.PICTURE_TYPE_WPG;
        else {/*from   w  w  w  .j  a va2s.  c o m*/
            System.err.println("Unsupported picture: " + imgFile
                    + ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
            continue;
        }

        r.setText(imgFile);
        r.addBreak();
        r.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels
        r.addBreak(BreakType.PAGE);
    }

    FileOutputStream out = new FileOutputStream("images.docx");
    doc.write(out);
    out.close();
    doc.close();
}

From source file:com.min.word.core.MakeWordFileTest.java

License:Apache License

public static void main(String[] args) throws Exception {
    String fileName = "test.docx";
    System.out.println("---------- Word Create Start ------------");
    //  word ? ?//  w w  w .j a  v  a2s  .  c  o m
    XWPFDocument document = new XWPFDocument();
    FileOutputStream out = new FileOutputStream(new File(fileName));
    System.out.println("---------- Create Blank Success ------------");

    //Paragraph ?
    XWPFParagraph paragraph = document.createParagraph();
    System.out.println("---------- Create Paragraph Success ------------");

    //border ?
    paragraph.setBorderBottom(Borders.BASIC_BLACK_DASHES);
    paragraph.setBorderLeft(Borders.BASIC_BLACK_DASHES);
    paragraph.setBorderRight(Borders.BASIC_BLACK_DASHES);
    paragraph.setBorderTop(Borders.BASIC_BLACK_DASHES);
    System.out.println("---------- Create Border Success ------------");

    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.");
    System.out.println("---------- Text Write to File ------------");

    //Table ?
    XWPFTable table = document.createTable();
    //row
    XWPFTableRow rowOne = table.getRow(0);
    rowOne.getCell(0).setText("Col One, Row One");
    rowOne.addNewTableCell().setText("Col Tow, Row One");
    rowOne.addNewTableCell().setText("Col Three, Row One");
    //row
    XWPFTableRow rowTow = table.createRow();
    rowTow.getCell(0).setText("Col One, Row Tow");
    rowTow.getCell(1).setText("Col Tow, Row Tow");
    rowTow.getCell(2).setText("Col Three, Row Tow");
    //row
    XWPFTableRow rowThree = table.createRow();
    rowThree.getCell(0).setText("Col One, Row Three");
    rowThree.getCell(1).setText("Col Tow, Row Three");
    rowThree.getCell(2).setText("Col Three, Row Three");
    System.out.println("---------- Create Table Success ------------");

    //Add Image
    XWPFParagraph imageParagraph = document.createParagraph();
    XWPFRun imageRun = imageParagraph.createRun();
    imageRun.addPicture(new FileInputStream("test.png"), XWPFDocument.PICTURE_TYPE_PNG, "test.png",
            Units.toEMU(300), Units.toEMU(300));
    System.out.println("---------- Create Image Success ------------");

    //Hyperlink
    XWPFParagraph hyperlink = document.createParagraph();
    String id = hyperlink.getDocument().getPackagePart()
            .addExternalRelationship("http://niee.kr", XWPFRelation.HYPERLINK.getRelation()).getId();
    CTR ctr = CTR.Factory.newInstance();
    CTHyperlink ctHyperlink = hyperlink.getCTP().addNewHyperlink();
    ctHyperlink.setId(id);

    CTText ctText = CTText.Factory.newInstance();
    ctText.setStringValue("Hyper-Link TEST");
    ctr.setTArray(new CTText[] { ctText });

    // ???? ?
    CTColor color = CTColor.Factory.newInstance();
    color.setVal("0000FF");
    CTRPr ctrPr = ctr.addNewRPr();
    ctrPr.setColor(color);
    ctrPr.addNewU().setVal(STUnderline.SINGLE);

    // 
    CTFonts fonts = ctrPr.isSetRFonts() ? ctrPr.getRFonts() : ctrPr.addNewRFonts();
    fonts.setAscii("?? ");
    fonts.setEastAsia("?? ");
    fonts.setHAnsi("?? ");

    // ? 
    CTHpsMeasure sz = ctrPr.isSetSz() ? ctrPr.getSz() : ctrPr.addNewSz();
    sz.setVal(new BigInteger("24"));
    ctHyperlink.setRArray(new CTR[] { ctr });
    hyperlink.setAlignment(ParagraphAlignment.LEFT);
    hyperlink.setVerticalAlignment(TextAlignment.CENTER);
    System.out.println("---------- Create Hyperlink Success ------------");

    //Font style
    XWPFParagraph fontStyle = document.createParagraph();

    //set Bold an Italic
    XWPFRun boldAnItalic = fontStyle.createRun();
    boldAnItalic.setBold(true);
    boldAnItalic.setItalic(true);
    boldAnItalic.setText("Bold an Italic");
    boldAnItalic.addBreak();

    //set Text Position
    XWPFRun textPosition = fontStyle.createRun();
    textPosition.setText("Set Text Position");
    textPosition.setTextPosition(100);

    //Set Strike through and font Size and Subscript
    XWPFRun otherStyle = fontStyle.createRun();
    otherStyle.setStrike(true);
    otherStyle.setFontSize(20);
    otherStyle.setSubscript(VerticalAlign.SUBSCRIPT);
    otherStyle.setText(" Set Strike through and font Size and Subscript");
    System.out.println("---------- Set Font Style ------------");

    //Set Alignment Paragraph
    XWPFParagraph alignment = document.createParagraph();
    //Alignment to Right
    alignment.setAlignment(ParagraphAlignment.RIGHT);

    XWPFRun alignRight = alignment.createRun();
    alignRight.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.");

    //Alignment to Center
    alignment = document.createParagraph();
    //Alignment to Right
    alignment.setAlignment(ParagraphAlignment.CENTER);
    XWPFRun alignCenter = alignment.createRun();
    alignCenter.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. ");
    System.out.println("---------- Set Alignment ------------");

    //word ? 
    document.write(out);
    out.close();
    System.out.println("---------- Save File Name : " + fileName + " ------------");
    System.out.println("---------- Word Create End ------------");
}

From source file:demo.poi.image.SimpleImages.java

License:Apache License

static void writeImageToDoc(String[] args) throws InvalidFormatException, IOException, FileNotFoundException {
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p = doc.createParagraph();

    XWPFRun r = p.createRun();

    for (String imgFile : args) {
        int format;

        if (imgFile.endsWith(".emf"))
            format = XWPFDocument.PICTURE_TYPE_EMF;
        else if (imgFile.endsWith(".wmf"))
            format = XWPFDocument.PICTURE_TYPE_WMF;
        else if (imgFile.endsWith(".pict"))
            format = XWPFDocument.PICTURE_TYPE_PICT;
        else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg"))
            format = XWPFDocument.PICTURE_TYPE_JPEG;
        else if (imgFile.endsWith(".png"))
            format = XWPFDocument.PICTURE_TYPE_PNG;
        else if (imgFile.endsWith(".dib"))
            format = XWPFDocument.PICTURE_TYPE_DIB;
        else if (imgFile.endsWith(".gif"))
            format = XWPFDocument.PICTURE_TYPE_GIF;
        else if (imgFile.endsWith(".tiff"))
            format = XWPFDocument.PICTURE_TYPE_TIFF;
        else if (imgFile.endsWith(".eps"))
            format = XWPFDocument.PICTURE_TYPE_EPS;
        else if (imgFile.endsWith(".bmp"))
            format = XWPFDocument.PICTURE_TYPE_BMP;
        else if (imgFile.endsWith(".wpg"))
            format = XWPFDocument.PICTURE_TYPE_WPG;
        else {//from   w  w w.  j  a v a2  s  .  com
            System.err.println("Unsupported picture: " + imgFile
                    + ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
            continue;
        }

        r.setText(imgFile);
        r.addBreak();
        r.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200
        // pixels
        r.addBreak(BreakType.PAGE);
    }

    FileOutputStream out = new FileOutputStream("images.docx");
    doc.write(out);
    out.close();
    // doc.close();
}

From source file:eremeykin.pete.reports.ui.ReportAction.java

@Override
public void actionPerformed(ActionEvent e) {
    resultChanged(null);//from  w  w  w .j a  v a  2 s .co  m
    if (model == null) {
        return;
    }

    XWPFDocument doc = new XWPFDocument();

    XWPFParagraph p1 = doc.createParagraph();
    p1.setAlignment(ParagraphAlignment.CENTER);
    p1.setVerticalAlignment(TextAlignment.TOP);

    XWPFRun r1 = p1.createRun();
    r1.setBold(true);
    r1.setText("");
    r1.setBold(true);
    r1.setFontFamily("Times New Roman");
    r1.setFontSize(24);
    r1.setTextPosition(10);

    XWPFParagraph p2 = doc.createParagraph();
    p2.setAlignment(ParagraphAlignment.LEFT);
    p2.setVerticalAlignment(TextAlignment.CENTER);
    XWPFRun r2 = p2.createRun();
    r2.setText(" ? : ");
    r2.setBold(false);
    r2.setFontFamily("Times New Roman");
    r2.setFontSize(14);
    r2.setTextPosition(10);

    XWPFTable table = doc.createTable(1, 2);
    table.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(9000));
    ModelParameter root = model.getRoot();

    int row = 1;
    Map.Entry<ModelParameter, Integer> kv = model.getParameterAndLevelByID(root, 0);
    ModelParameter parameter = kv.getKey();
    Integer level = kv.getValue();

    ArrayList<Integer> ids = new ArrayList(model.asMap().keySet());
    Collections.sort(ids);
    for (Integer each : ids) {
        table.createRow();
        String text = "";
        kv = model.getParameterAndLevelByID(root, each);
        parameter = kv.getKey();
        level = kv.getValue();
        for (int c = 0; c < level; c++) {
            text += "        ";
        }
        table.getRow(row - 1).getCell(0).setText(text + parameter.toString());
        table.getRow(row - 1).getCell(1).setText(parameter.getValue());
        row++;
    }
    table.setWidth(80);

    XWPFParagraph p3 = doc.createParagraph();
    p3.setAlignment(ParagraphAlignment.LEFT);
    p3.setVerticalAlignment(TextAlignment.CENTER);
    XWPFRun r3 = p3.createRun();
    r3.addBreak();
    r3.setText("\n : ");
    r3.setBold(false);
    r3.setFontFamily("Times New Roman");
    r3.setFontSize(14);

    File uPlotFile = new File(WorkspaceManager.INSTANCE.getWorkspace().getAbsolutePath() + "/uplot.png");
    try {
        byte[] picbytes = IOUtils.toByteArray(new FileInputStream(uPlotFile));
        doc.addPictureData(picbytes, XWPFDocument.PICTURE_TYPE_PNG);
        XWPFRun pr = doc.createParagraph().createRun();
        pr.addPicture(new FileInputStream(uPlotFile), Document.PICTURE_TYPE_PNG, "plot.png", Units.toEMU(450),
                Units.toEMU(337));
        pr.addCarriageReturn();
        pr.addBreak(BreakType.PAGE);
        pr.addBreak(BreakType.TEXT_WRAPPING);

    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    }

    XWPFParagraph p4 = doc.createParagraph();
    p4.setAlignment(ParagraphAlignment.LEFT);
    p4.setVerticalAlignment(TextAlignment.CENTER);
    XWPFRun r4 = p4.createRun();
    r4.addBreak();
    r4.setText("\n ?: ");
    r4.setBold(false);
    r4.setFontFamily("Times New Roman");
    r4.setFontSize(14);

    File sPlotFile = new File(WorkspaceManager.INSTANCE.getWorkspace().getAbsolutePath() + "/splot.png");
    try {
        byte[] picbytes = IOUtils.toByteArray(new FileInputStream(sPlotFile));
        doc.addPictureData(picbytes, XWPFDocument.PICTURE_TYPE_PNG);
        XWPFParagraph pp = doc.createParagraph();
        pp.createRun().addPicture(new FileInputStream(sPlotFile), Document.PICTURE_TYPE_PNG, "plot.png",
                Units.toEMU(450), Units.toEMU(337));
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    }

    File reportFile = new File("report.docx");
    try (FileOutputStream out = new FileOutputStream(reportFile)) {
        doc.write(out);
        if (Desktop.isDesktopSupported()) {
            Desktop.getDesktop().edit(reportFile);
        } else {
        }

    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}