java - how can I get more specific font information use poi XWPF API? when I use one Calibri font file to test, the fontName I get is null and fontsize is -1 -
i use code test 1 times new roman font file , 1 calibri font file. times new roman, code can return font name not font size. calibri, can't either font name or font size. don't know what's wrong happened...
import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import java.util.list; import org.apache.poi.xwpf.usermodel.xwpfdocument; import org.apache.poi.xwpf.usermodel.xwpfparagraph; import org.apache.poi.xwpf.usermodel.xwpfrun; public class test1 { public static void main(string[] args) { file file = new file("f:\\workspace\\zextractinformationtest2\\bold.docx"); xwpfdocument document; try { document = new xwpfdocument(new fileinputstream(file)); list<xwpfparagraph> paralist = document.getparagraphs(); (int index = 0; index < paralist.size(); index++) { xwpfparagraph para = paralist.get(index); (xwpfrun run : para.getruns()) { string font = run.getfontname(); int fontsize = run.getfontsize(); string text = run.text(); boolean isbold = run.isbold(); system.out.println("run: "+font+"\t"+fontsize+"\t"+text+"\t"+isbold); } } } catch (ioexception e) { e.printstacktrace(); } } }
this default font size & default font problem. use method follow can default layout xml text directly, , can know default font size, default font, , default alignment.
private string[] getdefault() { string[] defaultattibts = new string[] { "", "", "" }; try { ctstyles styles = document.getstyle(); if (styles.issetdocdefaults()) { string docdefaults = styles.getdocdefaults().xmltext(); if (docdefaults.contains("jc")) { docdefaults = docdefaults.substring(docdefaults.indexof("jc")); docdefaults = docdefaults.substring(docdefaults.indexof("=\"") + 2); defaultattibts[0] = docdefaults.substring(0, docdefaults.indexof("\"")).touppercase(); } if (docdefaults.contains("sz")) { docdefaults = docdefaults.substring(docdefaults.indexof("sz")); docdefaults = docdefaults.substring(docdefaults.indexof("=\"") + 2); defaultattibts[1] = docdefaults.substring(0, docdefaults.indexof("\"")); } if (docdefaults.contains("rfonts")) { docdefaults = docdefaults.substring(docdefaults.indexof("rfonts")); docdefaults = docdefaults.substring(docdefaults.indexof("=\"") + 2); defaultattibts[2] = docdefaults.substring(0, docdefaults.indexof("\"")); } } ctstyle[] style = styles.getstylearray(); (ctstyle s : style) { if (s.issetdefault() && s.issettype()) { if (s.gettype().tostring().equals("paragraph") && s.getdefault().tostring().equals("1")) { string text = s.xmltext(); if (defaultattibts[1] == "") { text = text.substring(text.indexof("sz")); text = text.substring(text.indexof("=\"") + 2); defaultattibts[1] = text.substring(0, text.indexof("\"")); } } } } } catch (xmlexception | ioexception e) { e.printstacktrace(); } return defaultattibts; }
Comments
Post a Comment