com.itextpdf.awt.PdfGraphics2D Java Examples
The following examples show how to use
com.itextpdf.awt.PdfGraphics2D.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SwingExportUtil.java From mzmine3 with GNU General Public License v2.0 | 6 votes |
/** * Writes swing to pdf * * @param panel * @param fileName * @throws DocumentException * @throws Exception */ public static void writeToPDF(JComponent panel, File fileName) throws IOException, DocumentException { // print the panel to pdf int width = panel.getWidth(); int height = panel.getHeight(); logger.info( () -> MessageFormat.format("Exporting panel to PDF file (width x height; {0} x {1}): {2}", width, height, fileName.getAbsolutePath())); Document document = new Document(new Rectangle(width, height)); PdfWriter writer = null; try { writer = PdfWriter.getInstance(document, new FileOutputStream(fileName)); document.open(); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(width, height); Graphics2D g2 = new PdfGraphics2D(contentByte, width, height, new DefaultFontMapper()); panel.print(g2); g2.dispose(); contentByte.addTemplate(template, 0, 0); document.close(); writer.close(); } finally { if (document.isOpen()) { document.close(); } } }
Example #2
Source File: JFreeChartTest.java From testarea-itext5 with GNU Affero General Public License v3.0 | 5 votes |
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); //instead of // cellJ.setImage(chartImage); //the OP now uses Chunk chunk = new Chunk(chartImage, 20, -50); cellJ.addElement(chunk); //presumably with different contents of the other cells at hand 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(); }
Example #3
Source File: SwingExportUtil.java From mzmine2 with GNU General Public License v2.0 | 5 votes |
/** * Writes swing to pdf * * @param panel * @param fileName * @throws DocumentException * @throws Exception */ public static void writeToPDF(JComponent panel, File fileName) throws IOException, DocumentException { // print the panel to pdf int width = panel.getWidth(); int height = panel.getHeight(); logger.info( () -> MessageFormat.format("Exporting panel to PDF file (width x height; {0} x {1}): {2}", width, height, fileName.getAbsolutePath())); Document document = new Document(new Rectangle(width, height)); PdfWriter writer = null; try { writer = PdfWriter.getInstance(document, new FileOutputStream(fileName)); document.open(); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(width, height); Graphics2D g2 = new PdfGraphics2D(contentByte, width, height, new DefaultFontMapper()); panel.print(g2); g2.dispose(); contentByte.addTemplate(template, 0, 0); document.close(); writer.close(); } finally { if (document.isOpen()) { document.close(); } } }