本文整理汇总了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();
}
}