Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

iText7 does not have a built-in graphing or charting library, however, you can use external libraries like JFreeChart or Google Charts API and then embed the chart in your PDF using iText7. Here is an example of how to use JFreeChart to create a pie chart and then add it to a PDF document using iText7:

// Create a new PDF document
PdfDocument pdfDoc = new PdfDocument(new PdfWriter("piechart.pdf"));

// Create a new document
Document doc = new Document(pdfDoc);

// Define data for pie chart
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("Slice 1", 20);
dataset.setValue("Slice 2", 30);
dataset.setValue("Slice 3", 50);

// Create a new pie chart
JFreeChart pieChart = ChartFactory.createPieChart("Pie Chart", dataset);

// Create a new chart panel to draw the chart
ChartPanel chartPanel = new ChartPanel(pieChart);

// Convert chart panel to image
BufferedImage image = pieChart.createBufferedImage(500, 500);

// Create an image element with the chart image
Image chartImage = new Image(ImageDataFactory.create(image, null));

// Add the chart image to the PDF document
doc.add(chartImage);

// Close the PDF document
doc.close();

This will create a new PDF document and add a pie chart to it using JFreeChart. You can customize the chart by changing the dataset or modifying the chart options in JFreeChart.