XWPFDocument document = new XWPFDocument(); XWPFParagraph tmpParagraph = document.createParagraph(); XWPFRun tmpRun = tmpParagraph.createRun(); tmpRun.setText("LALALALAALALAAAA"); tmpRun.setFontSize(18); document.write(new FileOutputStream(new File("yourpathhere"))); document.close();
XWPFDocument doc = new XWPFDocument(OPCPackage.open("input.docx")); for (XWPFParagraph p : doc.getParagraphs()) { List<XWPFRun> runs = p.getRuns(); if (runs != null) { for (XWPFRun r : runs) { String text = r.getText(0); if (text != null && text.contains("needle")) { text = text.replace("needle", "haystack"); r.setText(text, 0); } } } } for (XWPFTable tbl : doc.getTables()) { for (XWPFTableRow row : tbl.getRows()) { for (XWPFTableCell cell : row.getTableCells()) { for (XWPFParagraph p : cell.getParagraphs()) { for (XWPFRun r : p.getRuns()) { String text = r.getText(0); if (text.contains("needle")) { text = text.replace("needle", "haystack"); r.setText(text); } } } } } } doc.write(new FileOutputStream("output.docx"));
/** * */ public void createTOC() { CTSdtBlock block = this.getDocument().getBody().addNewSdt(); TOC toc = new TOC(block); for (XWPFParagraph par : paragraphs) { String parStyle = par.getStyle(); if (parStyle != null && parStyle.startsWith("Heading")) { try { int level = Integer.parseInt(parStyle.substring("Heading".length())); toc.addRow(level, par.getText(), 1, "112723803"); } catch (NumberFormatException e) { LOG.log(POILogger.ERROR, "can't format number in TOC heading", e); } } } }
private void setFooterReference(Enum type, XWPFHeaderFooter wrapper) { CTHdrFtrRef ref = doc.getDocument().getBody().getSectPr().addNewFooterReference(); ref.setType(type); ref.setId(doc.getRelationId(wrapper)); }
final XWPFDocument docx = new XWPFDocument(new FileInputStream(new File(inFileNameString))); final FileOutputStream out = new FileOutputStream(outFileNameString); docx.write(out); out.close(); docx.close();
String outpath = "F:\\Test.docx"; XWPFDocument doc = new XWPFDocument(new FileInputStream(filepath)); for (XWPFParagraph p : doc.getParagraphs()){ int numberOfRuns = p.getRuns().size(); for (XWPFRun r : p.getRuns()){ int pos = r.getTextPosition(); if(r.getText(pos) != null) { sb.append(r.getText(pos)); p.removeRun(i); doc.write(new FileOutputStream(outpath));
XWPFDocument wordDocument = new XWPFDocument(new FileInputStream(docxFile)); POIXMLProperties props = workDocument.getProperties(); String thumbnail = props.getThumbnailFilename(); if (thumbnail == null) { // No thumbnail } else { FileOutputStream fos = new FileOutputStream("c:\\temp\\"+thumbnail); IOUtils.copy(props.getThumbnailImage(), fos); }
XWPFDocument doc = new XWPFDocument(); XWPFParagraph title = doc.createParagraph(); XWPFRun run = title.createRun(); run.setText("Fig.1 A Natural Scene"); run.setBold(true); title.setAlignment(ParagraphAlignment.CENTER); String imgFile = "encabezado.jpg" FileInputStream is = new FileInputStream(imgFile); run.addBreak(); run.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels is.close(); FileOutputStream fos = new FileOutputStream("test4.docx"); doc.write(fos); fos.close();
XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraphOne = document.createParagraph(); XWPFRun paragraphOneRunOne = paragraphOne.createRun(); paragraphOneRunOne.setBold(true); paragraphOneRunOne.setItalic(true); paragraphOneRunOne.setText("Hello world! This is paragraph one!"); paragraphOneRunOne.addBreak(); FileOutputStream outStream = new FileOutputStream("D:\\Users\\user2777005\\Desktop\\MyDov.docx"); document.write(outStream); outStream.close(); } catch (IOException e) { e.printStackTrace(); XWPFDocument doc = new XWPFDocument(is); List<XWPFParagraph> paras = doc.getParagraphs(); //This list will hold the paragraphs XWPFWordExtractor ex = new XWPFWordExtractor(doc); //To get the words String words = ""; //This will hold all the text for(XWPFParagraph p : paras){ //For each paragraph we retrieved from the document words += p.getText(); //Add the text we retrieve to the words string XWPFDocument newDoc = new XWPFDocument(); XWPFParagraph para = newDoc.createParagraph(); XWPFRun run = para.createRun(); run.setText(words); newDoc.write(new FileOutputStream(new File("D:\\Users\\user2777005\\Desktop\\mydoc.docx")));} catch (IOException e) {System.out.println(e);}
XWPFDocument doc = new XWPFDocument(new FileInputStream("source.docx")); XWPFDocument destDoc = new XWPFDocument(); OutputStream out = new FileOutputStream("Destination.docx"); XWPFParagraph pr = doc.getParagraphs().get(0); destDoc.createParagraph(); int pos = destDoc.getParagraphs().size() - 1; destDoc.setParagraph(pr, pos); destDoc.write(out);
public static void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputStream(new File("transformed.docx")); XWPFDocument doc = new XWPFDocument(new FileInputStream(new File("original.docx"))); for(XWPFParagraph p:doc.getParagraphs()){ for(XWPFRun r:p.getRuns()){ for(CTText ct:r.getCTR().getTList()){ String str = ct.getStringValue(); if(str.contains("NAME")){ str = str.replace("NAME", "Java Dev"); ct.setStringValue(str); } } } } doc.write(fos); }
FileInputStream fis = new FileInputStream("document.docx"); XWPFDocument doc = new XWPFDocument(fis); List<XWPFTable> tables = doc.getTables(); XWPFTable table = tables.get(0); XWPFTableRow[] rows = table.getRows().toArray(new XWPFTableRow[0]); for (int r = 0; r < rows.length; r++) { if (r > 0) { XWPFTableRow row = rows[r]; table.removeRow(1); //remove second row. others shift upwards table.createRow(); //add new row at the end doc.write(new FileOutputStream("new document.docx"));
XWPFDocument wordDocument = new XWPFDocument(new FileInputStream(docxFile)); ArrayList<PackagePart> packageParts= wordDocument.getPackage().getPartsByRelationshipType ("http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail"); PackagePart packagePart = packageParts.get(0); FileOutputStream fos = new FileOutputStream("c:\\temp\\thumb.emf"); IOUtils.copy(packagePart.getInputStream(), fos);
InputStream inputstream = new FileInputStream(m_filepath); //read the file XWPFDocument adoc= new XWPFDocument(inputstream); //and place it in a xwpf format aString = new XWPFWordExtractor(adoc).getText(); //gets the full text
XWPFDocument doc = new XWPFDocument(new FileInputStream(fileName)); List<XWPFTable> table = doc.getTables(); for (XWPFTable xwpfTable : table) { List<XWPFTableRow> row = xwpfTable.getRows(); for (XWPFTableRow xwpfTableRow : row) { List<XWPFTableCell> cell = xwpfTableRow.getTableCells(); for (XWPFTableCell xwpfTableCell : cell) { if (xwpfTableCell != null) { System.out.println(xwpfTableCell.getText()); String s = xwpfTableCell.getText(); for (XWPFParagraph p : xwpfTableCell.getParagraphs()) { for (XWPFRun run : p.getRuns()) { for (XWPFPicture pic : run.getEmbeddedPictures()) { byte[] pictureData = pic.getPictureData().getData(); System.out.println("picture : " + pictureData); } } } } } } }
import java.io.File; import java.io.FileInputStream; import java.util.List; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; public void readDocxFile() { try { File file = new File("C:/NetBeans Output/documentx.docx"); FileInputStream fis = new FileInputStream(file.getAbsolutePath()); XWPFDocument document = new XWPFDocument(fis); List<XWPFParagraph> paragraphs = document.getParagraphs(); for (XWPFParagraph para : paragraphs) { System.out.println(para.getText()); } fis.close(); } catch (Exception e) { e.printStackTrace(); } }
XWPFDocument document = new XWPFDocument(); FileOutputStream out = new FileOutputStream(new File("create_table.docx")); XWPFParagraph para = document.createParagraph(); XWPFRun run = para.createRun(); run.setText("Hi"); XWPFTable table = document.createTable(); XWPFTableRow tableRowOne = table.getRow(0); tableRowOne.getCell(0).setText("col one, row one"); tableRowOne.addNewTableCell().setText("col two, row one"); tableRowOne.addNewTableCell().setText("col three, row one"); XWPFTableRow tableRowThree = table.createRow(); tableRowThree.getCell(0).setText("col one, row three"); tableRowThree.getCell(1).setText("col two, row three"); XWPFParagraph para2 = document.createParagraph(); XWPFRun run2 = para2.createRun(); run2.setText("Bye"); document.write(out); out.close(); System.out.println("create_table.docx written successully");
public static void main(String[] args) throws IOException { XWPFDocument document = new XWPFDocument(); XWPFTable tableOne = document.createTable(); XWPFTableRow tableOneRowOne = tableOne.getRow(0); tableOneRowOne.getCell(0).setText("Header1"); tableOneRowOne.addNewTableCell().setText("header2"); XWPFTableRow tableOneRowTwo = tableOne.createRow(); tableOneRowTwo.getCell(0).setText("Data1"); tableOneRowTwo.getCell(1).setText("Data2"); FileOutputStream outStream = new FileOutputStream("test.doc"); document.write(outStream); outStream.close(); }
//create word doc XWPFDocument doc = new XWPFDocument(); // create para and run XWPFParagraph para= doc.createParagraph(); XWPFRun run = para.createRun(); para.setAlignment(ParagraphAlignment.CENTER); // convert buffered image to Input Stream ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(screenFullImage, "jpeg", baos); baos.flush(); ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray()); baos.close(); // add image to word doc run.addBreak(); run.addPicture(bis, XWPFDocument.PICTURE_TYPE_JPEG, "image file", Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels bis.close(); // write word doc to file FileOutputStream fos = new FileOutputStream("WordDocWithImage.docx"); doc.write(fos); fos.close();
public class Bill { public static void writeDate(XWPFDocument document) { SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss"); String date = sdf.format(new Date()); XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText(date); } public static void main(String[] args) throws Exception { XWPFDocument document = new XWPFDocument(); writeDate(document); FileOutputStream out = new FileOutputStream("createparagraph.docx"); document.write(out); out.close(); } }