0
私はdocxファイル用の新しいスタイルをApache-poiで作成したいので、このスタイルのfontFamilyとして "IRnazanin"を設定したい(IRnazaninは一種のペルシア語フォントです)。私はこのコードをlinkとthis oneから書いていますが、実行するたびにこのスタイルの段落にArialが設定されています(apache-poiで作成されたdocxファイルを開くと、このスタイルの段落に "Arial ) "フォントはIRNazaninではなくThemeフォントから)。それを修正するために私は何をすべきですか?また、フォントサイズも設定されていません。どのようにいくつかの種類のフォントをfontFamilyとして設定すると、apache poiのスタイルが使用できますか?
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;
}
あなたがコードや全体の回答をコピーする場合(https://stackoverflow.com/a/36649411/461499)少なくともリンクするか、ソースを説明するのがよい方法です。他の人がリンクをたどって、より詳細な対象を閲覧することができます。そして、「これは私のコードです」と主張しないでください。 – RobAu
あなたは正しいです。私はそれを忘れてしまった。ありがとうございました。私の問題について私を助けてくれますか? @RobAu –
[最小限で完全であり、検証可能な例](https://stackoverflow.com/help/mcve)を入力してください。これはおそらく自分自身と他の人がこの質問に注意を向けさせるでしょう。あなたが現在示している乱雑なコードはこれをしません。 –