使用 JFreechart 和 pdfbox,我做了一些类似于您要求我制作的报告的事情。制作饼图如下:
public class PieChartExample {
public static void main(String[] args) {
// Create a simple pie chart
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("Chrome", new Integer(42));
pieDataset.setValue("Explorer", new Integer(24));
pieDataset.setValue("Firefox", new Integer(24));
pieDataset.setValue("Safari", new Integer(12));
pieDataset.setValue("Opera", new Integer(8));
JFreeChart chart = ChartFactory.createPieChart3D(
"Browser Popularity", // Title
pieDataset, // Dataset
true, // Show legend
true, // Use tooltips
false // Configure chart to generate URLs?
);
try {
ChartUtilities.saveChartAsJPEG(new File("C:\\Users\\myname\\Desktop\\chart.jpg"), chart, 500, 300);
} catch (Exception e) {
System.out.println("Problem occurred creating chart.");
}
}
}
上面的示例来自一个 pdf 文件,我认为可以在他们的网站上找到,如果您需要的话,它还有其他图表的示例。保存后,我可以将其导入到 pdf 中,类似于:
try {
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
document.addPage(page);
InputStream in = new FileInputStream(new File("c:/users/myname/desktop/chart.jpg"));
PDJpeg img = new PDJpeg(document, in);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.drawImage(img, 10, 300);
contentStream.close();
document.save("pathway/to/save.pdf");
} catch (IOException e) {
System.out.println(e);
} catch (COSVisitorException cos) {
System.out.println(cos);
}
itext 也是一个很好的 pdf 操作库,但它在一定程度上是商业化的,而 pdfbox 应该是开源的。
祝你好运!