Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在ApachePOI中将某些字体设置为fontFamily样式?_Java_Apache Poi_Docx - Fatal编程技术网

Java 如何在ApachePOI中将某些字体设置为fontFamily样式?

Java 如何在ApachePOI中将某些字体设置为fontFamily样式?,java,apache-poi,docx,Java,Apache Poi,Docx,我想通过ApachePOI为我的docx文件创建一种新样式,我想将“IRnazanin”设置为这种样式的fontFamily(IRnazanin是一种波斯字体)。我从中编写了这段代码,但每次运行它时,都会为具有这种样式的段落设置Arial(当我打开由ApachePOI创建的docx文件时,具有这种样式的段落具有来自主题字体而不是IRNazanin的“Arial(Body CS)”字体)。我该怎么做才能修好它?并且没有设置字体大小 XWPFDocument docx = new XWPFDocum

我想通过ApachePOI为我的docx文件创建一种新样式,我想将“IRnazanin”设置为这种样式的fontFamily(IRnazanin是一种波斯字体)。我从中编写了这段代码,但每次运行它时,都会为具有这种样式的段落设置Arial(当我打开由ApachePOI创建的docx文件时,具有这种样式的段落具有来自主题字体而不是IRNazanin的“Arial(Body CS)”字体)。我该怎么做才能修好它?并且没有设置字体大小

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("!@#$%^&*()(*&^%$#@!");
            }
            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;
        }
xwpfdocumentdocx=新的XWPFDocument(OPCPackage.open(“8.docx”);
XWPFStyles styles=docx.getStyles();
字符串标题1=“我的标题1”;
字符串标题4=“我的标题4”;
添加自定义标题样式(docx,样式,标题1,1,36,“4288BC”);
添加CustomHeadingStyle(docx,styles,heading4,4,20,“000000”);
XWPFParagraph paragraph paragraph=docx.createParagraph();
第3.3款(标题4);
XWPFRun=段落.createRun();
run.setText(“سااااام!”);
List xwpfparagraphs=docx.getParagraphs();
System.out.println();
对于(int i=0;i样式在格式栏中更为突出
ctStyle.setUiPriority(indentNumber);
CTOnOff onoffnull=CTOnOff.Factory.newInstance();
ctStyle.setUnhidewhen使用(onoffnull);
//样式将显示在“格式”栏中
ctStyle.setQFormat(onoffnull);
//样式定义给定级别的标题
CTPPr-ppr=CTPPr.Factory.newInstance();
ppr.设置大纲视图(缩进编号);
ctStyle.setPPr(ppr);
XWPFStyle=新的XWPFStyle(ctStyle);
CTHpsMeasure size=CTHpsMeasure.Factory.newInstance();
setVal(新的biginger(String.valueOf(pointSize));
CTHpsMeasure size2=CTHpsMeasure.Factory.newInstance();
size2.setVal(新的BigInteger(“24”));
CTFonts=CTFonts.Factory.newInstance();
setAscii(“IRnazanin”);
字体。setHAnsi(“IRnazanin”);
CTRPr rpr=CTRPr.Factory.newInstance();
rpr.setRFonts(字体);
rpr.setSz(尺寸);
rpr.setSzCs(尺寸2);
CTColor=CTColor.Factory.newInstance();
setVal(hexToBytes(hextocolor));
rpr.setColor(颜色);
style.getCTStyle().setRPr(rpr);
//如果已定义,则为空op
style.setType(STStyleType.段落);
样式。添加样式(样式);
}
公共静态字节[]hexToBytes(字符串hextString){
HexBinaryAdapter=新的HexBinaryAdapter();
byte[]bytes=adapter.unmarshal(十六进制字符串);
返回字节;
}
我从这里得到了这个代码,我找到了答案:

我替换此代码,它可以工作:

CTFonts fonts = CTFonts.Factory.newInstance();
fonts.setAscii("IRnazanin");
fonts.setHAnsi("IRnazanin");
fonts.setCs("IRnazanin");
rpr.setRFonts(fonts);
我找到了答案:

我替换此代码,它可以工作:

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

如果复制代码或完整答案()至少链接到它或解释它的来源是一个很好的做法。其他人可以按照链接更深入地浏览主题。不要声称它是我的代码。你是对的。我只是忘记了。谢谢。你能帮我解决我的问题吗?@RobAuPlease提供一个。这可能会促使我和其他人关注这个问题问题。您当前显示的凌乱代码无法执行此操作。我找到了答案。下次我将尝试执行此操作。谢谢。@axelrichter如果您复制了代码或完整答案()至少链接到它或解释它的来源是一个很好的做法。其他人可以按照链接更深入地浏览主题。不要声称它是我的代码。你是对的。我只是忘记了。谢谢。你能帮我解决我的问题吗?@RobAuPlease提供一个。这可能会促使我和其他人关注这个问题问题。您当前显示的混乱代码无法执行此操作。我找到了答案。下次我将尝试执行此操作。谢谢。@AxelRichter