Example usage for org.apache.poi.xwpf.usermodel XWPFDocument write

List of usage examples for org.apache.poi.xwpf.usermodel XWPFDocument write

Introduction

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

Prototype

@SuppressWarnings("resource")
public final void write(OutputStream stream) throws IOException 

Source Link

Document

Write out this document to an Outputstream.

Usage

From source file:genrate_doc.java

void create_tt_word(String fname, String[][] tt) throws FileNotFoundException, IOException {
    XWPFDocument doc = new XWPFDocument();
    insert_title(doc);// w w  w  .  j a v  a  2s  .c o  m
    changeOrientation(doc, "landscape");

    XWPFTable table = doc.createTable(7, 10);

    table.setCellMargins(200, 200, 200, 200);

    XWPFTableRow row0 = table.getRow(0);
    XWPFTableCell r0_cell0 = row0.getCell(0);
    r0_cell0.setText("DAY/TIME");
    XWPFTableRow row1 = table.getRow(1);
    XWPFTableCell r1_cell0 = row1.getCell(0);
    r1_cell0.setText("MON");
    XWPFTableRow row2 = table.getRow(2);
    XWPFTableCell r2_cell0 = row2.getCell(0);
    r2_cell0.setText("TUE");
    XWPFTableRow row3 = table.getRow(3);
    XWPFTableCell r3_cell0 = row3.getCell(0);
    r3_cell0.setText("WED");
    XWPFTableRow row4 = table.getRow(4);
    XWPFTableCell r4_cell0 = row4.getCell(0);
    r4_cell0.setText("THR");
    XWPFTableRow row5 = table.getRow(5);
    XWPFTableCell r5_cell0 = row5.getCell(0);
    r5_cell0.setText("FRI");
    XWPFTableRow row6 = table.getRow(6);
    XWPFTableCell r6_cell0 = row6.getCell(0);
    r6_cell0.setText("SAT");

    XWPFTableCell r0_cell1 = row0.getCell(1);
    r0_cell1.setText("08:40-09:40");
    XWPFTableCell r0_cell2 = row0.getCell(2);
    r0_cell2.setText("09:40-10:40");
    XWPFTableCell r0_cell3 = row0.getCell(3);
    r0_cell3.setText("10:40-11:00");
    XWPFTableCell r0_cell4 = row0.getCell(4);
    r0_cell4.setText("11:00-12:00");
    XWPFTableCell r0_cell5 = row0.getCell(5);
    r0_cell5.setText("12:00-01:00");
    XWPFTableCell r0_cell6 = row0.getCell(6);
    r0_cell6.setText("01:00-01:40");
    XWPFTableCell r0_cell7 = row0.getCell(7);
    r0_cell7.setText("01:40-02:30");
    XWPFTableCell r0_cell8 = row0.getCell(8);
    r0_cell8.setText("02:30-03:20");
    XWPFTableCell r0_cell9 = row0.getCell(9);
    r0_cell9.setText("03:20-04:10");

    // writing the time table
    /* XWPFTableCell r1_cell1 = row1.getCell(1); 
    r1_cell1.setText(tt[0][0]);
    XWPFTableCell r1_cell2 = row1.getCell(2); 
    r1_cell2.setText(tt[0][1]);
    XWPFTableCell r1_cell3 = row1.getCell(3); 
    r1_cell3.setText(tt[0][2]);
            
    XWPFTableCell r1_cell4 = row1.getCell(4); 
    r1_cell4.setText("B");
            
    XWPFTableCell r1_cell5 = row1.getCell(5); 
    r1_cell5.setText(tt[0][3]);
    XWPFTableCell r1_cell6 = row1.getCell(6); 
    r1_cell6.setText(tt[0][4]);*/
    XWPFTableRow row;
    XWPFTableCell cell;
    for (int i = 0; i < 6; i++) {
        row = table.getRow(i + 1);
        for (int j = 0; j < 9; j++) {
            cell = row.getCell(j + 1);
            cell.setText(tt[i][j]);
        }
    }

    FileOutputStream output = new FileOutputStream("timetable\\" + fname + ".docx");
    doc.write(output);
    output.close();
}

From source file:genrate_doc.java

void teach_doc(String fname, String teach[][]) throws FileNotFoundException, IOException {
    File fp = new File("teacher files\\" + fname + ".docx");
    if (fp.exists()) {
        return;//w  ww. j a  va 2s  . c  o  m
    }
    XWPFDocument doc = new XWPFDocument();
    insert_title(doc);
    changeOrientation(doc, "landscape");
    XWPFTable table = doc.createTable(7, 10);

    table.setCellMargins(200, 200, 200, 200);

    XWPFTableRow row0 = table.getRow(0);
    XWPFTableCell r0_cell0 = row0.getCell(0);
    r0_cell0.setText("DAY/TIME");
    XWPFTableRow row1 = table.getRow(1);
    XWPFTableCell r1_cell0 = row1.getCell(0);
    r1_cell0.setText("MON");
    XWPFTableRow row2 = table.getRow(2);
    XWPFTableCell r2_cell0 = row2.getCell(0);
    r2_cell0.setText("TUE");
    XWPFTableRow row3 = table.getRow(3);
    XWPFTableCell r3_cell0 = row3.getCell(0);
    r3_cell0.setText("WED");
    XWPFTableRow row4 = table.getRow(4);
    XWPFTableCell r4_cell0 = row4.getCell(0);
    r4_cell0.setText("THR");
    XWPFTableRow row5 = table.getRow(5);
    XWPFTableCell r5_cell0 = row5.getCell(0);
    r5_cell0.setText("FRI");
    XWPFTableRow row6 = table.getRow(6);
    XWPFTableCell r6_cell0 = row6.getCell(0);
    r6_cell0.setText("SAT");

    XWPFTableCell r0_cell1 = row0.getCell(1);
    r0_cell1.setText("08:40-09:40");
    XWPFTableCell r0_cell2 = row0.getCell(2);
    r0_cell2.setText("09:40-10:40");
    XWPFTableCell r0_cell3 = row0.getCell(3);
    r0_cell3.setText("10:40-11:00");
    XWPFTableCell r0_cell4 = row0.getCell(4);
    r0_cell4.setText("11:00-12:00");
    XWPFTableCell r0_cell5 = row0.getCell(5);
    r0_cell5.setText("12:00-01:00");
    XWPFTableCell r0_cell6 = row0.getCell(6);
    r0_cell6.setText("01:00-01:40");
    XWPFTableCell r0_cell7 = row0.getCell(7);
    r0_cell7.setText("01:40-02:30");
    XWPFTableCell r0_cell8 = row0.getCell(8);
    r0_cell8.setText("02:30-03:20");
    XWPFTableCell r0_cell9 = row0.getCell(9);
    r0_cell9.setText("03:20-04:10");

    // writing the time table
    /* XWPFTableCell r1_cell1 = row1.getCell(1); 
    r1_cell1.setText(tt[0][0]);
    XWPFTableCell r1_cell2 = row1.getCell(2); 
    r1_cell2.setText(tt[0][1]);
    XWPFTableCell r1_cell3 = row1.getCell(3); 
    r1_cell3.setText(tt[0][2]);
            
    XWPFTableCell r1_cell4 = row1.getCell(4); 
    r1_cell4.setText("B");
            
    XWPFTableCell r1_cell5 = row1.getCell(5); 
    r1_cell5.setText(tt[0][3]);
    XWPFTableCell r1_cell6 = row1.getCell(6); 
    r1_cell6.setText(tt[0][4]);*/
    XWPFTableRow row;
    XWPFTableCell cell;
    for (int i = 0; i < 6; i++) {
        row = table.getRow(i + 1);
        for (int j = 0; j < 9; j++) {
            cell = row.getCell(j + 1);
            cell.setText(teach[i][j]);
        }
    }

    FileOutputStream output = new FileOutputStream("teacher files\\" + fname + ".docx");
    doc.write(output);
    output.close();
}

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();//from  w  w  w  . jav a  2s. c om

    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 {
            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:AkashApplications.src.GenerateInvoice.java

public boolean printInvoice(boolean printCommand)
        throws FileNotFoundException, InvalidFormatException, IOException {

    XWPFDocument document = new XWPFDocument();
    CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
    CTPageMar pageMar = sectPr.addNewPgMar();
    pageMar.setLeft(BigInteger.valueOf(720L));
    pageMar.setTop(BigInteger.valueOf(460L));
    pageMar.setRight(BigInteger.valueOf(720L));
    pageMar.setBottom(BigInteger.valueOf(460L));

    XWPFParagraph dateP = document.createParagraph();
    XWPFRun dateRun = dateP.createRun();
    dateRun.addBreak();//w w w .  j  av a2s  . com
    dateRun.setText(copyType);
    dateRun.setFontSize(8);
    dateRun.setItalic(true);
    dateP.setAlignment(ParagraphAlignment.RIGHT);
    dateRun.addBreak();

    XWPFParagraph title = document.createParagraph();
    XWPFRun titleRun = title.createRun();
    titleRun.addBreak();
    titleRun.setText("Delivery Challan");
    titleRun.setBold(true);
    title.setAlignment(ParagraphAlignment.CENTER);
    titleRun.addBreak();

    //header table
    XWPFTable productTable = document.createTable();
    productTable.setCellMargins(50, 50, 50, 50);
    productTable.getCTTbl().getTblPr().unsetTblBorders();
    productTable.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(11000));

    XWPFTableRow row1 = productTable.getRow(0);

    XWPFTableCell cellSellerBuyer = row1.getCell(0);
    cellSellerBuyer.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(5000));

    CTTc ctTc = cellSellerBuyer.getCTTc();
    CTTcPr tcPr = ctTc.getTcPr();
    CTTcBorders border = tcPr.addNewTcBorders();
    border.addNewBottom().setVal(STBorder.SINGLE);
    //        border.addNewRight().setVal(STBorder.SINGLE);
    border.addNewLeft().setVal(STBorder.SINGLE);
    border.addNewTop().setVal(STBorder.SINGLE);

    XWPFParagraph seller = cellSellerBuyer.addParagraph();
    XWPFRun sellerRun = seller.createRun();
    seller.setAlignment(ParagraphAlignment.LEFT);
    sellerRun.setText("Buyer :");
    sellerRun.addBreak();
    String[] ar = buyerAddress.split("\n");
    for (String s : ar) {
        sellerRun.setText(s);
        sellerRun.addBreak();
    }

    XWPFTableCell cellProductDesc = row1.createCell();
    cellProductDesc.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(6000));

    ctTc = cellProductDesc.getCTTc();
    tcPr = ctTc.getTcPr();
    border = tcPr.addNewTcBorders();
    border.addNewBottom().setVal(STBorder.SINGLE);
    border.addNewRight().setVal(STBorder.SINGLE);
    //border.addNewLeft().setVal(STBorder.SINGLE);
    border.addNewTop().setVal(STBorder.SINGLE);

    XWPFParagraph productDesc = cellProductDesc.addParagraph();
    productDesc.setAlignment(ParagraphAlignment.LEFT);
    XWPFRun runInvoice = productDesc.createRun();

    //        if(table.getRowCount() == 1)
    //        {
    //            runInvoice.addTab();
    //            runInvoice.addTab();
    //            runInvoice.addTab();
    //            String imgLoc = "Barcodes/"+ dtm.getValueAt(0, 6) +".jpg";
    //            FileInputStream image = new FileInputStream(imgLoc);
    //            runInvoice.addPicture(image, XWPFDocument.PICTURE_TYPE_JPEG,imgLoc, Units.toEMU(200), Units.toEMU(50));
    //            runInvoice.addBreak();
    //            runInvoice.addBreak();
    //        }

    runInvoice.setText("Delivery Note : ");
    runInvoice.addTab();
    runInvoice.setText(invoiceNote);
    runInvoice.addBreak();
    runInvoice.addBreak();
    runInvoice.addBreak();
    runInvoice.setText("Date : ");
    runInvoice.addTab();
    runInvoice.setText(date);
    runInvoice.addBreak();
    runInvoice.addBreak();
    //document.createParagraph().createRun().addBreak();

    XWPFTable productDetails = document.createTable();
    productDetails.setCellMargins(50, 50, 50, 50);
    //productDetails.getCTTbl().getTblPr().addNewJc().setVal(STJc.RIGHT);
    productDetails.getCTTbl().getTblPr().unsetTblBorders();
    productDetails.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(11000));

    XWPFTableRow pDetailHeader = productDetails.getRow(0);

    XWPFTableCell header0 = pDetailHeader.getCell(0);
    header0.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(1000));
    XWPFParagraph headerText = header0.addParagraph();
    XWPFRun run = headerText.createRun();
    run.setText("Sl. No.");
    run.setBold(true);
    ctTc = header0.getCTTc();
    tcPr = ctTc.addNewTcPr();
    border = tcPr.addNewTcBorders();
    border.addNewBottom().setVal(STBorder.SINGLE);
    border.addNewRight().setVal(STBorder.SINGLE);
    border.addNewLeft().setVal(STBorder.SINGLE);
    border.addNewTop().setVal(STBorder.SINGLE);

    XWPFTableCell header1 = pDetailHeader.createCell();
    header1.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(8000));
    XWPFParagraph headerText1 = header1.addParagraph();
    XWPFRun run1 = headerText1.createRun();
    run1.setText("Description of Goods");
    run1.setBold(true);
    ctTc = header1.getCTTc();
    tcPr = ctTc.addNewTcPr();
    border = tcPr.addNewTcBorders();
    border.addNewBottom().setVal(STBorder.SINGLE);
    border.addNewRight().setVal(STBorder.SINGLE);
    border.addNewLeft().setVal(STBorder.SINGLE);
    border.addNewTop().setVal(STBorder.SINGLE);

    XWPFTableCell header4 = pDetailHeader.createCell();
    header4.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000));
    XWPFParagraph headerText4 = header4.addParagraph();
    XWPFRun run4 = headerText4.createRun();
    run4.setText("Quantity");
    run4.setBold(true);
    ctTc = header4.getCTTc();
    tcPr = ctTc.addNewTcPr();
    border = tcPr.addNewTcBorders();
    border.addNewBottom().setVal(STBorder.SINGLE);
    border.addNewRight().setVal(STBorder.SINGLE);
    border.addNewLeft().setVal(STBorder.SINGLE);
    border.addNewTop().setVal(STBorder.SINGLE);

    for (int i = 0; i < 1; i++) {
        XWPFTableRow fTableRow = productDetails.createRow();
        for (int j = 0; j < 4; j++) {
            XWPFTableCell cell = fTableRow.getCell(j);
            try {
                ctTc = cell.getCTTc();
                tcPr = ctTc.addNewTcPr();
                border = tcPr.addNewTcBorders();
                //border.addNewBottom().setVal(STBorder.SINGLE);
                border.addNewRight().setVal(STBorder.SINGLE);
                border.addNewLeft().setVal(STBorder.SINGLE);
                if (i == 0)
                    border.addNewTop().setVal(STBorder.SINGLE);
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }

            switch (j) {
            case 0:
                cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(1000));
                break;
            case 1:
                cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(8000));
                break;
            case 2:
                cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000));
                break;

            }

        }
    }

    int qty = 0;

    for (int i = 0; i < table.getRowCount(); i++) {
        XWPFTableRow fTableRow = productDetails.createRow();
        for (int j = 0; j < table.getColumnCount() - 1; j++) {
            XWPFTableCell cell = fTableRow.getCell(j);
            try {
                ctTc = cell.getCTTc();
                tcPr = ctTc.addNewTcPr();
                border = tcPr.addNewTcBorders();
                if (i == table.getRowCount() - 1)
                    border.addNewBottom().setVal(STBorder.SINGLE);
                border.addNewRight().setVal(STBorder.SINGLE);
                border.addNewLeft().setVal(STBorder.SINGLE);
                //                    border.addNewTop().setVal(STBorder.SINGLE); 
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }

            switch (j) {
            case 0:
                cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(1000));
                cell.setText(String.valueOf(dtm.getValueAt(i, j)));
                break;
            case 1:
                cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(8000));
                cell.setText(String.valueOf(dtm.getValueAt(i, j)));
                break;
            case 2:
                cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000));
                cell.setText(String.valueOf(dtm.getValueAt(i, j)));
                qty += Integer.parseInt(String.valueOf(dtm.getValueAt(i, j)));
                break;

            }

        }
    }

    XWPFTableRow finalRow = productDetails.createRow();
    XWPFTableCell fc0 = finalRow.getCell(0);
    fc0.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(1000));

    XWPFTableCell fc3 = finalRow.getCell(1);
    fc3.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(8000));
    XWPFParagraph fp1 = fc3.addParagraph();
    XWPFRun fRun1 = fp1.createRun();
    fRun1.setBold(true);
    fRun1.setText("TOTAL");

    XWPFTableCell fc4 = finalRow.getCell(2);
    fc4.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000));
    XWPFParagraph fp2 = fc4.addParagraph();
    XWPFRun fRun2 = fp2.createRun();
    fRun2.setBold(true);
    fRun2.setText(String.valueOf(qty));

    ctTc = fc0.getCTTc();
    tcPr = ctTc.getTcPr();
    border = tcPr.addNewTcBorders();
    border.addNewBottom().setVal(STBorder.SINGLE);
    //border.addNewRight().setVal(STBorder.SINGLE);
    border.addNewLeft().setVal(STBorder.SINGLE);
    border.addNewTop().setVal(STBorder.SINGLE);

    ctTc = fc3.getCTTc();
    tcPr = ctTc.getTcPr();
    border = tcPr.addNewTcBorders();
    border.addNewBottom().setVal(STBorder.SINGLE);
    border.addNewRight().setVal(STBorder.SINGLE);
    //border.addNewLeft().setVal(STBorder.SINGLE);
    border.addNewTop().setVal(STBorder.SINGLE);

    ctTc = fc4.getCTTc();
    tcPr = ctTc.getTcPr();
    border = tcPr.addNewTcBorders();
    border.addNewBottom().setVal(STBorder.SINGLE);
    border.addNewRight().setVal(STBorder.SINGLE);
    border.addNewLeft().setVal(STBorder.SINGLE);
    border.addNewTop().setVal(STBorder.SINGLE);

    XWPFParagraph netPMethod = document.createParagraph();
    XWPFRun pMethodRun = netPMethod.createRun();
    pMethodRun.addBreak();
    pMethodRun.addBreak();
    pMethodRun.setItalic(true);
    pMethodRun.setFontSize(10);
    pMethodRun.setText("NB. - Goods sold to the above buyer is in " + paymentType + ".");

    try {

        File f = new File(filePath);
        if (!f.exists()) {
            f.mkdir();
        }
        FileOutputStream outputStream = new FileOutputStream(filePath + File.separator + "Challan"
                + invoiceNote.replace("/", "_") + copyType.replace("/", "_") + ".docx");
        document.write(outputStream);
        outputStream.close();
        new InvoiceNoteManager().setProperty();
        if (printCommand)
            printDocument();
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }

    return true;
}

From source file:authorslilhelper.FXMLDocumentController.java

License:Open Source License

public void createPrintable() {
    try {//from   w  w  w .j  a  v a  2 s.c  o m

        //add the LAST paragraph to paragraphs here
        /*int endOfPara = s.length() - 1;
        char cur = 'a';
        char next = 'b';
        char nextNext = 'c';
        int variableIndex = endOfPara;
                
        while(!((cur == ' ' || cur == '\n') && (next == '\n')))
        {
            cur = s.charAt(variableIndex);
            next = s.charAt(variableIndex - 1);
            //nextNext = s.charAt(variableIndex - 2);
            variableIndex--;
        }
                
        String blah = s.substring(variableIndex, endOfPara);
        paragraphs.add(blah);*/

        /*    if(current == '@')            //read in the new document and make all the chapter headers bold
            {}*/

        final Stage dialog = new Stage();
        dialog.initModality(Modality.APPLICATION_MODAL);

        final TextField textField = new TextField();
        Button submit = new Button();
        Button cancel = new Button();

        cancel.setText("Cancel");
        cancel.setAlignment(Pos.CENTER);
        submit.setText("Continue");
        submit.setAlignment(Pos.BOTTOM_RIGHT);
        // submit.

        final VBox dialogVbox = new VBox(20);
        dialogVbox.getChildren().add(new Text("What is the name/title of your book? "));
        dialogVbox.getChildren().add(textField);
        dialogVbox.getChildren().add(submit);
        dialogVbox.getChildren().add(cancel);

        Scene dialogScene = new Scene(dialogVbox, 300, 200);
        dialog.setScene(dialogScene);
        dialog.setTitle("Give your book a title ");
        dialog.show();

        submit.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent anEvent) {

                System.out.println("Hi create Printable: " + characterCapacity);

                save();

                Primary.appendText("\n\n" + "----End of Document----" + "\n");

                ArrayList paragraphs = new ArrayList();
                String s = Primary.getText();
                char current = ' ';
                char previous = ' ';
                String aParagraph = "";
                int beginningIndex = 0;

                for (int i = 0; i < s.length(); i++) {
                    current = s.charAt(i);

                    /*if(i > 0)
                    {
                        previous = s.charAt(i - 1);
                    }*/

                    if (current == '\n' && previous == '\n') {
                        aParagraph = s.substring(beginningIndex, i);

                        //todo: remove every 90th character (eg when % 90 == 0) from aParagraph before adding it
                        //THIS IS THE "IMPORTANT" BLOCK YOU BRACKETED OUT ON A WHIM
                        /*                for(int a = 0; a < aParagraph.length(); a++)
                        {   //lecturer
                            //use your "fame" to your advantage
                            //yt channel- 
                            if(a >= characterCapacity)//surprising her, as the connection grew closer/stronger and stronger, chapter titles, last paragraph getting cut off
                            {//a state of deep & profound relaxation. Sitara cared about nothing/little. It was the l itself that made the [first] entreaty, wherein she could, in idle 'conversation' beckon it/request it to mve; or to perform any operation allowed/conceivable by its physical makeup.
                                if(a % characterCapacity == 0)         //author's note: there are things in there that are pure fantasy/while the vast majority is pure fantasy, as well as things in there that are based on my real life, loosely and even closely/nearly identically, in some cases
                                {//"a polite request works better than an assertion of dominance--something you're unfamiliar with, I'm sure."
                                    StringBuilder builder = new StringBuilder(aParagraph);
                                    System.out.println(aParagraph.charAt(a));
                                    builder.deleteCharAt(a);
                                    aParagraph = builder.toString();
                                }
                            }
                        }*/

                        /*    int b = 0;
                            for(int a = 0; a < aParagraph.length(); a++)
                            {   //lecturer
                                //use your "fame" to your advantage
                                //yt channel- 
                                //if(b == characterCapacity)//surprising her, as the connection grew closer/stronger and stronger, chapter titles, last paragraph getting cut off
                                //{//a state of deep & profound relaxation. Sitara cared about nothing/little. It was the l itself that made the [first] entreaty, wherein she could, in idle 'conversation' beckon it/request it to mve; or to perform any operation allowed/conceivable by its physical makeup.
                                    //if(a % (characterCapacity) == 0)         //author's note: there are things in there that are pure fantasy/while the vast majority is pure fantasy, as well as things in there that are based on my real life, loosely and even closely/nearly identically, in some cases
                                    //{//"a polite request works better than an assertion of dominance--something you're unfamiliar with, I'm sure."
                                if(b == characterCapacity) 
                                {
                                    StringBuilder builder = new StringBuilder(aParagraph);
                                        //builder.deleteCharAt(a);
                                        //builder.deleteCharAt(a-1);
                                        System.out.println(aParagraph.charAt(a));
                                        aParagraph = builder.toString();
                                
                            b = 0;
                                    //}
                                }
                                b++;
                            }*/

                        paragraphs.add(aParagraph);

                        /*if(s.substring(i, s.length()-1).length() <= 90)
                        {
                            String finalParagraph = s.substring(i, s.length()-1);               //light-blue tiles
                            paragraphs.add(finalParagraph);
                        }*/

                        while (s.charAt(i) == ' ') {
                            i++;
                        }

                        beginningIndex = i;
                    }

                    previous = current;
                }

                String title = textField.getText();

                dialog.close();

                int result = JOptionPane.showConfirmDialog(null,
                        "In the next screen, enter a title/a unique label for your manuscript.\n If needed, this can include a version number or type.\n It does not need to have the same title as your book. \n You do NOT need to include any extensions, such as .docx--this will\n be taken care of automatically.",
                        "No extension required", JOptionPane.OK_CANCEL_OPTION); //[before returning to the login screen?]

                if (result == 0) {
                    JFileChooser chooser = new JFileChooser();
                    int value = chooser.showOpenDialog(null);
                    String saveFile = "";

                    //                            JOptionPane.showMessageDialog(null, "In the next screen, enter a title/a unique label for your manuscript. If needed, this can include a version number or type. It does not need to have the same title as your book. You do NOT need to include any extensions, such as .docx--this will be taken care of automatically.");
                    if (value == JFileChooser.APPROVE_OPTION) {
                        saveFile = chooser.getSelectedFile().getPath() + ".docx";
                    }

                    try {
                        FileOutputStream outstream = new FileOutputStream(saveFile);
                        //FileOutputStream outstream = new FileOutputStream("C:/Users/Quiggy/Desktop/Test.docx");
                        XWPFDocument doc = new XWPFDocument();

                        XWPFParagraph titlePara = doc.createParagraph();
                        titlePara.setAlignment(ParagraphAlignment.CENTER);
                        XWPFRun titleRun = titlePara.createRun();
                        titleRun.setBold(true);
                        titleRun.setFontSize(18);
                        //titleRun.setText("\n\n\n\n\n\n" + title + "\n\n\n\n\n\n\n\n");
                        titleRun.addBreak();
                        titleRun.addBreak();
                        titleRun.addBreak();
                        titleRun.addBreak();
                        titleRun.addBreak();
                        titleRun.addBreak();
                        titleRun.addBreak();
                        titleRun.setText(title);
                        titleRun.addBreak(BreakType.PAGE);

                        XWPFParagraph contents = doc.createParagraph();
                        titlePara.setAlignment(ParagraphAlignment.CENTER);
                        XWPFRun contentsRun = contents.createRun();
                        contentsRun.setBold(true);
                        contentsRun.setFontSize(16);
                        contentsRun.setText("Contents: ");
                        contentsRun.addBreak();
                        contentsRun.addBreak();

                        XWPFParagraph chaptersNames = doc.createParagraph();
                        titlePara.setAlignment(ParagraphAlignment.CENTER);

                        int chapterCount = 0;
                        for (int c = 0; c < paragraphs.size(); c++) {
                            /*XWPFParagraph p = doc.createParagraph(); //Paragraph
                            XWPFRun run = p.createRun();  //Where the text will be written from
                            run.setText("\t");*/

                            XWPFRun chaptersRun = chaptersNames.createRun();
                            chaptersRun.setBold(false);
                            chaptersRun.setFontSize(12);
                            //chaptersRun.setText("A chapter \t\t\t\t\t\t\t\t ... ");

                            String currentParagraph = paragraphs.get(c).toString();
                            for (int d = 0; d < currentParagraph.length(); d++) {
                                if (currentParagraph.charAt(d) == '~') {
                                    chapterCount++;
                                    currentParagraph = currentParagraph.substring(d + 1,
                                            currentParagraph.length() - 2);
                                    if ((currentParagraph.indexOf("chapter") == -1)
                                            && (currentParagraph.indexOf("Chapter") == -1)
                                            && (currentParagraph.indexOf("CHAPTER") == -1)) {
                                        chaptersRun.setText("Chapter " + chapterCount + ": " + currentParagraph
                                                + "\t\t\t\t\t\t\t ... ");
                                    } else {
                                        chaptersRun.setText(currentParagraph + "\t\t\t\t\t\t\t ... ");
                                    }
                                    chaptersRun.addBreak();

                                } else {
                                    //run.setBold(false);
                                }
                            }

                            //doc.createParagraph().createRun().setText("           " + paragraphs.get(c).toString());
                            //doc.createParagraph().createRun().addBreak();
                        }

                        XWPFParagraph breakPara = doc.createParagraph();
                        titlePara.setAlignment(ParagraphAlignment.CENTER);
                        XWPFRun breakRun = breakPara.createRun();
                        breakRun.setBold(false);
                        breakRun.setFontSize(12);
                        breakRun.addBreak(BreakType.PAGE);

                        XWPFParagraph para = doc.createParagraph();
                        para.setAlignment(ParagraphAlignment.CENTER);
                        XWPFRun paraRun = para.createRun();
                        paraRun.setBold(true);
                        paraRun.setFontSize(12);//paraRun.setFontSize(16);
                        //paraRun.setText("Hello World");
                        //paraRun.setText(Primary.getText());

                        //doc.createParagraph().createRun().addBreak();                 //inserts a line break
                        //doc.createParagraph().createRun().setText("blah");            //creates another paragraph

                        for (int c = 0; c < paragraphs.size(); c++) {
                            /*XWPFParagraph p = doc.createParagraph(); //Paragraph
                            XWPFRun run = p.createRun();  //Where the text will be written from
                            run.setText("\t");*/

                            XWPFParagraph p = doc.createParagraph(); //Paragraph
                            XWPFRun run = p.createRun(); //Where the text will be written from

                            String currentParagraph = paragraphs.get(c).toString();
                            for (int d = 0; d < currentParagraph.length(); d++) {
                                if (currentParagraph.charAt(d) == '~') {
                                    currentParagraph = currentParagraph.substring(d + 1,
                                            currentParagraph.length() - 2);

                                    run.setBold(true);

                                } else {
                                    //run.setBold(false);
                                }
                            }

                            run.setText("           " + currentParagraph);
                            run.addBreak(); //NEW!

                            //doc.createParagraph().createRun().setText("           " + paragraphs.get(c).toString());
                            //doc.createParagraph().createRun().addBreak();
                        }

                        doc.write(outstream);
                        outstream.close();
                    } catch (Exception io) {
                    }
                    //JOptionPane.showConfirmDialog(null, "Are you sure you want to create the word document?");

                    //Makes the chapter titles bold and removes the "@" & "-"

                    JOptionPane.showMessageDialog(null,
                            "Document Successfully Created. The program/AA will now exit");
                    try {
                        if (Desktop.isDesktopSupported()) {
                            Desktop.getDesktop().open(new File(saveFile));
                        }
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                    System.exit(0);
                }
                if (result == 1) {

                }
                /*if(result == 2)
                {
                        
                }*/

            }
        });

        cancel.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent anEvent) {
                dialog.close();
                //close the window here
            }
        });

    } catch (Exception e) {

    }
}

From source file:biz.webgate.dominoext.poi.component.kernel.DocumentProcessor.java

License:Apache License

public void generateNewFile(ITemplateSource itsCurrent, List<IDocumentBookmark> bookmarks,
        List<DocumentTable> tables, HttpServletResponse httpResponse, String strFileName, FacesContext context,
        UIDocument uiDoc) throws IOException {
    Logger logCurrent = LoggerFactory.getLogger(this.getClass().getCanonicalName());
    try {// w w  w .  ja va 2s  .  c o m
        int nTemplateAccess = itsCurrent.accessTemplate();
        if (nTemplateAccess == 1) {
            XWPFDocument dxDocument = processDocument(itsCurrent, bookmarks, tables, context, uiDoc);
            if (uiDoc.getPdfOutput()) {

                logCurrent.info("Build PDF");
                ByteArrayOutputStream bosDoc = new ByteArrayOutputStream();
                dxDocument.write(bosDoc);
                bosDoc.flush();
                logCurrent.info("Size of worddocument ByteArrayOutputStream: " + bosDoc.size());
                ByteArrayInputStream is = new ByteArrayInputStream(bosDoc.toByteArray());
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                IPDFService isCurrent = PDFConversionService.getInstance().getPDFService();

                isCurrent.buildPDF(is, bos);
                is.close();
                bos.flush();
                logCurrent.info("Size of pdf ByteArrayOutputStream: " + bos.size());
                httpResponse.setContentType("application/octet-stream");
                httpResponse.addHeader("Content-disposition", "inline; filename=\"" + strFileName + "\"");
                OutputStream os = httpResponse.getOutputStream();
                bos.writeTo(os);
                bos.close();
                os.close();

            } else {
                logCurrent.info("Build docx");
                httpResponse.setContentType("application/octet-stream");
                httpResponse.addHeader("Content-disposition", "inline; filename=\"" + strFileName + "\"");
                OutputStream os = httpResponse.getOutputStream();
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                dxDocument.write(bos);
                bos.writeTo(os);
                bos.close();
                os.close();
            }
        } else {
            ErrorPageBuilder.getInstance().processError(httpResponse,
                    "TemplateAccess Problem NR: " + nTemplateAccess, null);
        }
    } catch (Exception e) {
        ErrorPageBuilder.getInstance().processError(httpResponse, "Error during Documentgeneration", e);
    }
}

From source file:br.com.techne.gluonsoft.eowexport.builder.WordBuilder.java

License:Apache License

/**
 * mtodo cria bytes de documento Excel/*from w w  w  .  j a va 2 s .c o  m*/
 * @param titles
 * @param columnIndex
 * @param dataRows
 * @param locale
 * @return
 * @throws Exception
 */
public static byte[] createStyledTable(String[] titles, String[] columnIndex,
        List<HashMap<String, Object>> dataRows, Locale locale) throws Exception {

    // Create a new document from scratch
    XWPFDocument doc = new XWPFDocument();
    byte[] outBytes;
    try {
        int nRows = dataRows.size() + 1;
        int nCols = columnIndex.length == 0 ? dataRows.get(0).keySet().size() : columnIndex.length;

        XWPFTable table = doc.createTable(nRows, nCols);

        // Set the table style. If the style is not defined, the table style
        // will become "Normal".
        CTTblPr tblPr = table.getCTTbl().getTblPr();
        CTString styleStr = tblPr.addNewTblStyle();
        styleStr.setVal("StyledTable");

        // Get a list of the rows in the table
        List<XWPFTableRow> rows = table.getRows();

        int rowCt = 1;

        addTitle(rows, titles);

        ValueCellUtil vcutil = new ValueCellUtil(locale);

        for (HashMap<String, Object> dataRow : dataRows) {
            addRow(rows, dataRow, rowCt, vcutil, columnIndex);
            rowCt++;
        }

        // write for return byte[]
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            doc.write(out);
            outBytes = out.toByteArray();
        } finally {
            out.close();
        }
    } finally {
        doc.close();
    }

    return outBytes;
}

From source file:certificatecreator.CertificateCreator.java

private void createStudentCertificate(String tempPath) {
    //Creates student certificate.
    //Read in template file and define output file.
    File inFile = new File(tempPath);
    File outFile = new File(userPath + File.separator + selectedStudent + "Certificate.docx");
    //Create output file. Throw alert if tempPath references nonexisting file.
    try {//w ww. ja  v a2 s. c  om
        copy(inFile, outFile);
        //Create internal word document based on copied template file. Write content.
        try {
            XWPFDocument certificate = new XWPFDocument(new FileInputStream(outFile));
            XWPFParagraph p1 = certificate.createParagraph();
            p1.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun r1 = p1.createRun();
            r1.setFontFamily("Candara");
            r1.setFontSize(40);
            r1.addBreak();
            r1.setText("Lawton Elementary Congratulates");
            XWPFRun r2 = p1.createRun();
            r2.setFontFamily("Candara");
            r2.setFontSize(36);
            r2.setBold(true);
            r2.addBreak();
            r2.addBreak();
            r2.setText(selectedStudent);
            r2.addBreak();
            r2.addBreak();
            XWPFRun r3 = p1.createRun();
            r3.setFontFamily("Candara");
            r3.setFontSize(26);
            r3.setText("For being a Lawton CARES winner on");
            r3.addBreak();
            String date = getDate();
            r3.setText(date);
            r3.addBreak();
            r3.addBreak();
            r3.addBreak();
            r3.addBreak();
            XWPFRun r4 = p1.createRun();
            r4.setColor("5B9BD5");
            r4.setFontFamily("Candara");
            r4.setFontSize(26);
            r4.setText("Compassion+Attitude+Respect+Effort+Safety=CARES");
            //Write internal document to copied templated file.
            try {
                FileOutputStream out = new FileOutputStream(outFile.toString());
                try {
                    certificate.write(out);
                    out.close();
                } catch (IOException io) {
                    System.err.println("Could not write file: " + io);
                }
            } catch (FileNotFoundException fnf) {
                System.err.println("Could not find output file: " + fnf);
            }
        } catch (IOException io) {
            System.err.println("Copy of template could not be found: " + io);
        }
    } catch (IOException io) {
        System.err.println("Method copy failed:" + io);
    } catch (NullPointerException npe) {
        System.err.println("Method copy failed:" + npe);
        Alert templateNotFound = new Alert(AlertType.WARNING,
                "Specified certificate template was not found. " + "Please choose a different template.");
        templateNotFound.showAndWait();
    }
}

From source file:ch.admin.isb.hermes5.business.word.TranslationWordAdapter.java

License:Apache License

public byte[] write(List<TranslateableText> texts, String lang) {
    XWPFDocument document = new XWPFDocument();

    // insert text into first row
    XWPFTable table = document.createTable();
    table.setCellMargins(0, 100, 0, 100);
    XWPFTableRow rowOne = table.getRow(0);
    TranslateableText first = texts.get(0);
    rowOne.getCell(0).setText(first.getElementIdentifier() + "/ " + first.getTextIdentifier());
    rowOne.addNewTableCell().setText(getText(first, lang));
    if (isDirty(first, lang)) {
        rowOne.getCell(1).setColor("FFFF99");
    }/*w w w  .  j  ava 2  s . c  om*/
    // add more rows and insert text
    if (texts.size() > 1) {
        for (int c = 1; c < texts.size(); c++) {
            TranslateableText text = texts.get(c);
            XWPFTableRow row = table.createRow();
            row.getCell(0).setText(text.getElementIdentifier() + "/ " + text.getTextIdentifier());
            row.getCell(1).setText(getText(text, lang));
            if (isDirty(text, lang)) {
                row.getCell(1).setColor("FFFF99");
            }
        }
    }

    try {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        document.write(outStream);
        return outStream.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:ch.admin.isb.hermes5.tools.translator.Translator.java

License:Apache License

public byte[] translateWordFile(InputStream in, String lang) {
    XWPFDocument document = null;
    try {//from   w ww  . j  a  va 2 s .  c  om
        document = new XWPFDocument(in);
        in.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
    }
    List<XWPFTableRow> rows = document.getTables().get(0).getRows();
    for (XWPFTableRow r : rows) {
        r.getCell(1).setText(lang + " " + lang);
    }
    try {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        document.write(outStream);
        return outStream.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}