将jasperreport导出为.pdf时如何设置jasperreport字体

时间:2012-06-22 08:30:53

标签: java jasper-reports

当jasperreport在java应用程序中导出为.pdf时,如何设置它的字体。

2 个答案:

答案 0 :(得分:1)

我需要将西里尔字体导出到jasperreport中以生成pdf。字体必须包含在jar中。在jasperreport v5.6.1中我解决了这个问题:
把" ttf"将文件存入"默认包"。
将jasperreport_extension.properties放入"默认包":
net.sf.jasperreports.extension.registry.factory.ytrpaymentorderpdffonts = testpdf.YtrJasperFontExtensionFactory
把我的班级" YtrJasperFontExtensionFactory"将字体扩展到包" testpdf":

package testpdf;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.sf.jasperreports.engine.DefaultJasperReportsContext;

import net.sf.jasperreports.engine.JRPropertiesMap;
import net.sf.jasperreports.engine.fonts.FontFamily;
import net.sf.jasperreports.engine.fonts.SimpleFontFace;
import net.sf.jasperreports.engine.fonts.SimpleFontFamily;
import net.sf.jasperreports.extensions.ExtensionsRegistry;
import net.sf.jasperreports.extensions.ExtensionsRegistryFactory;



public class YtrJasperFontExtensionFactory implements ExtensionsRegistryFactory, ExtensionsRegistry {
    private List <FontFamily> families;

    @Override
    public ExtensionsRegistry createRegistry(String string, JRPropertiesMap jrpm) {
        return new YtrJasperFontExtensionFactory();
    }

    @Override
    public List getExtensions(Class type) {
        if (type.equals(FontFamily.class)) {
            if (families == null) {
                families = new ArrayList<FontFamily>();
                loadFonts();
            }
            return new ArrayList<FontFamily>(families);
        } else {
            return Collections.emptyList();
        }
    }

    private void loadFonts() {                 
            DefaultJasperReportsContext dJRC = DefaultJasperReportsContext.getInstance();

                SimpleFontFamily family = new SimpleFontFamily();
                family.setName("Times New Roman");
                family.setPdfEmbedded(true); 

                SimpleFontFace sffNormal = new SimpleFontFace(dJRC);
                sffNormal.setTtf("Times_New_Roman.ttf");
                family.setNormalFace(sffNormal);

                family.setPdfEncoding("Cp1251");
                families.add(family);
    }
}

现在从流(jar)加载字体。

答案 1 :(得分:0)

您可以尝试将字体设置为参数。然后,您可以传递要在运行时设置的字体名称。或者只是改编一下。 http://jasperforge.org/plugins/espforum/view.php?group_id=102&forumid=103&topicid=5860