Code: Select all
Document document = new Document(PageSize.A4.rotate());
Font titleFont = new Font(FontFamily.TIMES_ROMAN, 40.f, Font.NORMAL, BaseColor.BLUE);
Font subTitleFont = new Font(FontFamily.TIMES_ROMAN, 25.f, Font.NORMAL, BaseColor.RED);
Font dateFont = new Font(FontFamily.TIMES_ROMAN, 20.f, Font.NORMAL, BaseColor.GREEN);
PdfWriter writer=null;
try {
writer=PdfWriter.getInstance(document,new FileOutputStream(filePath.getText()));
writer.setPageEvent(new Footer(document));
document.open();
document.add(setParagraph("Stoker Monitor Report", titleFont));
document.add(setParagraph(name.getText(), subTitleFont));
document.add(setParagraph(date.getText(), dateFont));
Rectangle page=writer.getPageSize();
float sizeY=page.getHeight();
float sizeX=page.getWidth();
System.out.println("page size is "+Float.toString(sizeX)+" X "+Float.toString(sizeY));
float currentY=writer.getVerticalPosition(false);
System.out.println("distance from top="+Float.toString(sizeY-currentY));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(Chart.getInstance().getImage(), "png", bos);
Image image = Image.getInstance(bos.toByteArray());
image.setRotationDegrees(90);
image.scalePercent(10.f);
document.add(image);
} catch (DocumentException | IOException e1) {
System.err.println("Unable to open " + filePath.getText() + " for writing");
e1.printStackTrace();
}
document.close();
The bottom line is I want to take the JFreeChart created, effectively as a screenshot, and add it to my PDF document in landscape orientation. My method above seems to have been the wrong choice although it was the easiest I found. So my question is what is the proper method I should be using to accomplish what I thought would be a simple task? TIA.