我被这个问题困扰了一段时间。我在画一张画 pie chart
并显示 jtable data
. 我不能 align
文件中的内容 pdf file
. 如何对齐 jtable data
以及 pie chart
为了这个 pdf
. 我已经提到我的 code
下面有一个 image
.
如你所见,我需要调整我的 jtable data
有点顶,而且它必须适合 width
的 page
.
private void graphical_to_pdf() {
graph_table.setSize(graph_table.getPreferredSize());
JTableHeader th = graph_table.getTableHeader();
th.setSize(th.getPreferredSize());
PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 800, 800, 0.65f);
com.itextpdf.text.Font f = new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.TIMES_ROMAN, 18.0f, com.itextpdf.text.Font.NORMAL, BaseColor.BLACK);
Document doc = new Document(new com.itextpdf.text.Rectangle(900, 900));
//Creating a paper to store the jtable contents
Paper paper = new Paper();
paper.setSize(800, 800);
paper.setImageableArea(0, 100, 800, 800);
PageFormat pf = new PageFormat();
pf.setPaper(paper);
Printable printable = graph_table.getPrintable(JTable.PrintMode.NORMAL, null, null);
try {
PdfWriter writer;
writer = PdfWriter.getInstance(doc, new FileOutputStream(save_pdf.getSelectedFile().getAbsoluteFile() + ".pdf"));
writer.setViewerPreferences(PdfWriter.PageLayoutSinglePage);
doc.open();
int width = 375;
int height = 300;
JFreeChart chart = create_pie_chart_count();
doc.add(new Paragraph("Some TExt", f));
doc.add(new Paragraph("Some Text", f));
doc.add(new Paragraph("Document Generated On - " + generatedDate, f));
BufferedImage bufferedImage = chart.createBufferedImage(width, height);
Image image = Image.getInstance(writer, bufferedImage, 1.0f);
image.scalePercent(100f);
image.setAlignment(image.MIDDLE);
doc.add(image);
PdfAction action = PdfAction.gotoLocalPage(1, pdfDest, writer);
writer.setOpenAction(action);
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
int page = 0;
int result = Printable.NO_SUCH_PAGE;
PdfContentByte cb = writer.getDirectContent();
do {
result = printable.print(g, pf, page);
if (result == Printable.PAGE_EXISTS) {
cb.saveState();
Graphics2D g2 = cb.createGraphics(650, 225);
result = printable.print(g2, pf, page);
g2.dispose();
cb.restoreState();
page++;
doc.newPage();
}
} while (result == Printable.PAGE_EXISTS);
g.dispose();
} catch (Exception e) {
} finally {
doc.close();
}
}
1条答案
按热度按时间atmip9wb1#
请看以下问题的答案:如何在itext中定位pdfgraphis2d对象?
如您所见,通常不直接将图表或表格等对象绘制到直接内容。相反,您应该创建一个
PdfTemplate
吸引。使用PdfTemplate
你可以把它包在Image
. 别担心:如果PdfTemplate
包含矢量数据Image
将是一个矢量图像;它不会被光栅化(这将导致分辨率的损失)。一旦你有
Image
对象,有许多不同的选项来调整大小、定位和组织它们。阅读以下问题的答案时,您可以了解不同的选项:如何将图像和文本并排显示itext
itextsharp图像不会挨在一起
向columntext itext添加文本和对齐的图像
...