本文整理匯總了Java中org.docx4j.fonts.PhysicalFont類的典型用法代碼示例。如果您正苦於以下問題:Java PhysicalFont類的具體用法?Java PhysicalFont怎麽用?Java PhysicalFont使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
PhysicalFont類屬於org.docx4j.fonts包,在下文中一共展示了PhysicalFont類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: configSimSunFont
import org.docx4j.fonts.PhysicalFont; //導入依賴的package包/類
/**
* 為 {@link org.docx4j.openpackaging.packages.WordprocessingMLPackage} 配置中文字體
*
* @param wordMLPackage
* @throws Exception
*/
protected void configSimSunFont(WordprocessingMLPackage wordMLPackage) throws Exception {
Mapper fontMapper = new IdentityPlusMapper();
wordMLPackage.setFontMapper(fontMapper);
String fontFamily = "SimSun";
URL simsunUrl = this.getClass().getResource("/org/noahx/html2docx/simsun.ttc"); //加載字體文件(解決linux環境下無中文字體問題)
PhysicalFonts.addPhysicalFonts(fontFamily, simsunUrl);
PhysicalFont simsunFont = PhysicalFonts.get(fontFamily);
fontMapper.put(fontFamily, simsunFont);
RFonts rfonts = Context.getWmlObjectFactory().createRFonts(); //設置文件默認字體
rfonts.setAsciiTheme(null);
rfonts.setAscii(fontFamily);
wordMLPackage.getMainDocumentPart().getPropertyResolver()
.getDocumentDefaultRPr().setRFonts(rfonts);
}
示例2: configInternalFonts
import org.docx4j.fonts.PhysicalFont; //導入依賴的package包/類
public void configInternalFonts(){
ChineseFont[] fonts = ChineseFont.values();
for (ChineseFont font : fonts){
FontScheme fontScheme = COMPLIED_FONTSCHEME.get(font.getFontName());
if (fontScheme == null) {
fontScheme = new FontScheme(font.getFontName(), font.getFontAlias(), font.getFontURL());
COMPLIED_FONTSCHEME.putIfAbsent(font.getFontName(), fontScheme);
PhysicalFont physicalFont = PhysicalFonts.get(fontScheme.getFontName());
if(physicalFont == null){
//加載字體文件(解決linux環境下無中文字體問題)
PhysicalFonts.addPhysicalFonts(fontScheme.getFontName(), fontScheme.getFontURL());
LOG.debug("Add Internal " + fontScheme.toString() );
}
}
}
}
示例3: configExternalFonts
import org.docx4j.fonts.PhysicalFont; //導入依賴的package包/類
public void configExternalFonts() {
/**
* 加載外部字體庫:參數格式如:字體名稱:字體別名:字體URL;多個以 ",; \t\n"分割
* 隸書:LiSu:URL,宋體:SimSun:URL
*/
String external_fonts_mapping = Docx4jProperties.getProperty(Docx4jConstants.DOCX4J_FONTS_EXTERNAL_MAPPING);
if(external_fonts_mapping!=null && external_fonts_mapping.length() > 0){
String[] fonts_mappings = StringUtils.tokenizeToStringArray(external_fonts_mapping);
if(fonts_mappings.length > 0){
for (String fonts_mapping : fonts_mappings) {
String[] font = fonts_mapping.split(":");
FontScheme fontScheme = COMPLIED_FONTSCHEME.get(font[0]);
if (fontScheme != null) {
continue;
}
try {
fontScheme = new FontScheme(font[0], font[1], new URL(font[2]));
COMPLIED_FONTSCHEME.putIfAbsent(font[0], fontScheme);
PhysicalFont physicalFont = PhysicalFonts.get(fontScheme.getFontName());
if(physicalFont == null){
//加載字體文件(解決linux環境下無中文字體問題)
PhysicalFonts.addPhysicalFonts(fontScheme.getFontName(), fontScheme.getFontURL());
LOG.debug("Add External " + fontScheme.toString() );
}
} catch (MalformedURLException e) {
// ignore
}
}
}
}
}
示例4: setPhysicalFont
import org.docx4j.fonts.PhysicalFont; //導入依賴的package包/類
/**
* 為 {@link org.docx4j.openpackaging.packages.WordprocessingMLPackage} 增加新的字體
*/
public static void setPhysicalFont(WordprocessingMLPackage wmlPackage,PhysicalFont physicalFont) throws Exception {
Mapper fontMapper = wmlPackage.getFontMapper() == null ? new IdentityPlusMapper() : wmlPackage.getFontMapper();
//分別設置字體名和別名對應的字體庫
fontMapper.put(physicalFont.getName(), physicalFont );
//設置文檔字體庫
wmlPackage.setFontMapper(fontMapper, true);
}
示例5: getFontMapper
import org.docx4j.fonts.PhysicalFont; //導入依賴的package包/類
public Mapper getFontMapper() throws Exception {
// Set up font mapper (optional)
// example of mapping font Times New Roman which doesn't have certain Arabic glyphs
// eg Glyph "ي" (0x64a, afii57450) not available in font "TimesNewRomanPS-ItalicMT".
// eg Glyph "ج" (0x62c, afii57420) not available in font "TimesNewRomanPS-ItalicMT".
// to a font which does
PhysicalFonts.get("Arial Unicode MS");
/*
* This mapper uses Panose to guess the physical font which is a closest fit for the font used in the document.
* (這個映射器使用Panose算法猜測最適合這個文檔使用的物理字體。)
*
* Panose是一種依照字體外觀來進行分類的方法。我們可以通過PANOSE體係將字體的外觀特征進行整理,並且與其它字體歸類比較。
* Panose的原形在1985年由Benjamin Bauermeister開發,當時一種字體由7位16進製數字定義,現在則發展為10位,也就是字體的十種特征。這每一位數字都給出了它定義的一種視覺外觀的量度,如筆劃的粗細或是字體襯線的樣式等。
* Panose定義的範圍:Latin Text,Latin Script,Latin Decorative,Iconographic,Japanese Text,Cyrillic Text,Hebrew。
*
* It is most likely to be suitable on Linux or OSX systems which don't have Microsoft's fonts installed.
* (它很可能適用於沒有安裝Microsoft字體的Linux或OSX係統。)
*
* 1、獲取Microsoft字體我們需要這些:a.在Microsoft平台上,嵌入PDF輸出; b. docx4all - 所有平台 - 填充字體下拉列表
* setupMicrosoftFontFilenames();
* 2、 自動檢測係統上可用的字體
* PhysicalFonts.discoverPhysicalFonts();
*
*/
//Mapper fontMapper = new BestMatchingMapper();
/*
*
* This mapper automatically maps document fonts for which the exact font is physically available.
* Think of this as an identity mapping. For this reason, it will work best on Windows, or a system on
* which Microsoft fonts have been installed.
* (此映射器自動映射確切可用的文檔字體,將此視為標識映射;基於這個原因,它在Windows係統或安裝了微軟字體庫的係統運行的更好。)
* You can manually add your own additional mappings if you wish.
* 如果需要,你可以手動添加自己的字體映射
*
* 1、 自動檢測係統上可用的字體
* PhysicalFonts.discoverPhysicalFonts();
*
*/
Mapper fontMapper = new IdentityPlusMapper();
//遍曆自定義的字體庫信息
for (FontScheme fontScheme : COMPLIED_FONTSCHEME.values()) {
//獲取字體庫
PhysicalFont physicalFont = PhysicalFonts.get(fontScheme.getFontName());
//分別設置字體名和別名對應的字體庫
fontMapper.put(fontScheme.getFontName(), physicalFont );
fontMapper.put(fontScheme.getFontAlias(), physicalFont );
}
//進行中文字體兼容處理
fontMapper.put("微軟雅黑",PhysicalFonts.get("Microsoft Yahei"));
fontMapper.put("黑體",PhysicalFonts.get("SimHei"));
fontMapper.put("楷體",PhysicalFonts.get("KaiTi"));
fontMapper.put("隸書", PhysicalFonts.get("LiSu"));
fontMapper.put("宋體",PhysicalFonts.get("SimSun"));
fontMapper.put("宋體擴展",PhysicalFonts.get("simsun-extB"));
fontMapper.put("新宋體",PhysicalFonts.get("NSimSun"));
fontMapper.put("仿宋",PhysicalFonts.get("FangSong"));
fontMapper.put("仿宋_GB2312",PhysicalFonts.get("FangSong_GB2312"));
fontMapper.put("幼圓",PhysicalFonts.get("YouYuan"));
fontMapper.put("華文宋體",PhysicalFonts.get("STSong"));
fontMapper.put("華文仿宋", PhysicalFonts.get("STFangsong"));
fontMapper.put("華文中宋",PhysicalFonts.get("STZhongsong"));
fontMapper.put("華文行楷", PhysicalFonts.get("STXingkai"));
return fontMapper;
}
示例6: toNode
import org.docx4j.fonts.PhysicalFont; //導入依賴的package包/類
@Override
public Node toNode(AbstractWmlConversionContext context, Object unmarshalledNode,
Node modelContent, TransformState state, Document doc)
throws TransformerException {
R.Sym modelData = (R.Sym)unmarshalledNode;
String fontName = modelData.getFont();
String textValue = modelData.getChar();
PhysicalFont pf = context.getWmlPackage().getFontMapper().get(fontName);
char chValue = '\0';
Typeface typeface = null;
if (pf != null) {
typeface = pf.getTypeface();
if (typeface != null) {
if (textValue.length() > 1) {
try {
chValue = (char)Integer.parseInt(textValue, 16);
}
catch (NumberFormatException nfe) {
chValue = '\0';
}
}
else {
chValue = textValue.charAt(0);
}
if (chValue != '\0') {
if (chValue > 0xf000) { //let's check first the character in the lower ascii (Pre-process according to ECMA-376 2.3.3.29)
chValue -= 0xf000;
}
if (typeface.mapChar(chValue) == 0) {
chValue += 0xf000;
if (typeface.mapChar(chValue) == 0) {
chValue = '\0';
}
}
if (chValue != '\0') {//character was found
textValue = Character.toString(chValue);
}
}
}
}
Text theChar = doc.createTextNode(textValue);
DocumentFragment docfrag = doc.createDocumentFragment();
if (pf==null) {
log.warn("No physical font present for:" + fontName);
docfrag.appendChild( theChar );
} else {
Element foInline = doc.createElementNS("http://www.w3.org/1999/XSL/Format", "fo:inline");
docfrag.appendChild(foInline);
foInline.setAttribute("font-family", pf.getName() );
foInline.appendChild(theChar);
}
return docfrag;
}
示例7: output
import org.docx4j.fonts.PhysicalFont; //導入依賴的package包/類
/** Create a pdf version of the document.
*
* @param os
* The OutputStream to write the pdf to
*
* */
public void output(OutputStream os) throws Docx4JException {
try {
// Put the html in result
org.w3c.dom.Document xhtmlDoc = org.docx4j.XmlUtils.neww3cDomDocument();
javax.xml.transform.dom.DOMResult result = new javax.xml.transform.dom.DOMResult(xhtmlDoc);
AbstractHtmlExporter exporter = new HtmlExporter();
exporter.html(wordMLPackage, result, false,
System.getProperty("java.io.tmpdir") ); // false -> don't use HTML fonts.
// Now render the XHTML
org.xhtmlrenderer.pdf.ITextRenderer renderer = new org.xhtmlrenderer.pdf.ITextRenderer();
// 4. Use addFont code like that below as necessary for the fonts
// See https://xhtmlrenderer.dev.java.net/guide/users-guide-r7.html#xil_32
org.xhtmlrenderer.extend.FontResolver resolver = renderer.getFontResolver();
Map fontsInUse = wordMLPackage.getMainDocumentPart().fontsInUse();
Iterator fontMappingsIterator = fontsInUse.entrySet().iterator();
while (fontMappingsIterator.hasNext()) {
Map.Entry pairs = (Map.Entry)fontMappingsIterator.next();
if(pairs.getKey()==null) {
log.info("Skipped null key");
pairs = (Map.Entry)fontMappingsIterator.next();
}
String fontName = (String)pairs.getKey();
PhysicalFont pf = wordMLPackage.getFontMapper().getFontMappings().get(fontName);
if (pf==null) {
log.error("Document font " + fontName + " is not mapped to a physical font!");
continue;
}
embed(renderer, pf);
// For any font we embed, also embed the bold, italic, and bold italic substitute
// .. at present, we can't tell which of these forms are actually used, so add them all
// bold, italic etc
PhysicalFont pfVariation = PhysicalFonts.getBoldForm(pf);
if (pfVariation!=null) {
embed(renderer, pfVariation);
}
pfVariation = PhysicalFonts.getBoldItalicForm(pf);
if (pfVariation!=null) {
embed(renderer, pfVariation);
}
pfVariation = PhysicalFonts.getItalicForm(pf);
if (pfVariation!=null) {
embed(renderer, pfVariation);
}
}
// TESTING
// xhtmlDoc = org.docx4j.XmlUtils.neww3cDomDocument();
// try {
// xhtmlDoc = XmlUtils.getNewDocumentBuilder().parse(new File("C:\\Users\\jharrop\\workspace\\docx4all\\sample-docs\\comic.html"));
// } catch (Exception e) {
// e.printStackTrace();
// }
renderer.setDocument(xhtmlDoc, null);
renderer.layout();
renderer.createPDF(os);
} catch (Exception e) {
throw new Docx4JException("Failed creating PDF via HTML " ,e);
}
}
示例8: createContent
import org.docx4j.fonts.PhysicalFont; //導入依賴的package包/類
public static void createContent(MainDocumentPart wordDocumentPart ) {
/*
* NB, this currently works nicely with
* viaIText, and viaXSLFO (provided
* you view with Acrobat Reader .. it
* seems to overwhelm pdfviewer, which
* is weird, since viaIText works in both).
*/
try {
// Do this explicitly, since we need
// it in order to create our content
PhysicalFonts.discoverPhysicalFonts();
Map<String, PhysicalFont> physicalFontMap = PhysicalFonts.getPhysicalFonts();
Iterator physicalFontMapIterator = physicalFontMap.entrySet().iterator();
while (physicalFontMapIterator.hasNext()) {
Map.Entry pairs = (Map.Entry)physicalFontMapIterator.next();
if(pairs.getKey()==null) {
pairs = (Map.Entry)physicalFontMapIterator.next();
}
String fontName = (String)pairs.getKey();
PhysicalFont pf = (PhysicalFont)pairs.getValue();
System.out.println("Added paragraph for " + fontName);
addObject(wordDocumentPart, sampleText, fontName );
// bold, italic etc
PhysicalFont pfVariation = PhysicalFonts.getBoldForm(pf);
if (pfVariation!=null) {
addObject(wordDocumentPart, sampleTextBold, pfVariation.getName() );
}
pfVariation = PhysicalFonts.getBoldItalicForm(pf);
if (pfVariation!=null) {
addObject(wordDocumentPart, sampleTextBoldItalic, pfVariation.getName() );
}
pfVariation = PhysicalFonts.getItalicForm(pf);
if (pfVariation!=null) {
addObject(wordDocumentPart, sampleTextItalic, pfVariation.getName() );
}
}
} catch (Exception e) {
e.printStackTrace();
}
}