我使用JFreeCharts生成了一个pie char,并将它放在了iText的Image中。 后来把这张图片放在桌子上。 但即使原始图像不是那么大,细胞的大小也在增加。如何减小该单元的大小。
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();
}
}
答案 0 :(得分:1)
我想到的两个选项是:
Chunk
将图像添加到addElement
中包含的单元格中。第一个选项有一个缺点:如果同一行中的其他单元格需要更大的高度,则具有固定大小的单元格最终也会获得更大的高度,单元格中的图像也会如此。
第二种选择没有这个缺点。人们必须尝试使用块中图像的 x 和 y 偏移量。
在OP的情况下,以下值证明是好的:
Chunk chunk = new Chunk(chartImage, 20, -50);
cellJ.addElement(chunk);