use of com.itextpdf.text.pdf.PdfTemplate in project polyGembler by c-zhou.
the class JfreeChart method print.
public void print(String plot_pdf) {
try {
float width = jframe.getSize().width, height = jframe.getSize().height;
Document document = new Document(new Rectangle(width, height));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(plot_pdf));
document.open();
PdfContentByte canvas = writer.getDirectContent();
PdfTemplate template = canvas.createTemplate(width, height);
Graphics2D g2d = new PdfGraphics2D(template, width, height);
jframe.paint(g2d);
g2d.dispose();
canvas.addTemplate(template, 0, 0);
document.close();
} catch (FileNotFoundException | DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of com.itextpdf.text.pdf.PdfTemplate in project Java-Matrix-Benchmark by lessthanoptimal.
the class UtilPlotPdf method saveAsPdf.
public static void saveAsPdf(JFreeChart chart, String FILENAME, int width, int height) {
File parent = new File(new File(FILENAME).getParent());
if (!parent.exists()) {
if (!parent.mkdirs())
throw new RuntimeException("Can't make directory path");
}
Document document = new Document(new Rectangle(width, height));
try {
FileOutputStream file = new FileOutputStream(FILENAME);
PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(width, height);
Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper());
Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height);
chart.draw(g2d, r2d);
g2d.dispose();
cb.addTemplate(tp, 0, 0);
document.close();
g2d.dispose();
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.itextpdf.text.pdf.PdfTemplate in project Zong by Xenoage.
the class PdfPrinter method print.
/**
* Prints the given {@link Layout} into the given PDF output stream.
*/
public static void print(Layout layout, OutputStream out) {
Document document = new Document();
PdfWriter writer = null;
try {
writer = PdfWriter.getInstance(document, out);
} catch (Exception e) {
handle(warning(Voc.ErrorWhilePrinting));
}
document.open();
PdfContentByte cb = writer.getDirectContent();
It<Page> pages = it(layout.getPages());
for (Page page : pages) {
// create PDF page
Size2f pageSize = page.getFormat().getSize();
float width = Units.mmToPx(pageSize.width, 1);
float height = Units.mmToPx(pageSize.height, 1);
document.newPage();
PdfTemplate tp = cb.createTemplate(width, height);
// fill PDF page
Graphics2D g2d = new PdfGraphics2D(cb, width, height);
INSTANCE.log(Companion.remark("Printing page " + pages.getIndex() + "..."));
LayoutRenderer.paintToCanvas(layout, pages.getIndex(), new AwtCanvas(g2d, pageSize, CanvasFormat.Vector, CanvasDecoration.None, CanvasIntegrity.Perfect));
// finish page
g2d.dispose();
cb.addTemplate(tp, 0, 0);
}
document.close();
}
use of com.itextpdf.text.pdf.PdfTemplate in project mzmine2 by mzmine.
the class ChartExportUtil method writeChartToPDF.
/**
* This method saves a chart as a PDF with given dimensions
*
* @param chart
* @param width
* @param height
* @param fileName is a full path
*/
public static void writeChartToPDF(JFreeChart chart, int width, int height, File fileName) throws Exception {
PdfWriter writer = null;
Document document = new Document(new Rectangle(width, height));
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
document.open();
PdfContentByte contentByte = writer.getDirectContent();
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper());
Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width, height);
chart.draw(graphics2d, rectangle2d);
graphics2d.dispose();
contentByte.addTemplate(template, 0, 0);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
document.close();
}
}
use of com.itextpdf.text.pdf.PdfTemplate in project Java-Matrix-Benchmark by lessthanoptimal.
the class UtilPlotPdf method saveAsPdf.
public static void saveAsPdf(LegendTitle legend, String FILENAME, int width, int height) {
Document document = new Document(new Rectangle(width, height));
try {
FileOutputStream file = new FileOutputStream(FILENAME);
PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(width, height);
Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper());
Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height);
legend.draw(g2d, r2d);
g2d.dispose();
cb.addTemplate(tp, 0, 0);
document.close();
g2d.dispose();
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations