如何使用CMYK顏色生成JFreeChart?如何僅使用CMYK顏色生成JFreeChart
我是否必須撥打所有的.setPaint(new CmykColor(...))
或者有沒有更優雅的方式? 即使我不知道所有可能的.setPaint(...)
方法。
重現這個問題我寫了一個小單元測試。它生成一個圖表,最後將其添加到只允許CMYK的PDF/x。
如果沒有,則測試變爲綠色com.lowagie.text.pdf.PdfXConformanceException
:不允許使用色彩空間RGB。拋出異常。
public class TestChart
{
public static final int WIDTH = 500;
public static final int HEIGHT = 400;
private JFreeChart chart;
@Before
public void createChart()
{
final DefaultPieDataset dataSet = new DefaultPieDataset();
dataSet.setValue("United States", 4.54);
dataSet.setValue("Brazil", 2.83);
this.chart = ChartFactory.createPieChart("World Population by countries", dataSet, true, true, false);
// ChartUtilities.saveChartAsPNG(new File("test.png"), chart, width, height);
}
@Test
public void shouldAddChartToPdfX() throws FileNotFoundException, DocumentException
{
final Document document = new Document();
document.addTitle("Test PDF/x");
final PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
pdfWriter.setPDFXConformance(PdfWriter.PDFX1A2001);
document.open();
final PdfContentByte directContent = pdfWriter.getDirectContent();
final PdfTemplate pdfTemplate = directContent.createTemplate(TestChart.WIDTH, TestChart.HEIGHT);
final Graphics2D graphics2d = pdfTemplate.createGraphics(TestChart.WIDTH, TestChart.HEIGHT);
final Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, TestChart.WIDTH, TestChart.HEIGHT);
this.chart.draw(graphics2d, rectangle2d);
graphics2d.dispose();
directContent.addTemplate(pdfTemplate, 0, 0);
document.close();
}
}
要執行此,你需要這個Maven依賴:
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.14</version>
</dependency>