Example usage for org.apache.poi.xwpf.usermodel ParagraphAlignment LEFT

List of usage examples for org.apache.poi.xwpf.usermodel ParagraphAlignment LEFT

Introduction

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

Prototype

ParagraphAlignment LEFT

To view the source code for org.apache.poi.xwpf.usermodel ParagraphAlignment LEFT.

Click Source Link

Usage

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 ? ?//from w w  w.j a  va2s  . 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:com.siemens.sw360.licenseinfo.outputGenerators.DocxUtils.java

License:Open Source License

private static void styleTableHeaderParagraph(XWPFParagraph paragraph) {
    paragraph.setIndentationLeft(0);/*from ww  w.j a  v a2s.  co m*/
    paragraph.setWordWrap(true);
    paragraph.setAlignment(ParagraphAlignment.LEFT);
}

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

@Override
public void actionPerformed(ActionEvent e) {
    resultChanged(null);//w ww  . ja  v  a2 s .  c o 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);
    }
}

From source file:File.DOCX.WriteDocx.java

public void Write(String header, String footer, String kalimat, String alignment, String path) {
    try {/*from   ww w  .ja  v  a  2 s  . c  om*/

        CreateHeader(header);
        CreateFooter(footer);
        ParagraphAlignment align = null;
        if (alignment.equalsIgnoreCase("left")) {
            align = ParagraphAlignment.LEFT;
        } else if (alignment.equalsIgnoreCase("right")) {
            align = ParagraphAlignment.RIGHT;
        } else if (alignment.equalsIgnoreCase("center")) {
            align = ParagraphAlignment.CENTER;
        }
        //write body content
        String[] split_kalimat = kalimat.split("\n");
        for (String text : split_kalimat) {
            XWPFParagraph bodyParagraph = docx.createParagraph();
            bodyParagraph.setAlignment(align);
            XWPFRun r = bodyParagraph.createRun();
            r.setText(text);
        }

        FileOutputStream out = new FileOutputStream(path);
        docx.write(out);
        out.close();
        System.out.println("Done");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.TableCellVerticalAlignmentTestCase.java

License:Open Source License

private void testsA(XWPFParagraph paragraph, XWPFStylesDocument stylesDocument) {
    // vertical aligment
    XWPFTableCell cell = (XWPFTableCell) paragraph.getBody();
    Enum jc = stylesDocument.getTableCellVerticalAlignment(cell);
    Assert.assertNull(jc);/*from w ww .ja v  a  2 s .c  o m*/

    // text aligment
    ParagraphAlignment alignment = stylesDocument.getParagraphAlignment(paragraph);
    if (alignment == null) {
        Assert.assertNull(alignment);
    } else {
        Assert.assertEquals(ParagraphAlignment.LEFT, alignment);
    }
}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.TableCellVerticalAlignmentTestCase.java

License:Open Source License

private void testsD(XWPFParagraph paragraph, XWPFStylesDocument stylesDocument) {
    // vertical aligment
    XWPFTableCell cell = (XWPFTableCell) paragraph.getBody();
    Enum jc = stylesDocument.getTableCellVerticalAlignment(cell);
    Assert.assertNotNull(jc);// ww  w . j ava2s .  c o m
    Assert.assertEquals(jc.intValue(), STVerticalJc.INT_CENTER);

    // text aligment
    ParagraphAlignment alignment = stylesDocument.getParagraphAlignment(paragraph);
    if (alignment == null) {
        Assert.assertNull(alignment);
    } else {
        Assert.assertEquals(ParagraphAlignment.LEFT, alignment);
    }
}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.TableCellVerticalAlignmentTestCase.java

License:Open Source License

private void testsG(XWPFParagraph paragraph, XWPFStylesDocument stylesDocument) {
    // vertical aligment
    XWPFTableCell cell = (XWPFTableCell) paragraph.getBody();
    Enum jc = stylesDocument.getTableCellVerticalAlignment(cell);
    Assert.assertNotNull(jc);//from w  w  w.  j a  v  a 2  s  . c  o m
    Assert.assertEquals(jc.intValue(), STVerticalJc.INT_BOTTOM);

    // text aligment
    ParagraphAlignment alignment = stylesDocument.getParagraphAlignment(paragraph);
    if (alignment == null) {
        Assert.assertNull(alignment);
    } else {
        Assert.assertEquals(ParagraphAlignment.LEFT, alignment);
    }
}

From source file:MainPackage.Controllers.BookController.java

private XWPFParagraph createPrintInformation(XWPFDocument document, Account _account) {
    XWPFParagraph paragraph = document.createParagraph();

    paragraph.setAlignment(ParagraphAlignment.LEFT);
    XWPFRun run = createFieldRun(paragraph, "Ngy in: ");
    run.setFontSize(10);//from ww  w .  j  ava 2s.c  om
    run = createValueRun(paragraph, getCurrentDateString());
    run.setFontSize(10);
    run.addCarriageReturn();
    run = createFieldRun(paragraph, "Th?i gian in: ");
    run.setFontSize(10);
    run = createValueRun(paragraph, getCurrentTimeString());
    run.setFontSize(10);
    run.addCarriageReturn();
    run = createFieldRun(paragraph, "Ti khon in: ");
    run.setFontSize(10);
    run = createValueRun(paragraph, _accountProvider.getById(_account.getId()).getUsername());
    run.setFontSize(10);
    run.addCarriageReturn();

    return paragraph;
}

From source file:MainPackage.Controllers.BookController.java

private XWPFParagraph createBookProductInformation(XWPFDocument document, BookViewModel bookView) {
    XWPFParagraph paragraph = document.createParagraph();

    paragraph.setAlignment(ParagraphAlignment.LEFT);
    XWPFRun run = createFieldRun(paragraph, "Thng tin Sn phm: ");
    run.setFontSize(16);/* w w w. j  a  va 2  s.c  om*/
    run.addCarriageReturn();

    ArrayList<PrintRunField> productInformations = new ArrayList<PrintRunField>() {
        {
            add(new PrintRunField("M sch: ", bookView.IdCode));
            add(new PrintRunField("M ISBN: ", bookView.ISBN));
            add(new PrintRunField("Ti khon to: ", bookView.CreatedBy));
            add(new PrintRunField("Th?i gian to: ", bookView.CreateTime));
            add(new PrintRunField("Gi sch: ", bookView.Price));
            add(new PrintRunField("Tnh trng: ", bookView.Status));
        }
    };

    for (int i = 0; i < productInformations.size(); i++) {

        run = createFieldRun(paragraph, productInformations.get(i).Field);
        run.addTab();
        run = createValueRun(paragraph, productInformations.get(i).Value);
        if (i % 2 == 0) {
            run.addTab();
        } else {
            run.addCarriageReturn();
        }
    }

    return paragraph;
}

From source file:MainPackage.Controllers.OrderController.java

private XWPFParagraph createBookProductInformation(XWPFDocument document, Orders order) {
    XWPFParagraph paragraph = document.createParagraph();

    paragraph.setAlignment(ParagraphAlignment.LEFT);
    XWPFRun run = createFieldRun(paragraph, "Thng tin Ha n: ");
    run.setFontSize(16);/*from  w ww  . j  a v  a2 s  . c  o  m*/
    run.addCarriageReturn();

    ArrayList<BookController.PrintRunField> productInformations = new ArrayList<BookController.PrintRunField>() {
        {
            add(new BookController.PrintRunField("M Ha n: ", order.getIdCode()));
            add(new BookController.PrintRunField("Ti khon lp: ", order.getCreateBy().getUsername()));
            add(new BookController.PrintRunField("Th?i gian lp: ",
                    getDateTimeString(order.getCreateTime())));
            add(new BookController.PrintRunField(" ", ""));
            add(new BookController.PrintRunField("Tng ti?n: ", IntToVND(order.getTotalPrice())));
            add(new BookController.PrintRunField("Thu VAT: ", IntToVND(order.getVATPrice())));
            add(new BookController.PrintRunField("Khuyn mi: ", IntToVND(order.getDiscount())));
            add(new BookController.PrintRunField("Thanh ton: ", IntToVND(order.getPaidPrice())));
        }
    };

    for (int i = 0; i < productInformations.size(); i++) {

        run = createFieldRun(paragraph, productInformations.get(i).Field);
        run.addTab();
        run = createValueRun(paragraph, productInformations.get(i).Value);
        run.addCarriageReturn();
    }

    return paragraph;
}