List of usage examples for org.apache.poi.xwpf.usermodel XWPFDocument write
@SuppressWarnings("resource") public final void write(OutputStream stream) throws IOException
From source file:ch.admin.searchreplace.SearchReplaceTerms.java
License:Apache License
/** * Ersetzt eine Liste von Ausdruecken in einer Datei und schreibt das Resultat nach Output * @param input Inputdatei/*from w w w. ja v a 2 s .co m*/ * @param output Outputdatei * @param srTerms Begriff * @throws IOException I/O Fehler * @throws InvalidFormatException OpenXML Document korrupt * @throws FileNotFoundException Datei nicht vorhanden */ private static void searchReplaceInFile(String input, String output, HashMap<String, String> srTerms) throws IOException, InvalidFormatException, FileNotFoundException { XWPFDocument doc = new XWPFDocument(OPCPackage.open(input)); // Header List<XWPFHeader> header = doc.getHeaderList(); for (Iterator<XWPFHeader> e = header.iterator(); e.hasNext();) { XWPFHeader h = e.next(); for (XWPFParagraph p : h.getParagraphs()) { XWPFRun r = consolidateRuns(p); if (r != null) searchReplace(srTerms, r); } for (XWPFTable tbl : h.getTables()) for (XWPFTableRow row : tbl.getRows()) for (XWPFTableCell cell : row.getTableCells()) for (XWPFParagraph p : cell.getParagraphs()) { XWPFRun r = consolidateRuns(p); if (r != null) searchReplace(srTerms, r); } } // Document for (XWPFParagraph p : doc.getParagraphs()) { XWPFRun r = consolidateRuns(p); if (r != null) searchReplace(srTerms, r); } for (XWPFTable tbl : doc.getTables()) for (XWPFTableRow row : tbl.getRows()) for (XWPFTableCell cell : row.getTableCells()) for (XWPFParagraph p : cell.getParagraphs()) { XWPFRun r = consolidateRuns(p); if (r != null) searchReplace(srTerms, r); } doc.write(new FileOutputStream(output)); }
From source file:ch.admin.searchreplace.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 ww. ja v a 2 s . com 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("To be, or not to be: that is the question: " + "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("simple.docx"); doc.write(out); out.close(); }
From source file:cn.afterturn.easypoi.view.EasypoiTemplateWordView.java
License:Apache License
@Override protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { String codedFileName = ".docx"; if (model.containsKey(TemplateWordConstants.FILE_NAME)) { codedFileName = (String) model.get(TemplateWordConstants.FILE_NAME) + ".docx"; }/*from w ww . j a va2s . c o m*/ // ??RFC 5987??header, UA response.setHeader("content-disposition", WebFilenameUtils.disposition(codedFileName)); XWPFDocument document = WordExportUtil.exportWord07((String) model.get(TemplateWordConstants.URL), (Map<String, Object>) model.get(TemplateWordConstants.MAP_DATA)); ServletOutputStream out = response.getOutputStream(); document.write(out); out.flush(); }
From source file:cn.wantedonline.porobot.DataBaseEntity.java
License:Apache License
private static void genDBDictionary(List<DBTableBean> tableBeans) throws IOException { XWPFDocument doc = new XWPFDocument(); String databaseName = "default_database"; for (DBTableBean bean : tableBeans) { databaseName = bean.getDatabaseName(); XWPFParagraph p0 = doc.createParagraph(); p0.setAlignment(ParagraphAlignment.CENTER); XWPFRun r0 = p0.createRun();/*from w w w. ja va 2s . c o m*/ r0.setBold(true); r0.setText(databaseName); r0.setFontSize(16); XWPFParagraph p1 = doc.createParagraph(); p1.setAlignment(ParagraphAlignment.LEFT); XWPFRun r1 = p1.createRun(); r1.setBold(true); r1.setText("Table:" + bean.getTableName()); XWPFTable table = doc.createTable(); int pos = 0; XWPFTableRow row1 = table.insertNewTableRow(pos++); XWPFTableCell cell1 = row1.createCell(); cell1.setText("Field"); cell1.setColor("FFE4C4"); XWPFTableCell cell2 = row1.createCell(); cell2.setText("Type"); cell2.setColor("FFE4C4"); XWPFTableCell cell3 = row1.createCell(); cell3.setText("Comment"); cell3.setColor("FFE4C4"); for (int i = 0; i < bean.getColumnSize(); i++) { XWPFTableRow row = table.insertNewTableRow(pos++); XWPFTableCell cell11 = row.createCell(); cell11.setText(bean.getColumnName(i)); XWPFTableCell cell22 = row.createCell(); cell22.setText(bean.getColumnType(i)); XWPFTableCell cell33 = row.createCell(); cell33.setText(bean.getColumnComment(i)); } table.removeRow(pos); XWPFParagraph p2 = doc.createParagraph(); XWPFRun r2 = p2.createRun(); r2.addBreak(); } FileOutputStream out = new FileOutputStream(databaseName + ".docx"); doc.write(out); out.close(); doc.close(); }
From source file:colina.angel.controller.CreadorController.java
private void procesar() { try {//from w w w . ja va 2 s .c o m fechaActual = new Date(); InputStream st = ClassLoader.getSystemResource("Carta de Renuncia.docx").openStream(); XWPFDocument document = new XWPFDocument(st); Field fields[] = ReflectionUtils.getAllFields(this.getClass()); document.getParagraphs().parallelStream().forEach(p -> { StringBuilder sb = new StringBuilder(p.getText()); Arrays.stream(fields).forEach(f -> { StringBuilder name = new StringBuilder(); name.append("{").append(f.getName()).append("}"); int start = sb.indexOf(name.toString()); if (start != -1) { String value = ""; if (f.getAnnotation(CustomValue.class) != null) { value = getCustomValue(f.getName()); } else if (JDateChooser.class.isAssignableFrom(f.getType())) { value = formatearFecha( ((JDateChooser) ReflectionUtils.getFieldValue(f, this)).getDate()); } else if (JTextField.class.isAssignableFrom(f.getType())) { value = ((JTextField) ReflectionUtils.getFieldValue(f, this)).getText(); } else if (JTextArea.class.isAssignableFrom(f.getType())) { value = ((JTextArea) ReflectionUtils.getFieldValue(f, this)).getText(); } sb.replace(start, start + name.length(), value); changeText(p, sb.toString()); } }); }); int opc = fileChooser.showSaveDialog(this); if (opc == 0) { File f = fileChooser.getSelectedFile(); if (!f.getName().endsWith(".docx")) { f = new File(f.toString().concat(".docx")); } FileOutputStream fo = new FileOutputStream(f); BufferedOutputStream bos = new BufferedOutputStream(fo); document.write(bos); JOptionPane.showMessageDialog(this, "La carta ha sido guardada en la siguiente ruta \"".concat(f.toString()).concat("\""), "Carta guardada con exito", JOptionPane.INFORMATION_MESSAGE); } } catch (IOException ex) { LOG.log(Level.SEVERE, null, ex); JOptionPane.showMessageDialog(this, ex.getMessage(), "Error al guardar", JOptionPane.ERROR_MESSAGE); } }
From source file:com.altar.worddocreader.WordDocReaderMain.java
public static void readWordDoc(String orginalFilePath, String newFilePath, HashMap<String, String> keyMap) { FileInputStream fis = null;/* w ww .ja v a 2 s .c om*/ FileOutputStream out = null; try { fis = new FileInputStream(orginalFilePath); XWPFDocument doc = new XWPFDocument(fis); doc = replaceText(doc, keyMap); out = new FileOutputStream(new File(newFilePath)); // yeni dosya oluturuluyor. doc.write(out); } catch (Exception e) { e.printStackTrace(); } finally { if (fis != null) { try { if (out != null) { out.close(); } fis.close(); out = null; fis = null; } catch (IOException ioEx) { ioEx.printStackTrace(); } } } }
From source file:com.Base.JavaApplication1.java
public static void save(String path) { try {//from ww w . j ava 2s . c o m // JOptionPane.showMessageDialog(null, "Start ImageDoc","Error",JOptionPane.INFORMATION_MESSAGE); //String comment=comment; XWPFDocument docx = new XWPFDocument(); // JOptionPane.showMessageDialog(null, "create ImageDoc","Error",JOptionPane.INFORMATION_MESSAGE); XWPFParagraph par = docx.createParagraph(); XWPFRun run = par.createRun(); run.setText(comment); run.setFontSize(13); FileOutputStream out = new FileOutputStream(path); //JOptionPane.showMessageDialog(null, "Dir"+dir+"\\"+name,"Error",JOptionPane.INFORMATION_MESSAGE); // byte [] picbytes = IOUtils.toByteArray(pic); // run.setText("Software Genrated File"); File f = new File("C:\\images\\*.*"); String str = f.getParent(); File k = new File(str); String list[] = k.list(); JOptionPane.showMessageDialog(null, "Reading Files", "Error", JOptionPane.INFORMATION_MESSAGE); for (int y = 0; y < list.length; y++) { InputStream pic = new FileInputStream("C:\\images\\" + list[y]); XWPFRun run2 = par.createRun(); // run2.setText("Screen Shot No."+(y+1)); run2.addPicture(pic, Document.PICTURE_TYPE_JPEG, "C:\\images\\" + list[y], Units.toEMU(500), Units.toEMU(350)); // XWPFRun run2 = title.createRun(); run2.setText("ScreenShot :" + (y + 1)); pic.close(); System.gc(); } //run.setText("Hello, World. This is my first java generated docx-file. Have fun2shhhh."); // FileInputStream pic2 = new FileInputStream("D:\\i.png"); //byte [] picbytes2 = IOUtils.toByteArray(pic); //docx.addPictureData(IOUtils.toByteArray(pic2), Document.PICTURE_TYPE_PNG); // docx.createParagraph().createRun().addPicture(pic, Document.PICTURE_TYPE_JPEG, "D:\\images\\image0.jpg", Units.toEMU(500), Units.toEMU(500)); XWPFParagraph title = docx.createParagraph(); XWPFRun run2 = title.createRun(); run2.setText(" "); run2.setBold(true); title.setAlignment(ParagraphAlignment.CENTER); JOptionPane.showMessageDialog(null, "Writing File", "Error", JOptionPane.INFORMATION_MESSAGE); System.out.println(dir + name); docx.write(out); out.close(); // pic.close(); JOptionPane.showMessageDialog(null, "Operation Complete", "Error", JOptionPane.INFORMATION_MESSAGE); } catch (Exception e) { JOptionPane.showMessageDialog(null, "No Image to save!" + e, "Error", JOptionPane.ERROR_MESSAGE); } }
From source file:com.bxf.hradmin.testgen.service.impl.DocxTestGenerator.java
License:Open Source License
@Override public void generate(String versionOid, List<QuestionSnapshot> questions) { File docxFile = new File(getRootPath(versionOid), "T-Java.docx"); try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(docxFile)); InputStream is = TestGenerator.class.getResourceAsStream("/template/questions.docx")) { XWPFDocument doc = new XWPFDocument(is); replaceWordHolder(doc);/*ww w . j a v a 2s .co m*/ XWPFTable table = doc.getTables().get(0); for (QuestionSnapshot question : questions) { XWPFTableRow row = table.createRow(); writeQuestionNo(question.getQuestionNo(), row); writeQuestion(question, row); } // template row table.removeRow(0); // setBorders(table); // doc.write(bos); File[] unzipFiles = { docxFile, new File(getRootPath(versionOid), "A-Java.txt") }; File destination = new File(getRootPath(versionOid), "T-Java-docx.zip"); ZipUtils.zip(destination, true, null, unzipFiles); } catch (IOException e) { log.error(e.getMessage(), e); throw new TestGenException(e.getMessage(), ModuleInfo.TestGenMgr); } }
From source file:com.camel.util.WordDocReader.java
public File readWordDoc(String orginalFilePath, File newFile, String content) { FileInputStream fis = null;// w w w . ja v a 2 s .c om FileOutputStream out = null; try { JTextPane textPane = new JTextPane(); textPane.setText(content); StyledDocument sdoc = textPane.getStyledDocument(); fis = new FileInputStream(orginalFilePath); XWPFDocument doc = new XWPFDocument(fis); XWPFParagraph paragraph = doc.createParagraph(); XWPFRun run = paragraph.createRun(); System.out.println("sonuc..:" + sdoc.getText(0, sdoc.getEndPosition().getOffset())); run.setText(sdoc.getText(0, sdoc.getEndPosition().getOffset())); out = new FileOutputStream(newFile); // yeni dosya oluturuluyor. doc.write(out); } catch (Exception e) { e.printStackTrace(); } finally { if (fis != null) { try { out.close(); fis.close(); out = null; fis = null; } catch (IOException ioEx) { ioEx.printStackTrace(); } } } return newFile; }
From source file:com.deepoove.poi.tl.SimpleTable.java
License:Apache License
public static void createSimpleTable() throws Exception { XWPFDocument doc = new XWPFDocument(); 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();// ww w. j a v a2 s. co m 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"); FileOutputStream out = new FileOutputStream("simpleTable.docx"); doc.write(out); out.close(); }