亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過樣式 ID 在 XWPFRun 上設置樣式

通過樣式 ID 在 XWPFRun 上設置樣式

守候你守候我 2021-12-10 14:53:45
我正在嘗試將命名樣式應用于 XWPFDocument 中的單個運行,但我看到了奇怪的結果。XWPFRun的javadoc 描述了 setStyle 方法,但該樣式似乎未應用于最終文檔。我說會出現,因為在 Finder 的 QuickLook 預覽中,樣式確實會按預期出現在運行中。在下面的示例中,我將命名樣式應用于超鏈接,它在右側的預覽中按預期顯示,但在左側的 Word 中沒有顯示。很明顯 POI 實際上是在做一些事情來應用樣式,但 Word 沒有渲染樣式。我嘗試了其他幾個 .docx 閱讀器,所有這些都產生了類似的結果。所以我開始剝離樣式并將屬性單獨應用到運行中,這在 Word中確實有效。這是其中一件似乎我必須錯過的事情。我當然可以編寫一個可以讀取現有樣式的例程并將其應用于這樣的運行,但我寧愿不這樣做。我已經搜索了答案,但這部分 POI 似乎正在進行中。那么我是否只是遺漏了一些明顯的東西,或者我只是不得不接受它并以痛苦的方式做到這一點?//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貢獻1793條經驗 獲得超6個贊

如果XWPfDocument是從模板創建的,則該模板必須已經包含命名樣式“超鏈接”。這意味著,它必須包含/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();


 }

}

請注意,除了使用低級類之外,不可能創建XWPFHyperlinkRunorg.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink。


它產生:

查看完整回答
反對 回復 2021-12-10
  • 1 回答
  • 0 關注
  • 975 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號