if (p.getRuns().size() > 0) { r = p.getRuns().get(0); r = p.createRun(); CTR ctr = r.getCTR(); boolean foundRef = false; for (CTFtnEdnRef ref : ctr.getFootnoteReferenceList()) { if (getId().equals(ref.getId())) { foundRef = true; ctr.addNewRPr().addNewRStyle().setVal("FootnoteReference"); ctr.addNewFootnoteRef();
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();
/** * 增加一个段落 * * @param align 段落对齐方式{@link ParagraphAlignment} * @param font 字体信息{@link Font} * @param texts 段落中的文本,支持多个文本作为一个段落 * @return this */ public Word07Writer addText(ParagraphAlignment align, Font font, String... texts) { final XWPFParagraph p = this.doc.createParagraph(); if (null != align) { p.setAlignment(align); } if (ArrayUtil.isNotEmpty(texts)) { XWPFRun run; for (String text : texts) { run = p.createRun(); run.setText(text); if (null != font) { run.setFontFamily(font.getFamily()); run.setFontSize(font.getSize()); run.setBold(font.isBold()); run.setItalic(font.isItalic()); } } } return this; }
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();
InputStream is = new FileInputStream("Japan.docx"); XWPFDocument doc = new XWPFDocument(is); List<XWPFParagraph> paras = doc.getParagraphs(); XWPFDocument newdoc = new XWPFDocument(); for (XWPFParagraph para : paras) { final int DEFAULT_FONT_SIZE = 10; for (XWPFRun run : oldPar.getRuns()) { String textInRun = run.getText(0); if (textInRun == null || textInRun.isEmpty()) { continue; int fontSize = run.getFontSize(); System.out.println("run text = '" + textInRun + "' , fontSize = " + fontSize); XWPFRun newRun = newPar.createRun(); newRun.setText(textInRun); newRun.setFontSize( ( fontSize == -1) ? DEFAULT_FONT_SIZE : run.getFontSize() ); newRun.setFontFamily( run.getFontFamily() ); newRun.setBold( run.isBold() ); newRun.setItalic( run.isItalic() ); newRun.setStrike( run.isStrike() ); newRun.setColor( run.getColor() );
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);}
try { File file = new File(PATH_TO_FILE); //"C:\\Reports\\Template.docx"; FileInputStream fis = new FileInputStream(file); docTemplate = new XWPFDocument(fis); generateTOC(docTemplate); saveDocument(docTemplate); FileOutputStream outputFile = null; try { outputFile = new FileOutputStream(OUTFILENAME); docTemplate.write(outputFile); } finally { if (outputFile != null) { outputFile.close(); String findText = "${TOC}"; String replaceText = ""; for (XWPFParagraph p : document.getParagraphs()) { for (XWPFRun r : p.getRuns()) { int pos = r.getTextPosition(); String text = r.getText(pos); if (text != null && text.contains(findText)) { text = text.replace(findText, replaceText); r.setText(text, 0); addField(p, "TOC \\o \"1-3\" \\h \\z \\u"); break; CTSimpleField ctSimpleField = paragraph.getCTP().addNewFldSimple();
XWPFDocument doc = new XWPFDocument(new FileInputStream("source.docx")); for (XWPFParagraph p : doc.getParagraphs()) { //go through all paragraphs int runNumber = 0; while (runNumber < p.getRuns().size()) { //go through all runs, we cannot use for each since we will possibly insert new runs XWPFRun r = p.getRuns().get(runNumber); String runText = r.getText(0); if (runText != null && runText.contains("a")) { //if we have a run with an "a" in it, then char[] runChars = runText.toCharArray(); for (int charNumber = 0; charNumber < runChars.length; charNumber++) { //go through all characters in that run if (runChars[charNumber] == 'a') { //if the charcter is an 'a' then r.setText(sb.toString(), 0); //set all characters, which are current buffered, as the text of the actual run r = p.insertNewRun(++runNumber); //insert new run for the '@' as the replacement for the 'a' r.setText("@", 0); r.setColor("DC143C"); r = p.insertNewRun(++runNumber); //insert new run for the next characters sb = new StringBuffer(); //empty buffer r.setText(sb.toString(), 0); //set all characters, which are current buffered, as the text of the actual run doc.write(new FileOutputStream("result.docx")); doc.close();
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); }
public static void main(String[] args)throws Exception { XWPFDocument docx1 = new XWPFDocument(new FileInputStream("Test1.docx")); XWPFNumbering numberingDocx1 = docx1.getNumbering(); List<XWPFParagraph> paragraphListDocx1 = docx1.getParagraphs(); XWPFParagraph paragraphDocx2 = docx2.createParagraph(); XWPFRun run = paragraphDocx2.createRun(); run.setText("This is from Test1.docx:"); paragraphDocx2 = docx2.createParagraph(); run = paragraphDocx2.createRun(); run.setText("^-- this was from Test1.docx."); FileOutputStream out = new FileOutputStream(new File("Test2.docx")); docx2.write(out); docx2.close();
try { File file = new File(filename); XWPFDocument template = new XWPFDocument(new FileInputStream(file)); List<XWPFParagraph> xwpfParagraphs = template.getParagraphs(); replaceInParagraphs(xwpfParagraphs); List<XWPFTable> tables = template.getTables(); for (XWPFTable xwpfTable : tables) { List<XWPFTableRow> tableRows = xwpfTable.getRows(); out = new FileOutputStream(new File(outputPath)); template.write(out); out.flush(); List<XWPFRun> xwpfRuns = xwpfParagraph.getRuns(); for (XWPFRun xwpfRun : xwpfRuns) { String xwpfRunText = xwpfRun.getText(xwpfRun .getTextPosition()); for (Map.Entry<String, String> entry : replacements .entrySet()) { xwpfRun.setText(xwpfRunText, 0);
XWPFDocument document= new XWPFDocument(); FileOutputStream out = new FileOutputStream( new File("fontstyle.docx")); XWPFParagraph paragraph = document.createParagraph(); XWPFRun paragraphOneRunOne = paragraph.createRun(); paragraphOneRunOne.setBold(true); paragraphOneRunOne.setItalic(true); paragraphOneRunOne.setText("Font Style"); paragraphOneRunOne.addBreak(); XWPFRun paragraphOneRunTwo = paragraph.createRun(); paragraphOneRunTwo.setText("Font Style two"); paragraphOneRunTwo.setTextPosition(100); XWPFRun paragraphOneRunThree = paragraph.createRun(); paragraphOneRunThree.setStrike(true); paragraphOneRunThree.setFontSize(20); paragraphOneRunThree.setSubscript( VerticalAlign.SUBSCRIPT); paragraphOneRunThree.setText(" Different Font Styles"); document.write(out); out.close(); System.out.println("fontstyle.docx written successully");
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;
FileInputStream fis = new FileInputStream("This is a Test.docx"); XWPFDocument xdoc = new XWPFDocument(fis); List<XWPFParagraph> paragraphs = xdoc.getParagraphs(); for (XWPFParagraph paragraph : paragraphs) { List<XWPFRun> runs = paragraph.getRuns(); for (XWPFRun run : runs) { CTRPr cTRPr = run.getCTR().getRPr(); if (cTRPr != null) { if (cTRPr.getRStyle() != null) { System.out.println(styleID); System.out.println("============================================================="); XWPFStyle xStyle = xdoc.getStyles().getStyle(styleID); if (xStyle.getType() == STStyleType.CHARACTER) { System.out.println(xStyle.getCTStyle());
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")));
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); } } } }
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"); CTText t = ctP.addNewR().addNewT(); t.setStringValue("header"); XWPFParagraph[] pars = new XWPFParagraph[1]; p = new XWPFParagraph(ctP, doc); pars[0] = p; XWPFHeaderFooterPolicy hfPolicy = doc.createHeaderFooterPolicy(); hfPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, pars); t = ctP.addNewR().addNewT(); t.setStringValue("My Footer"); pars[0] = new XWPFParagraph(ctP, doc); hfPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, pars); try (OutputStream os = new FileOutputStream(new File("header.docx"))) { doc.write(os);