/** * Sets the text of this text run in the * * @param value the literal text which shall be displayed in the document * @param pos - position in the text array (NB: 0 based) */ public void setText(String value, int pos) { if (pos > run.sizeOfTArray()) { throw new ArrayIndexOutOfBoundsException("Value too large for the parameter position in XWPFRun.setText(String value,int pos)"); } CTText t = (pos < run.sizeOfTArray() && pos >= 0) ? run.getTArray(pos) : run.addNewT(); t.setStringValue(value); preserveSpaces(t); }
public TOC(CTSdtBlock block) { this.block = block; CTSdtPr sdtPr = block.addNewSdtPr(); CTDecimalNumber id = sdtPr.addNewId(); id.setVal(new BigInteger("4844945")); sdtPr.addNewDocPartObj().addNewDocPartGallery().setVal("Table of contents"); CTSdtEndPr sdtEndPr = block.addNewSdtEndPr(); CTRPr rPr = sdtEndPr.addNewRPr(); CTFonts fonts = rPr.addNewRFonts(); fonts.setAsciiTheme(STTheme.MINOR_H_ANSI); fonts.setEastAsiaTheme(STTheme.MINOR_H_ANSI); fonts.setHAnsiTheme(STTheme.MINOR_H_ANSI); fonts.setCstheme(STTheme.MINOR_BIDI); rPr.addNewB().setVal(STOnOff.OFF); rPr.addNewBCs().setVal(STOnOff.OFF); rPr.addNewColor().setVal("auto"); rPr.addNewSz().setVal(new BigInteger("24")); rPr.addNewSzCs().setVal(new BigInteger("24")); CTSdtContentBlock content = block.addNewSdtContent(); CTP p = content.addNewP(); p.setRsidR("00EF7E24".getBytes(LocaleUtil.CHARSET_1252)); p.setRsidRDefault("00EF7E24".getBytes(LocaleUtil.CHARSET_1252)); p.addNewPPr().addNewPStyle().setVal("TOCHeading"); p.addNewR().addNewT().setStringValue("Table of Contents"); }
CTR run = p.addNewR(); run.addNewRPr().addNewNoProof(); run.addNewT().setStringValue(title); run = p.addNewR(); run.addNewRPr().addNewNoProof(); text.setSpace(Space.PRESERVE); text.setStringValue(" PAGEREF _Toc" + bookmarkRef + " \\h "); p.addNewR().addNewRPr().addNewNoProof(); run = p.addNewR(); run.addNewT().setStringValue(Integer.toString(page)); run = p.addNewR(); run.addNewRPr().addNewNoProof();
CTP ctp = CTP.Factory.newInstance(); CTR ctr = ctp.addNewR(); CTRPr rpr = ctr.addNewRPr(); CTText textt = ctr.addNewT(); textt.setStringValue( " Page 1" ); XWPFParagraph codePara = new XWPFParagraph( ctp, document ); XWPFParagraph[] newparagraphs = new XWPFParagraph[1]; newparagraphs[0] = codePara; CTSectPr sectPr = document.getDocument().getBody().addNewSectPr(); XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy( document, sectPr ); headerFooterPolicy.createFooter( STHdrFtr.DEFAULT, newparagraphs );
/** * 直接填充内容到段落,将忽略从1-end的所有文字样式 * * @param paragraph 段落实例 * @param params 填充内容 */ public void initParaGraph(XWPFParagraph paragraph, Object... params) { if (params == null) return; int index = 0; for (XWPFRun run : paragraph.getRuns()) { for (CTText ctText : run.getCTR().getTList()) { if (index++ == 0) { ctText.setStringValue(StringUtils.concat(params)); } else { ctText.setStringValue(""); } } } }
/** * 直接填充内容到段落,将忽略从1-end的所有文字样式 * * @param paragraph 段落实例 * @param params 填充内容 */ public void initParaGraph(XWPFParagraph paragraph, Object... params) { if (params == null) return; int index = 0; for (XWPFRun run : paragraph.getRuns()) { for (CTText ctText : run.getCTR().getTList()) { if (index++ == 0) { ctText.setStringValue(StringUtils.concat(params)); } else { ctText.setStringValue(""); } } } }
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); }
String text = "Test"; File docxFile = new File("C:/testeXWPF.docx"); FileInputStream finStream = new FileInputStream(docxFile.getAbsolutePath()); XWPFDocument doc = new XWPFDocument(finStream); XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy(); if (policy == null) { policy = new XWPFHeaderFooterPolicy(doc); } CTP ctP1 = CTP.Factory.newInstance(); CTR ctR1 = ctP1.addNewR(); CTText t = ctR1.addNewT(); t.setStringValue(text); XWPFParagraph codePara = new XWPFParagraph(ctP1); XWPFParagraph[] newparagraphs = new XWPFParagraph[1]; newparagraphs[0] = codePara; policy.createFooter(policy.DEFAULT, newparagraphs); FileOutputStream fileOut = new FileOutputStream(docxFile); doc.write(fileOut); fileOut.close();
XWPFDocument document = new XWPFDocument(); CTP ctp = CTP.Factory.newInstance(); CTR ctr = ctp.addNewR(); CTRPr rpr = ctr.addNewRPr(); CTText textt = ctr.addNewT(); textt.setStringValue( " Page 1" ); XWPFParagraph codePara = new XWPFParagraph( ctp, document ); XWPFParagraph[] newparagraphs = new XWPFParagraph[1]; newparagraphs[0] = codePara; CTSectPr sectPr = document.getDocument().getBody().addNewSectPr(); XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy( document, sectPr ); headerFooterPolicy.createFooter( STHdrFtr.DEFAULT, newparagraphs );
/** * Sets the text of this text run in the * * @param value the literal text which shall be displayed in the document * @param pos - position in the text array (NB: 0 based) */ public void setText(String value, int pos) { if(pos > run.sizeOfTArray()) throw new ArrayIndexOutOfBoundsException("Value too large for the parameter position in XWPFRun.setText(String value,int pos)"); CTText t = (pos < run.sizeOfTArray() && pos >= 0) ? run.getTArray(pos) : run.addNewT(); t.setStringValue(value); preserveSpaces(t); }
/** * Sets the text of this text run in the * * @param value the literal text which shall be displayed in the document * @param pos - position in the text array (NB: 0 based) */ public void setText(String value, int pos) { if (pos > run.sizeOfTArray()) { throw new ArrayIndexOutOfBoundsException("Value too large for the parameter position in XWPFRun.setText(String value,int pos)"); } CTText t = (pos < run.sizeOfTArray() && pos >= 0) ? run.getTArray(pos) : run.addNewT(); t.setStringValue(value); preserveSpaces(t); }
fldChar.setFldCharType(STFldCharType.Enum.forString("begin")); CTText ctText = r1.getCTR().addNewInstrText(); ctText.setStringValue("PAGE \\* MERGEFORMAT"); ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve")); if(hf.getFontSize()>1){ fldChar.setFldCharType(STFldCharType.Enum.forString("begin")); CTText ctText = r1.getCTR().addNewInstrText(); ctText.setStringValue("NUMPAGES \\* MERGEFORMAT "); ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve")); if(hf.getFontSize()>1){ fldChar.setFldCharType(STFldCharType.Enum.forString("begin")); CTText ctText = r1.getCTR().addNewInstrText(); ctText.setStringValue("PAGE \\* MERGEFORMAT"); ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve")); if(hf.getFontSize()>1){ fldChar.setFldCharType(STFldCharType.Enum.forString("begin")); CTText ctText = r1.getCTR().addNewInstrText(); ctText.setStringValue("NUMPAGES \\* MERGEFORMAT "); ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve")); if(hf.getFontSize()>1){ fldChar.setFldCharType(STFldCharType.Enum.forString("begin")); CTText ctText = r1.getCTR().addNewInstrText(); ctText.setStringValue("PAGE \\* MERGEFORMAT"); ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve")); if(hf.getFontSize()>1){
for(XWPFParagraph paragraph : paragraphs) { CTP ctp = paragraph.getCTP(); // Get all bookmarks and loop through them List<CTBookmark> bookmarks = ctp.getBookmarkStartList(); for(CTBookmark bookmark : bookmarks) { // Extract the name of the bookmark String bookmarkName = bookmark.getName(); // Get matching bookmark value String bookmarkValue = bookmarkValues.get(bookmarkName); if(bookmarkValue!=null && !bookmarkValue.equals("")) { // Create a new RSID (revision identifier) and add text CTR ctr = ctp.addNewR(); CTText text = ctr.addNewT(); text.setStringValue(bookmarkValue); } } }
/** * 初始化一个段落 * * @param infoList 表达式信息 * @param paragraph 段落实例 */ public void init(List<GroovyExpressionRunner.ExpressInfo> infoList, XWPFParagraph paragraph) { for (GroovyExpressionRunner.ExpressInfo expressInfo : infoList) { int cindex = 0, rindex = 0; for (XWPFRun run : paragraph.getRuns()) { for (CTText ctText : run.getCTR().getTList()) { //如果当前位置是一个表达式,则将值替换为空 if (cindex > expressInfo.getStartWith() && cindex < expressInfo.getEndWith() + 1) ctText.setStringValue(""); cindex++; } rindex++; } XWPFRun run = expressInfo.getRun(); int index = 0; for (CTText ctText : run.getCTR().getTList()) { //将表达式的第一个文本设置为真实的值,其他设置为空,因此,此段落的样式为表达式第一个文字的样式 if (index == 0&&!expressInfo.isKeyWord()) { ctText.setStringValue(String.valueOf(expressInfo.getValue())); } else { ctText.setStringValue(""); } } } }
CTFtnEdn ctfInstance = CTFtnEdn.Factory.newInstance(); BigInteger id = new BigInteger("1"); ctfInstance.setId(id); CTP ctp = ctfInstance.addNewP(); ctp.addNewPPr().addNewPStyle().setVal("FootnoteText"); CTR ctr = ctp.addNewR(); ctr.addNewRPr().addNewRStyle().setVal("FootnoteReference"); ctr.addNewFootnoteRef(); CTText cttext = ctp.addNewR().addNewT(); cttext.setStringValue("This is the text in the footnote"); cttext.setSpace(SpaceAttribute.Space.PRESERVE);
XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy(); if (policy.getDefaultHeader() == null && policy.getFirstPageHeader() == null && policy.getDefaultFooter() == null) { // Need to create some new headers // The easy way, gives a single empty paragraph XWPFHeader headerD = policy.createHeader(policy.DEFAULT); headerD.getParagraphs(0).createRun().setText("Hello Header World!"); // Or the full control way CTP ctP1 = CTP.Factory.newInstance(); CTR ctR1 = ctP1.addNewR(); CTText t = ctR1.addNewT(); t.setStringValue("Paragraph in header"); XWPFParagraph p1 = new XWPFParagraph(ctP1, sampleDoc); XWPFParagraph[] pars = new XWPFParagraph[1]; pars[0] = p1; policy.createHeader(policy.FIRST, pars); } else { // Already has a header, change it }
public void example() throws Exception{ XWPFDocument document = new XWPFDocument(); //Append a link to appendExternalHyperlink("https://poi.apache.org", " Link to POI", document.createParagraph()); document.write(new FileOutputStream("resultat.docx")); } /** * Appends an external hyperlink to the paragraph. * * @param url The URL to the external target * @param text The linked text * @param paragraph the paragraph the link will be appended to. */ public static void appendExternalHyperlink(String url, String text, XWPFParagraph paragraph){ //Add the link as External relationship String id=paragraph.getDocument().getPackagePart().addExternalRelationship(url, XWPFRelation.HYPERLINK.getRelation()).getId(); //Append the link and bind it to the relationship CTHyperlink cLink=paragraph.getCTP().addNewHyperlink(); cLink.setId(id); //Create the linked text CTText ctText=CTText.Factory.newInstance(); ctText.setStringValue(text); CTR ctr=CTR.Factory.newInstance(); ctr.setTArray(new CTText[]{ctText}); //Insert the linked text into the link cLink.setRArray(new CTR[]{ctr}); }
t.setStringValue("header"); XWPFParagraph[] pars = new XWPFParagraph[1]; p = new XWPFParagraph(ctP, doc); t.setStringValue("My Footer"); pars[0] = new XWPFParagraph(ctP, doc); hfPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, pars);
public TOC(CTSdtBlock block) { this.block = block; CTSdtPr sdtPr = block.addNewSdtPr(); CTDecimalNumber id = sdtPr.addNewId(); id.setVal(new BigInteger("4844945")); sdtPr.addNewDocPartObj().addNewDocPartGallery().setVal("Table of contents"); CTSdtEndPr sdtEndPr = block.addNewSdtEndPr(); CTRPr rPr = sdtEndPr.addNewRPr(); CTFonts fonts = rPr.addNewRFonts(); fonts.setAsciiTheme(STTheme.MINOR_H_ANSI); fonts.setEastAsiaTheme(STTheme.MINOR_H_ANSI); fonts.setHAnsiTheme(STTheme.MINOR_H_ANSI); fonts.setCstheme(STTheme.MINOR_BIDI); rPr.addNewB().setVal(STOnOff.OFF); rPr.addNewBCs().setVal(STOnOff.OFF); rPr.addNewColor().setVal("auto"); rPr.addNewSz().setVal(new BigInteger("24")); rPr.addNewSzCs().setVal(new BigInteger("24")); CTSdtContentBlock content = block.addNewSdtContent(); CTP p = content.addNewP(); p.setRsidR("00EF7E24".getBytes(LocaleUtil.CHARSET_1252)); p.setRsidRDefault("00EF7E24".getBytes(LocaleUtil.CHARSET_1252)); p.addNewPPr().addNewPStyle().setVal("TOCHeading"); p.addNewR().addNewT().setStringValue("Table of Contents"); }
public TOC(CTSdtBlock block) { this.block = block; CTSdtPr sdtPr = block.addNewSdtPr(); CTDecimalNumber id = sdtPr.addNewId(); id.setVal(new BigInteger("4844945")); sdtPr.addNewDocPartObj().addNewDocPartGallery().setVal("Table of contents"); CTSdtEndPr sdtEndPr = block.addNewSdtEndPr(); CTRPr rPr = sdtEndPr.addNewRPr(); CTFonts fonts = rPr.addNewRFonts(); fonts.setAsciiTheme(STTheme.MINOR_H_ANSI); fonts.setEastAsiaTheme(STTheme.MINOR_H_ANSI); fonts.setHAnsiTheme(STTheme.MINOR_H_ANSI); fonts.setCstheme(STTheme.MINOR_BIDI); rPr.addNewB().setVal(STOnOff.OFF); rPr.addNewBCs().setVal(STOnOff.OFF); rPr.addNewColor().setVal("auto"); rPr.addNewSz().setVal(new BigInteger("24")); rPr.addNewSzCs().setVal(new BigInteger("24")); CTSdtContentBlock content = block.addNewSdtContent(); CTP p = content.addNewP(); p.setRsidR("00EF7E24".getBytes()); p.setRsidRDefault("00EF7E24".getBytes()); p.addNewPPr().addNewPStyle().setVal("TOCHeading"); p.addNewR().addNewT().setStringValue("Table of Contents"); }