添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
I'm trying to create charts with a custom font given as ttf-file and then put them in an itext pdf-document using a DefaultFontMapper.
The code looks like this:

Code: Select all

imports ....
public class ITextFontTest {
	public static void main(String[] args) {
		Document document = new Document(PageSize.A4.rotate());
		try {
			PdfWriter writer = PdfWriter.getInstance(document,
					new FileOutputStream("HelloWorld.pdf"));
			document.open();
			BaseFont bf = BaseFont.createFont("MyFont.ttf",
			BaseFont.CP1252, BaseFont.EMBEDDED);
			BaseFont bf = BaseFont.createFont("MyFont.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
			Font font = new Font(bf, 10);
			document.add(new Paragraph("MyFont.ttf Test ", font));
			// Font Mapping 
			DefaultFontMapper mapper = new DefaultFontMapper();
			// font file is in the curtrent directory 
			mapper.insertDirectory(".");
			DefaultFontMapper.BaseFontParameters pp =
				mapper.getBaseFontParameters("My Font");
			System.out.println("My Font found ");
			if (pp!=null) {
				pp.encoding = BaseFont.IDENTITY_H;
				System.out.println("Fontname: "+ pp.fontName);
				System.out.println("embedded: + "+ pp.embedded);
			} else {
				System.out.println("My Font not found! ");
			// Adding the Chart to PDF
			PdfContentByte cb = writer.getDirectContent();
			PdfTemplate tp = cb.createTemplate(750, 350);
			Graphics2D g2d = tp.createGraphics(750, 350, mapper);
			Rectangle2D r2d = new Rectangle2D.Double(0, 0, 750, 350);
			JFreeChart chart = getChart();
			chart.draw(g2d, r2d);
			g2d.dispose();
			cb.addTemplate(tp, document.getPageSize().width() / 2 - 750 / 2, document.bottom() + 10);
		} catch (DocumentException de) {
			System.err.println(de.getMessage());
		} catch (IOException ioe) {
			System.err.println(ioe.getMessage());
		document.close();
	private static JFreeChart getChart() {
	DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		for (int i = 0; i < 10; i++) {
			dataset.setValue(Math.random(), "first value", "probe " + i);
			dataset.setValue(Math.random(), "second value", "probe " + i);
		final StackedBarRenderer myRenderer = new StackedBarRenderer();
		final CategoryPlot myPlot = new CategoryPlot();
		myPlot.setDataset(dataset);
		myPlot.setRenderer(myRenderer);
		myRenderer.setSeriesPaint(0, java.awt.Color.lightGray);
		myRenderer.setSeriesPaint(1, java.awt.Color.red);
		myPlot.setDomainAxis(new CategoryAxis());
		myPlot.setRangeAxis(new NumberAxis("NumberAxis"));
		myPlot.setOrientation(PlotOrientation.VERTICAL);
		java.awt.Font font = new java.awt.Font("My Font", java.awt.Font.PLAIN, 12);
		final JFreeChart chart = new JFreeChart("Test Chart with MyFont.ttf legends ", font, myPlot,true);
		chart.setBackgroundPaint(Color.white);
		chart.setBorderPaint(Color.black);
		return chart;
On my Computer (Windows) I get a PDF with the font embedded - as expected. Under Linux and even on other Windows systems the pdf written contains also the embedded font for the iText paragraph, but unfortunately the mapping for JFreeChart doesn't seem to work - all fonts in the chart are Helvetica (can be seen in document properties-fonts in Acrobat Reader).
I don't know if this is a JFreeChart problem but perhaps someone has at least a hint or can show me a direction where to look further ...
Thanks ahead!
Harris