0

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

4

2 回答 2

1

I need to export cyrillic fonts into jasperreport fo generate pdf. Fonts must be included into jar. In jasperreport v5.6.1 I resolve this so:
Put "ttf" file into "default package".
Put jasperreport_extension.properties into "default package":
net.sf.jasperreports.extension.registry.factory.ytrpaymentorderpdffonts=testpdf.YtrJasperFontExtensionFactory
Put mine class "YtrJasperFontExtensionFactory" for font extension into package "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);
    }
}

And fonts loaded from stream(jar) now.

于 2014-10-24T11:35:59.243 回答
0

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

于 2012-06-22T09:25:01.867 回答