Reputation: 231
I have generated a pie char using JFreeCharts and have put it in Image of iText. Later putting this image in table. But the size of the cell is increasing even when the original image is not of that size. How can I reduce size of that cell.
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import com.itextpdf.awt.PdfGraphics2D;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
public class JFreeChartTest {
public static void main(String[] args) {
writeChartToPDF(generatePieChart(), 50, 50, "D://piechart5.pdf");
}
public static JFreeChart generatePieChart() {
DefaultPieDataset dataSet = new DefaultPieDataset();
dataSet.setValue("China", 30);
dataSet.setValue("India", 30);
dataSet.setValue("United States", 40);
JFreeChart chart = ChartFactory.createPieChart(
"", dataSet, false, true, false);
PiePlot piePlot = (PiePlot) chart.getPlot();
piePlot.setBackgroundPaint(Color.WHITE); //set background color white
piePlot.setOutlineVisible(false); // remove background border
piePlot.setLabelGenerator(null); // remove pie section labels
piePlot.setSectionPaint("China", Color.GRAY);
piePlot.setSectionPaint("India", Color.GREEN);
piePlot.setSectionPaint("United States", Color.BLUE);
piePlot.setShadowPaint(Color.WHITE);
return chart;
}
public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName) {
PdfWriter writer = null;
Document document = new Document();
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(
fileName));
document.open();
PdfContentByte pdfContentByte = writer.getDirectContent();
PdfTemplate pdfTemplateChartHolder = pdfContentByte.createTemplate(50,50);
Graphics2D graphics2d = new PdfGraphics2D(pdfTemplateChartHolder,50,50);
Rectangle2D chartRegion = new Rectangle2D.Double(0,0,50,50);
chart.draw(graphics2d,chartRegion);
graphics2d.dispose();
Image chartImage = Image.getInstance(pdfTemplateChartHolder);
document.add(chartImage);
PdfPTable table = new PdfPTable(5);
// the cell object
// we add a cell with colspan 3
PdfPCell cellX = new PdfPCell(new Phrase("A"));
cellX.setBorder(com.itextpdf.text.Rectangle.NO_BORDER);
cellX.setRowspan(6);
table.addCell(cellX);
PdfPCell cellA = new PdfPCell(new Phrase("A"));
cellA.setBorder(com.itextpdf.text.Rectangle.NO_BORDER);
cellA.setColspan(4);
table.addCell(cellA);
PdfPCell cellB = new PdfPCell(new Phrase("B"));
table.addCell(cellB);
PdfPCell cellC = new PdfPCell(new Phrase("C"));
table.addCell(cellC);
PdfPCell cellD = new PdfPCell(new Phrase("D"));
table.addCell(cellD);
PdfPCell cellE = new PdfPCell(new Phrase("E"));
table.addCell(cellE);
PdfPCell cellF = new PdfPCell(new Phrase("F"));
table.addCell(cellF);
PdfPCell cellG = new PdfPCell(new Phrase("G"));
table.addCell(cellG);
PdfPCell cellH = new PdfPCell(new Phrase("H"));
table.addCell(cellH);
PdfPCell cellI = new PdfPCell(new Phrase("I"));
table.addCell(cellI);
PdfPCell cellJ = new PdfPCell(new Phrase("J"));
cellJ.setColspan(2);
cellJ.setRowspan(3);
cellJ.setImage(chartImage);
table.addCell(cellJ);
PdfPCell cellK = new PdfPCell(new Phrase("K"));
cellK.setColspan(2);
table.addCell(cellK);
PdfPCell cellL = new PdfPCell(new Phrase("L"));
cellL.setColspan(2);
table.addCell(cellL);
PdfPCell cellM = new PdfPCell(new Phrase("M"));
cellM.setColspan(2);
table.addCell(cellM);
document.add(table);
} catch (Exception e) {
e.printStackTrace();
}
document.close();
}
}
Upvotes: 2
Views: 2322
Reputation: 95918
The two options coming to my mind are:
Chunk
using addElement
.The first option has one disadvantage: If the other cells in the same row require a larger height, a cell with a fixed size eventually also gets that larger height, and so would the image in the cell.
The second option does not have this disadvantage. One has to experiment a bit with the x and y offsets of the image in the chunk.
In the case of the OP the following values turned out to be good:
Chunk chunk = new Chunk(chartImage, 20, -50);
cellJ.addElement(chunk);
Upvotes: 1