第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

通過樣式 ID 在 XWPFRun 上設(shè)置樣式

通過樣式 ID 在 XWPFRun 上設(shè)置樣式

守候你守候我 2021-12-10 14:53:45
我正在嘗試將命名樣式應(yīng)用于 XWPFDocument 中的單個(gè)運(yùn)行,但我看到了奇怪的結(jié)果。XWPFRun的javadoc 描述了 setStyle 方法,但該樣式似乎未應(yīng)用于最終文檔。我說會(huì)出現(xiàn),因?yàn)樵?Finder 的 QuickLook 預(yù)覽中,樣式確實(shí)會(huì)按預(yù)期出現(xiàn)在運(yùn)行中。在下面的示例中,我將命名樣式應(yīng)用于超鏈接,它在右側(cè)的預(yù)覽中按預(yù)期顯示,但在左側(cè)的 Word 中沒有顯示。很明顯 POI 實(shí)際上是在做一些事情來應(yīng)用樣式,但 Word 沒有渲染樣式。我嘗試了其他幾個(gè) .docx 閱讀器,所有這些都產(chǎn)生了類似的結(jié)果。所以我開始剝離樣式并將屬性單獨(dú)應(yīng)用到運(yùn)行中,這在 Word中確實(shí)有效。這是其中一件似乎我必須錯(cuò)過的事情。我當(dāng)然可以編寫一個(gè)可以讀取現(xiàn)有樣式的例程并將其應(yīng)用于這樣的運(yùn)行,但我寧愿不這樣做。我已經(jīng)搜索了答案,但這部分 POI 似乎正在進(jìn)行中。那么我是否只是遺漏了一些明顯的東西,或者我只是不得不接受它并以痛苦的方式做到這一點(diǎn)?//This does not work.run.setStyle(styleId);if(docStyles.styleExist(styleId)){    /*        In order to set the style on the run, we need to manually        determine the properties of the style, and set them on the        run individually.        This makes no sense.     */    XWPFStyle style = docStyles.getStyle(styleId);    CTStyle ctStyle = style.getCTStyle();    CTRPr ctRpr = ctStyle.getRPr();    if (ctRpr.isSetB())    {        CTOnOff onOff = ctRpr.getB();        STOnOff.Enum stOnOff = onOff.getVal();        boolean bold = (stOnOff == STOnOff.TRUE);        run.setBold(bold);    }    if(ctRpr.isSetU())    {        CTUnderline underline = ctRpr.getU();        STUnderline.Enum val = underline.getVal();        UnderlinePatterns underlinePattern = UnderlinePatterns.valueOf(val.intValue());        run.setUnderline(underlinePattern);    }    // ... //}else{    System.out.println("404: Style not found");}
查看完整描述

1 回答

?
搖曳的薔薇

TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊

如果XWPfDocument是從模板創(chuàng)建的,則該模板必須已經(jīng)包含命名樣式“超鏈接”。這意味著,它必須包含/word/styles.xml在潛在樣式的條目中


...

<w:latentStyles...

...

 <w:lsdException w:name="Hyperlink" w:qFormat="1"/>

...

以及樣式定義


...

<w:style w:type="character" w:styleId="Hyperlink">

 <w:name w:val="Hyperlink"/>

 <w:basedOn w:val="..."/>

 <w:uiPriority w:val="99"/>

 <w:unhideWhenUsed/>

 <w:qFormat/>

 <w:rsid w:val="00072FE4"/>

 <w:rPr>

  <w:color w:val="0000FF" w:themeColor="hyperlink"/>

  <w:u w:val="single"/>

 </w:rPr>

</w:style>

...

如果這是真的,那么以下代碼適用于我使用apache poi 4.0.0:


import java.io.FileInputStream;

import java.io.FileOutputStream;


import org.apache.poi.xwpf.usermodel.*;


import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink;


public class CreateWordStyledHyperlinkRunFromTemplate {


 static XWPFHyperlinkRun createHyperlinkRun(XWPFParagraph paragraph, String uri) throws Exception {

  String rId = paragraph.getPart().getPackagePart().addExternalRelationship(

    uri, 

    XWPFRelation.HYPERLINK.getRelation()

   ).getId();


  CTHyperlink cthyperLink=paragraph.getCTP().addNewHyperlink();

  cthyperLink.setId(rId);

  cthyperLink.addNewR();


  return new XWPFHyperlinkRun(

    cthyperLink,

    cthyperLink.getRArray(0),

    paragraph

   );

 }


 public static void main(String[] args) throws Exception {


  XWPFDocument document = new XWPFDocument(new FileInputStream("Template.docx"));


  XWPFParagraph paragraph = document.createParagraph();

  XWPFRun run = paragraph.createRun();

  run.setText("This is a text paragraph having a link to Google ");


  XWPFHyperlinkRun hyperlinkrun = createHyperlinkRun(paragraph, "https://www.google.de");

  hyperlinkrun.setText("https://www.google.de");

  XWPFStyles styles = document.getStyles();

  if (styles.styleExist("Hyperlink")) {

   System.out.println("Style Hyperlink exists."); //Template must contain named style "Hyperlink" already

   hyperlinkrun.setStyle("Hyperlink");

  } else {

   hyperlinkrun.setColor("0000FF");

   hyperlinkrun.setUnderline(UnderlinePatterns.SINGLE);

  }


  run = paragraph.createRun();

  run.setText(" in it.");


  FileOutputStream out = new FileOutputStream("CreateWordStyledHyperlinkRunFromTemplate.docx");

  document.write(out);

  out.close();

  document.close();


 }

}

請(qǐng)注意,除了使用低級(jí)類之外,不可能創(chuàng)建XWPFHyperlinkRunorg.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink。


它產(chǎn)生:

查看完整回答
反對(duì) 回復(fù) 2021-12-10
  • 1 回答
  • 0 關(guān)注
  • 885 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)