2017-09-20 88 views
0

我想通過Apache-poi爲我的docx文件創建一個新樣式,並且我想爲此樣式設置「IRnazanin」作爲fontFamily(IRnazanin是一種波斯語字體)。我從linkthis one編寫了這段代碼,但是每次運行它時,Arial都會爲這個樣式的段落設置(當我打開由apache-poi創建的docx文件時,此樣式的段落具有「Arial(Body CS )「主題字體的字體不是IRNazanin)。我應該怎麼做才能解決它?而且字體大小也沒有設置。如何爲apache poi中的樣式設置某種字體作爲fontFamily?

XWPFDocument docx = new XWPFDocument(OPCPackage.open("8.docx")); 
XWPFStyles styles = docx.getStyles(); 
     String heading1 = "My Heading 1"; 
     String heading4 = "My Heading 4"; 
     addCustomHeadingStyle(docx, styles, heading1, 1, 36, "4288BC"); 
     addCustomHeadingStyle(docx, styles, heading4, 4, 20, "000000"); 
     XWPFParagraph paragraph = docx.createParagraph(); 
     paragraph.setStyle(heading4); 
     XWPFRun run = paragraph.createRun(); 
     run.setText("سلااااام!"); 

     List<XWPFParagraph> xwpfparagraphs = docx.getParagraphs(); 
     System.out.println(); 
     for (int i = 0; i < xwpfparagraphs.size(); i++) { 
      if (xwpfparagraphs.get(i).getText().equals("اول")) { 
       xwpfparagraphs.get(i).setStyle(heading1); 
       System.out.println("[email protected]#$%^&*()(*&^%$#@!"); 
      } 
      System.out.println("paragraph style id " + (i + 1) + ":" + xwpfparagraphs.get(i).getStyleID()); 
      if (xwpfparagraphs.get(i).getStyleID() != null) { 
       String styleid = xwpfparagraphs.get(i).getStyleID(); 
       XWPFStyle style = styles.getStyle(styleid); 
       if (style != null) { 
        System.out.println(xwpfparagraphs.get(i).getText()); 
        System.out.println("Style name:" + style.getName()); 
        if (style.getName().startsWith("heading")) { 
         //this is a heading 
         System.out.println("@@@@@@@@@@@@@@@"); 
        } 
       } 

      } 

     } 

     docx.write(docxOut); 

     private static void addCustomHeadingStyle(XWPFDocument docxDocument, XWPFStyles styles, String strStyleId, int headingLevel, int pointSize, String hexColor) { 

      CTStyle ctStyle = CTStyle.Factory.newInstance(); 
      ctStyle.setStyleId(strStyleId); 

      CTString styleName = CTString.Factory.newInstance(); 
      styleName.setVal(strStyleId); 
      ctStyle.setName(styleName); 

      CTDecimalNumber indentNumber = CTDecimalNumber.Factory.newInstance(); 
      indentNumber.setVal(BigInteger.valueOf(headingLevel)); 

      // lower number > style is more prominent in the formats bar 
      ctStyle.setUiPriority(indentNumber); 

      CTOnOff onoffnull = CTOnOff.Factory.newInstance(); 
      ctStyle.setUnhideWhenUsed(onoffnull); 

      // style shows up in the formats bar 
      ctStyle.setQFormat(onoffnull); 

      // style defines a heading of the given level 
      CTPPr ppr = CTPPr.Factory.newInstance(); 
      ppr.setOutlineLvl(indentNumber); 
      ctStyle.setPPr(ppr); 

      XWPFStyle style = new XWPFStyle(ctStyle); 

      CTHpsMeasure size = CTHpsMeasure.Factory.newInstance(); 
      size.setVal(new BigInteger(String.valueOf(pointSize))); 
      CTHpsMeasure size2 = CTHpsMeasure.Factory.newInstance(); 
      size2.setVal(new BigInteger("24")); 


      CTFonts fonts = CTFonts.Factory.newInstance(); 

      fonts.setAscii("IRnazanin"); 
      fonts.setHAnsi("IRnazanin"); 

      CTRPr rpr = CTRPr.Factory.newInstance(); 
      rpr.setRFonts(fonts); 
      rpr.setSz(size); 
      rpr.setSzCs(size2); 

      CTColor color = CTColor.Factory.newInstance(); 
      color.setVal(hexToBytes(hexColor)); 
      rpr.setColor(color); 
      style.getCTStyle().setRPr(rpr); 
      // is a null op if already defined 

      style.setType(STStyleType.PARAGRAPH); 
      styles.addStyle(style); 

     } 

     public static byte[] hexToBytes(String hexString) { 
      HexBinaryAdapter adapter = new HexBinaryAdapter(); 
      byte[] bytes = adapter.unmarshal(hexString); 
      return bytes; 
     } 

我從這個linkthis one.

+0

如果複製代碼或整個答案(https://stackoverflow.com/a/36649411/461499)至少鏈接到它或解釋它的來源是一個好習慣。其他人可以按照鏈接進行更深入的瀏覽。不要聲稱它是'這是我的代碼' – RobAu

+0

你說得對。我只是忘了它。謝謝。你能幫我解決我的問題嗎? @RobAu –

+0

請提供[最小,完整和可驗證示例](https://stackoverflow.com/help/mcve)。這可能會促使我和其他人在這個問題上得到關注。你目前顯示的混亂的代碼不會這樣做。 –

回答

1

我找到答案得到這個代碼:

我將這段代碼和它的工作原理:

CTFonts fonts = CTFonts.Factory.newInstance(); 
fonts.setAscii("IRnazanin"); 
fonts.setHAnsi("IRnazanin"); 
fonts.setCs("IRnazanin"); 
rpr.setRFonts(fonts); 
相關問題