List of usage examples for org.apache.poi.xwpf.usermodel XWPFRun setText
public void setText(String value)
From source file:com.glodon.tika.SimpleTable.java
License:Apache License
public static void createSimpleTable() throws Exception { XWPFDocument doc = new XWPFDocument(); try {//from w w w . j av a 2 s . c om XWPFTable table = doc.createTable(3, 3); table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE"); // table cells have a list of paragraphs; there is an initial // paragraph created when the cell is created. If you create a // paragraph in the document to put in the cell, it will also // appear in the document following the table, which is probably // not the desired result. XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0); XWPFRun r1 = p1.createRun(); r1.setBold(true); r1.setText("The quick brown fox"); r1.setItalic(true); r1.setFontFamily("Courier"); r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH); r1.setTextPosition(100); table.getRow(2).getCell(2).setText("only text"); OutputStream out = new FileOutputStream("simpleTable.docx"); try { doc.write(out); } finally { out.close(); } } finally { doc.close(); } }
From source file:com.glodon.tika.SimpleTable.java
License:Apache License
/** * Create a table with some row and column styling. I "manually" add the * style name to the table, but don't check to see if the style actually * exists in the document. Since I'm creating it from scratch, it obviously * won't exist. When opened in MS Word, the table style becomes "Normal". * I manually set alternating row colors. This could be done using Themes, * but that's left as an exercise for the reader. The cells in the last * column of the table have 10pt. "Courier" font. * I make no claims that this is the "right" way to do it, but it worked * for me. Given the scarcity of XWPF examples, I thought this may prove * instructive and give you ideas for your own solutions. //w w w . j av a 2 s .co m * @throws Exception */ public static void createStyledTable() throws Exception { // Create a new document from scratch XWPFDocument doc = new XWPFDocument(); try { // -- OR -- // open an existing empty document with styles already defined //XWPFDocument doc = new XWPFDocument(new FileInputStream("base_document.docx")); // Create a new table with 6 rows and 3 columns int nRows = 6; int nCols = 3; 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 = 0; int colCt = 0; for (XWPFTableRow row : rows) { // get table row properties (trPr) CTTrPr trPr = row.getCtRow().addNewTrPr(); // set row height; units = twentieth of a point, 360 = 0.25" CTHeight ht = trPr.addNewTrHeight(); ht.setVal(BigInteger.valueOf(360)); // get the cells in this row List<XWPFTableCell> cells = row.getTableCells(); // add content to each cell for (XWPFTableCell cell : cells) { // get a table cell properties element (tcPr) CTTcPr tcpr = cell.getCTTc().addNewTcPr(); // set vertical alignment to "center" CTVerticalJc va = tcpr.addNewVAlign(); va.setVal(STVerticalJc.CENTER); // create cell color element CTShd ctshd = tcpr.addNewShd(); ctshd.setColor("auto"); ctshd.setVal(STShd.CLEAR); if (rowCt == 0) { // header row ctshd.setFill("A7BFDE"); } else if (rowCt % 2 == 0) { // even row ctshd.setFill("D3DFEE"); } else { // odd row ctshd.setFill("EDF2F8"); } // get 1st paragraph in cell's paragraph list XWPFParagraph para = cell.getParagraphs().get(0); // create a run to contain the content XWPFRun rh = para.createRun(); // style cell as desired if (colCt == nCols - 1) { // last column is 10pt Courier rh.setFontSize(10); rh.setFontFamily("Courier"); } if (rowCt == 0) { // header row rh.setText("header row, col " + colCt); rh.setBold(true); para.setAlignment(ParagraphAlignment.CENTER); } else { // other rows rh.setText("row " + rowCt + ", col " + colCt); para.setAlignment(ParagraphAlignment.LEFT); } colCt++; } // for cell colCt = 0; rowCt++; } // for row // write the file OutputStream out = new FileOutputStream("styledTable.docx"); try { doc.write(out); } finally { out.close(); } } finally { 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 ? ?//from www . j ava2 s. co 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
public static void addLicenseTextsHeader(XWPFDocument document, String header) { XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); addPageBreak(run);/*from ww w . ja v a2 s . co m*/ XWPFParagraph textParagraph = document.createParagraph(); XWPFRun textRun = textParagraph.createRun(); textParagraph.setStyle("Heading1"); textRun.setText(header); addNewLines(textRun, 1); }
From source file:com.siemens.sw360.licenseinfo.outputGenerators.DocxUtils.java
License:Open Source License
public static void addLicenseTexts(XWPFDocument document, Collection<LicenseInfoParsingResult> projectLicenseInfoResults) { projectLicenseInfoResults.stream().map(LicenseInfoParsingResult::getLicenseInfo).filter(Objects::nonNull) .map(LicenseInfo::getLicenseNamesWithTexts).filter(Objects::nonNull).forEach(set -> { set.forEach(lt -> {//ww w . j av a 2 s. c o m XWPFParagraph licenseParagraph = document.createParagraph(); licenseParagraph.setStyle("Heading2"); XWPFRun licenseRun = licenseParagraph.createRun(); String licenseName = lt.isSetLicenseName() ? lt.getLicenseName() : "Unknown license name"; licenseRun.setText(licenseName); addNewLines(licenseRun, 1); XWPFParagraph licenseTextParagraph = document.createParagraph(); XWPFRun licenseTextRun = licenseTextParagraph.createRun(); addFormattedText(licenseTextRun, nullToEmptyString(lt.getLicenseText()), 12); addNewLines(licenseTextRun, 1); }); }); }
From source file:com.siemens.sw360.licenseinfo.outputGenerators.DocxUtils.java
License:Open Source License
private static void addFormattedText(XWPFRun run, String text, String fontFamily, int fontSize, boolean bold) { run.setFontSize(fontSize);//from www. j a va 2 s . c om run.setFontFamily(fontFamily); run.setBold(bold); run.setText(text); }
From source file:com.simplexwork.mysql.tools.utils.DocumentUtils.java
License:Apache License
/** * ??word//from w w w. ja v a 2s .co m * * @param tablesMap * @throws Exception */ public static void productDatabaseDoc(Map<String, TableInfo> tablesMap) throws Exception { XWPFDocument xwpfDocument = new XWPFDocument(); for (Entry<String, TableInfo> entry : tablesMap.entrySet()) { TableInfo tableInfo = entry.getValue(); XWPFParagraph xwpfParagraph = xwpfDocument.createParagraph(); XWPFRun xwpfRun = xwpfParagraph.createRun(); xwpfRun.setText(tableInfo.getTableName() + "(" + tableInfo.getTableComment() + ")"); xwpfRun.setFontSize(18); xwpfRun.setTextPosition(10); XWPFTable xwpfTable = xwpfDocument.createTable(tableInfo.getColumns().size() + 1, 6); int i = 0; xwpfTable.getRow(i).getCell(0).setText("??"); xwpfTable.getRow(i).getCell(1).setText(""); xwpfTable.getRow(i).getCell(2).setText(""); xwpfTable.getRow(i).getCell(3).setText(""); xwpfTable.getRow(i).getCell(4).setText(""); xwpfTable.getRow(i).getCell(5).setText("?"); for (Column column : tableInfo.getColumns()) { int j = 0; i++; xwpfTable.getRow(i).getCell(j++).setText(column.getName()); xwpfTable.getRow(i).getCell(j++).setText(column.getType()); xwpfTable.getRow(i).getCell(j++).setText(column.isKey() ? "" : ""); xwpfTable.getRow(i).getCell(j++).setText(column.isNullable() ? "?" : ""); xwpfTable.getRow(i).getCell(j++).setText(column.getComment()); xwpfTable.getRow(i).getCell(j++).setText(column.getExtra()); } } File file = new File(generatedPath); if (!file.exists()) { file.mkdirs(); } FileOutputStream fos = new FileOutputStream(generatedPath + "database.docx"); xwpfDocument.write(fos); fos.close(); }
From source file:csv2docxconverter.DocumentGenerator.java
/** * Generate DocX element from list of the rows containing account information * @param columnNames a list of columns for data parsing * @param contents list of data rows for parsing * @return an XWPFDocument representing a DocX file *///from www .j ava 2 s . c om public XWPFDocument generateDocx(String[] columnNames, List contents) { XWPFDocument document = new XWPFDocument(); // create title XWPFParagraph title = document.createParagraph(); title.setAlignment(ParagraphAlignment.CENTER); XWPFRun titleRun = title.createRun(); titleRun.setText("G SUITE created accounts"); titleRun.setFontSize(18); for (int k = 0; k < contents.size(); k++) { List tableContent = (List) contents.get(k); // create title title = document.createParagraph(); title.setAlignment(ParagraphAlignment.CENTER); titleRun = title.createRun(); titleRun.setText("Table " + (k + 1)); titleRun.setFontSize(18); // create account table XWPFTable table = document.createTable(); // set "justified" alignment table.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(10000)); //create header for table setHeader(table, columnNames); // if account list is empty if (tableContent == null || tableContent.size() == 0) { // add empty row to the table XWPFTableRow emptyRow = table.createRow(); } else { String[] headerRow = null; // create rows in table for (int i = 0; i < tableContent.size(); i++) { if (i == 0) { headerRow = (String[]) tableContent.get(i); continue; } String[] csvRow = (String[]) tableContent.get(i); XWPFTableRow row = table.createRow(); //create cells in a row for (int j = 0; j < columnNames.length; j++) { int number = getColumnNumber(columnNames[j], headerRow); XWPFTableCell cell = row.getCell(j); if (cell != null) { XWPFRun run = setBodyCell(cell); if (number >= 0 && number < csvRow.length) { run.setText(csvRow[number]); } } } } } } return document; }
From source file:csv2docxconverter.DocumentGenerator.java
/** * Create header for a table// w w w .jav a2 s .c om */ private void setHeader(XWPFTable table, String[] columnNames) { // set initial cell XWPFTableRow tableRowOne = table.getRow(0); // add other cells for (int i = 0; i < columnNames.length; i++) { XWPFTableCell cell; if (i == 0) { cell = tableRowOne.getCell(0); } else { cell = tableRowOne.addNewTableCell(); } XWPFRun run = setHeaderCell(cell); run.setText(columnNames[i]); } }
From source file:de.knowwe.include.export.DefaultBuilder.java
License:Open Source License
private XWPFRun append(String text, XWPFRun run) { if (bold)//w w w .j a v a 2 s . c o m run.setBold(true); if (italic) run.setItalic(true); if (code) run.setFontFamily("Courier New"); run.setText(text); return run; }