List of usage examples for org.apache.poi.xwpf.usermodel XWPFDocument XWPFDocument
public XWPFDocument()
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 *///w w w .ja va 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: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();// w w w .j a va2 s. c o m 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 r.addBreak(BreakType.PAGE); } FileOutputStream out = new FileOutputStream("images.docx"); doc.write(out); out.close(); // doc.close(); }
From source file:editordetext.IO.java
public void save(String txt) { if (!path.equals("")) { switch (fileType) { case "txt": ArrayList<String> lines = new ArrayList(); Path file = Paths.get(path); lines.add(txt);/*from w w w.j ava 2 s.co m*/ try { Files.write(file, lines, Charset.forName("UTF-8")); } catch (IOException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); } break; case "Word": XWPFDocument document = new XWPFDocument(); try { FileOutputStream out = new FileOutputStream(new File(path)); XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText(txt); document.write(out); out.close(); } catch (FileNotFoundException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); } break; case "PDF": try { Document pdf = new Document(); PdfWriter.getInstance(pdf, new FileOutputStream(path)); pdf.open(); pdf.add(new Paragraph(txt)); pdf.close(); } catch (DocumentException | FileNotFoundException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); } break; } } else { new Save().setVisible(true); } }
From source file:editordetext.IO.java
public void save(String txt, String path) { System.out.println(path);/* w w w. ja v a2 s. c o m*/ if (!path.equals("")) { switch (fileType) { case "txt": ArrayList<String> lines = new ArrayList(); Path file = Paths.get(path); lines.add(txt); try { Files.write(file, lines, Charset.forName("UTF-8")); } catch (IOException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); } break; case "Word": XWPFDocument document = new XWPFDocument(); try { FileOutputStream out = new FileOutputStream(new File(path)); XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText(txt); document.write(out); out.close(); } catch (FileNotFoundException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); } break; case "PDF": try { Document pdf = new Document(); PdfWriter.getInstance(pdf, new FileOutputStream(path)); pdf.open(); pdf.add(new Paragraph(txt)); pdf.close(); } catch (DocumentException | FileNotFoundException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); } break; } } else { new Save().setVisible(true); } }
From source file:edu.cqupt.test.SimpleDocument.java
License:Apache License
public static void main(String[] args) throws Exception { XWPFDocument doc = new XWPFDocument(); XWPFParagraph p1 = doc.createParagraph(); p1.setAlignment(ParagraphAlignment.CENTER); p1.setBorderBottom(Borders.DOUBLE);//from w w w. ja va 2 s . c om p1.setBorderTop(Borders.DOUBLE); p1.setBorderRight(Borders.DOUBLE); p1.setBorderLeft(Borders.DOUBLE); p1.setBorderBetween(Borders.SINGLE); p1.setVerticalAlignment(TextAlignment.TOP); XWPFRun r1 = p1.createRun(); r1.setBold(true); r1.setText("The quick brown fox"); r1.setBold(true); r1.setFontFamily("Courier"); r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH); r1.setTextPosition(100); XWPFParagraph p2 = doc.createParagraph(); p2.setAlignment(ParagraphAlignment.RIGHT); // BORDERS p2.setBorderBottom(Borders.DOUBLE); p2.setBorderTop(Borders.DOUBLE); p2.setBorderRight(Borders.DOUBLE); p2.setBorderLeft(Borders.DOUBLE); p2.setBorderBetween(Borders.SINGLE); XWPFRun r2 = p2.createRun(); r2.setText("jumped over the lazy dog"); //r2.setStrike(true); r2.setFontSize(20); XWPFRun r3 = p2.createRun(); r3.setText("and went away"); //r3.setStrike(true); r3.setFontSize(20); r3.setSubscript(VerticalAlign.SUPERSCRIPT); XWPFParagraph p3 = doc.createParagraph(); //p3.setWordWrap(true); p3.setPageBreak(true); // p3.setAlignment(ParagraphAlignment.DISTRIBUTE); p3.setAlignment(ParagraphAlignment.BOTH); p3.setSpacingLineRule(LineSpacingRule.EXACT); p3.setIndentationFirstLine(600); XWPFRun r4 = p3.createRun(); r4.setTextPosition(20); r4.setText("??? " + "Whether 'tis nobler in the mind to suffer " + "The slings and arrows of outrageous fortune, " + "Or to take arms against a sea of troubles, " + "And by opposing end them? To die: to sleep; "); r4.addBreak(BreakType.PAGE); r4.setText("No more; and by a sleep to say we end " + "The heart-ache and the thousand natural shocks " + "That flesh is heir to, 'tis a consummation " + "Devoutly to be wish'd. To die, to sleep; " + "To sleep: perchance to dream: ay, there's the rub; " + "......."); r4.setItalic(true); // This would imply that this break shall be treated as a simple line // break, and break the line after that word: XWPFRun r5 = p3.createRun(); r5.setTextPosition(-10); r5.setText("For in that sleep of death what dreams may come"); r5.addCarriageReturn(); r5.setText("When we have shuffled off this mortal coil," + "Must give us pause: there's the respect" + "That makes calamity of so long life;"); r5.addBreak(); r5.setText("For who would bear the whips and scorns of time," + "The oppressor's wrong, the proud man's contumely,"); r5.addBreak(BreakClear.ALL); r5.setText("The pangs of despised love, the law's delay," + "The insolence of office and the spurns" + "......."); FileOutputStream out = new FileOutputStream("F://simple.docx"); doc.write(out); out.close(); }
From source file:eremeykin.pete.reports.ui.ReportAction.java
@Override public void actionPerformed(ActionEvent e) { resultChanged(null);//from w ww . j a va 2s . 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); } }
From source file:File.DOCX.WriteDocx.java
public WriteDocx() { docx = new XWPFDocument(); sectPr = docx.getDocument().getBody().addNewSectPr(); policy = new XWPFHeaderFooterPolicy(docx, sectPr); }
From source file:FilesHandlers.WordHandler.java
public void changeLine(String docName, int row, String newLine) throws Exception { String[] strArr = getDocContentByLine(docName); StringBuilder strBuilder = new StringBuilder(); for (int i = 0; i < strArr.length; i++) { if (row == i + 1) { System.out.println("s s s s"); strBuilder.append(newLine);/*from w w w . j a va2 s . c o m*/ } else { strBuilder.append(strArr[i]); } strBuilder.append("\n"); } String content = strBuilder.toString(); System.out.println(content); // Blank Document XWPFDocument document = new XWPFDocument(); // Write the Document in file system FileOutputStream out = new FileOutputStream(new File(workingDirectory.concat(docName))); // create Paragraph XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText(content); document.write(out); out.close(); System.out.println("It was updated succesfully"); }
From source file:IsiXhosa_spellchecker.Spellchecker.java
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed // Save file being edited to machine int len = textArea.getText().length(); boolean English = jRadioButton1.isSelected(); if (len == 0) { if (English) { instruction.setText("You are trying to save an empty file!"); instruction.setForeground(Color.RED); } else {//from ww w . jav a 2 s. co m instruction.setText("Uzama ukugcina ifayile engenanto!"); instruction.setForeground(Color.RED); } //System.out.println(data); } else { int returnVal = fileChooser.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { // What to do with the file, e.g. display it in a TextArea if (highlightSet) { highlighter.removeAllHighlights(); } if (file.getName().endsWith(".docx")) { XWPFDocument document = new XWPFDocument(); XWPFParagraph tmpParagraph = document.createParagraph(); XWPFRun tmpRun = tmpParagraph.createRun(); tmpRun.setText(textArea.getText()); tmpRun.setFontSize(12); document.write(new FileOutputStream(new File(file.getPath()))); } else { textArea.write(new FileWriter(file.getAbsolutePath()));//this file has no extension } } catch (IOException ex) { System.out.println("problem accessing file" + file.getAbsolutePath()); } } } }
From source file:isizulu_spellchecker.Spellchecker.java
private void saveAsAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsAsActionPerformed // TODO add your handling code here: int returnVal = fileChooser.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try {// w w w . j a va 2s .com // What to do with the file, e.g. display it in a TextArea if (highlightSet) { highlighter.removeAllHighlights(); } if (file.getName().endsWith(".docx")) { XWPFDocument document = new XWPFDocument(); XWPFParagraph tmpParagraph = document.createParagraph(); XWPFRun tmpRun = tmpParagraph.createRun(); tmpRun.setText(textArea.getText()); tmpRun.setFontSize(12); document.write(new FileOutputStream(new File(file.getPath()))); } else { textArea.write(new FileWriter(file.getAbsolutePath()));//this file has no extension } } catch (IOException ex) { System.out.println("problem accessing file" + file.getAbsolutePath()); } } }