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

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

Introduction

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

Prototype

public void addTab() 

Source Link

Document

Specifies that a tab shall be placed at the current location in the run content.

Usage

From source file:sofmeth.mco3.gui.SourceCodeFrame.java

private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneButtonActionPerformed
    // TODO add your handling code here:
    XWPFDocument document = new XWPFDocument();
    //write document
    try {/*from  www  .j av  a 2  s .  com*/
        FileOutputStream out = new FileOutputStream(new File("sourcecode.docx"));
        //making of title
        XWPFParagraph para = document.createParagraph();
        para.setAlignment(ParagraphAlignment.CENTER);
        para.setSpacingAfter(500);
        XWPFRun run = para.createRun();
        run.setText("Source Code Listing");
        run.setBold(true);
        run.setFontSize(16);
        //end making of title
        para = document.createParagraph();
        run = para.createRun();

        //adding name etc to document
        XWPFTable details = document.createTable(3, 2);
        //setting cell width
        CTTblWidth width = details.getCTTbl().addNewTblPr().addNewTblW();
        width.setType(STTblWidth.DXA);
        width.setW(BigInteger.valueOf(9000));
        //end setting cell width
        //details.getCTTbl().getTblPr().unsetTblBorders();
        XWPFTableRow row = details.getRow(0);
        row.getCell(0).setText("Name: " + nameField);
        row.getCell(1).setText("Date: " + dateField);
        row = details.getRow(1);
        row.getCell(0).setText("Program: " + progField);
        row.getCell(1).setText("Program#: " + progNumField);
        row = details.getRow(2);
        row.getCell(0).setText("Professor: " + profField);
        row.getCell(1).setText("Language: " + langField);
        //end adding name to document

        para = document.createParagraph();
        run = para.createRun();
        run.addBreak();
        run.addBreak();
        String sourceCode = codeTextArea.getText();
        if (sourceCode.contains("\n")) {
            String[] lines = sourceCode.split("\n");
            run.setText(lines[0], 0);
            for (int i = 1; i < lines.length; i++) {

                run.addBreak();
                if (lines[i].contains("\t")) {
                    int tabSpace = lines[i].length() - lines[i].replaceAll("\t", "").length();
                    for (int j = 0; j < tabSpace; j++) {
                        run.addTab();
                    }
                }
                run.setText(lines[i]);
            }
        } else
            run.setText(sourceCode);

        //sourceCode.replaceAll("\\n", "&#xA;");
        document.write(out);
        out.close();

    } catch (Exception e) {

    }

}