public XWPFCommentsDecorator(XWPFParagraph paragraph, XWPFParagraphDecorator nextDecorator) { super(paragraph, nextDecorator); XWPFComment comment; commentText = new StringBuilder(64); for (CTMarkupRange anchor : paragraph.getCTP().getCommentRangeStartArray()) { if ((comment = paragraph.getDocument().getCommentByID(anchor.getId().toString())) != null) { commentText.append("\tComment by ") .append(comment.getAuthor()) .append(": ") .append(comment.getText()); } } }
public XWPFComment(CTComment comment, XWPFDocument document) { text = new StringBuilder(64); id = comment.getId().toString(); author = comment.getAuthor(); for (CTP ctp : comment.getPArray()) { XWPFParagraph p = new XWPFParagraph(ctp, document); text.append(p.getText()); } }
public void setText(String text) { CTP ctP = (ctTc.sizeOfPArray() == 0) ? ctTc.addNewP() : ctTc.getPArray(0); XWPFParagraph par = new XWPFParagraph(ctP, this); par.createRun().setText(text); }
XWPFDocument doc= new XWPFDocument(); XWPFParagraph paragraph = doc.createParagraph(); XWPFRun run=paragraph.createRun(); run.setText("The Body:"); paragraph = doc.createParagraph(); run=paragraph.createRun(); run.setText("Lorem ipsum...."); run = paragraph.createRun(); String imgFile="Koala.png"; XWPFPicture picture = run.addPicture(new FileInputStream(imgFile), XWPFDocument.PICTURE_TYPE_PNG, imgFile, Units.toEMU(100), Units.toEMU(100)); run = paragraph.createRun(); run.setText("...Lorem ipsum...."); paragraph = doc.createParagraph(); run=paragraph.createRun(); run.setText("Lorem ipsum...."); doc.write(new FileOutputStream("test.docx"));
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); XWPFRun run = p.createRun(); run.setText(text); p.addRun(run); doc.write(new FileOutputStream(outpath));
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();
public static void main(String[] args){ 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(); FileInputStream is = new FileInputStream(new File("D:\\Users\\user2777005\\Desktop\\MyDov.docx")); XWPFDocument doc = new XWPFDocument(is); 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);}
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); }
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(); XWPFParagraph tmpParagraph = document.createParagraph(); XWPFRun tmpRun = tmpParagraph.createRun(); tmpRun.setText("LALALALAALALAAAA"); tmpRun.setFontSize(18); document.write(new FileOutputStream(new File("yourpathhere"))); document.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(); } }
XWPFDocument template = new XWPFDocument(new FileInputStream(new File("Template.dotx"))); XWPFDocument doc = new XWPFDocument(); // let's copy styles from template to new doc XWPFStyles newStyles = doc.createStyles(); newStyles.setStyles(template.getStyle()); XWPFParagraph para = doc.createParagraph(); para.setStyle("Heading1"); XWPFRun run = para.createRun(); run.setText("Heading 1"); return doc;
XWPFDocument document = new XWPFDocument(PoiTest.class.getResourceAsStream("twocolumn.docx")); XWPFParagraph tmpParagraph = document.getParagraphs().get(0); for (int i = 0; i < 100; i++) { XWPFRun tmpRun = tmpParagraph.createRun(); tmpRun.setText("LALALALAALALAAAA"); tmpRun.setFontSize(18); } document.write(new FileOutputStream(new File("C:\\temp\\poi.docx")));
XWPFDocument document= new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); paragraph.setAlignment(ParagraphAlignment.CENTER); XWPFRun run=paragraph.createRun(); run.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 " + FileOutputStream fileOut = new FileOutputStream("/Users/chenpengye/Documents/excel/test/test.docx"); document.write(fileOut); document.close();
public static void createSimpleTable() throws Exception { try (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(); 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"); try (OutputStream out = new FileOutputStream("simpleTable.docx")) { doc.write(out); } } }
public static void main(String[] args) throws IOException { try (XWPFDocument doc = new XWPFDocument()) { XWPFParagraph p = doc.createParagraph(); XWPFRun r = p.createRun(); r.setText("Some Text"); r.setBold(true); r = p.createRun(); r.setText("Goodbye"); // create header/footer functions insert an empty paragraph XWPFHeader head = doc.createHeader(HeaderFooterType.DEFAULT); head.createParagraph().createRun().setText("header"); XWPFFooter foot = doc.createFooter(HeaderFooterType.DEFAULT); foot.createParagraph().createRun().setText("footer"); try (OutputStream os = new FileOutputStream(new File("header2.docx"))) { doc.write(os); } } } }
@Override public void produce(Report report,OutputStream outputStream) { XWPFDocument document = new XWPFDocument(); try{ CTSectPr sectpr = document.getDocument().getBody().addNewSectPr(); if(!sectpr.isSetPgSz()) { sectpr.addNewPgSz(); if(orientation.equals(Orientation.landscape)){ pageSize.setOrient(STPageOrientation.LANDSCAPE); pageSize.setH(BigInteger.valueOf(DxaUtils.points2dxa(paper.getWidth()))); pageSize.setW(BigInteger.valueOf(DxaUtils.points2dxa(paper.getHeight()))); }else{ pageSize.setOrient(STPageOrientation.PORTRAIT); pageSize.setW(BigInteger.valueOf(DxaUtils.points2dxa(paper.getWidth()))); pageSize.setH(BigInteger.valueOf(DxaUtils.points2dxa(paper.getHeight()))); for(Page page:pages){ List<Row> rows=page.getRows(); XWPFTable table = document.createTable(rows.size(), totalColumn); table.getCTTbl().getTblPr().unsetTblBorders(); table.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(DxaUtils.points2dxa(tableWidth))); XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.setFontSize(0); run.addBreak(BreakType.PAGE);