Java Code Examples for com.itextpdf.text.pdf.PdfContentByte#addTemplate()
The following examples show how to use
com.itextpdf.text.pdf.PdfContentByte#addTemplate() .
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: PlotUtils.java From Scripts with GNU General Public License v3.0 | 6 votes |
/** * Exports the specified JFreeChart to a PDF file using * <a href="http://itextpdf.com" target="_blank">iText</a>, bundled with * Fiji. The destination file is specified by the user in a save dialog * prompt. An error message is displayed if the file could not be saved. * Does nothing if {@code chart} is {@code null}. * * @param chart * the <a href="http://javadoc.imagej.net/JFreeChart/" target= * "_blank">JFreeChart </a> to export. * @param bounds * the Rectangle delimiting the boundaries within which the chart * should be drawn. * @see #exportChartAsPDF(JFreeChart, Rectangle) * @see #exportChartAsSVG(JFreeChart, Rectangle) * @see #exportChartAsSVG(JFreeChart, Rectangle, File) */ public static void exportChartAsPDF(final JFreeChart chart, final Rectangle bounds, final File f) throws FileNotFoundException, DocumentException { final int margin = 0; // page margins // Initialize writer final Document document = new Document(new com.itextpdf.text.Rectangle(bounds.width, bounds.height), margin, margin, margin, margin); final PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(f)); document.open(); final PdfContentByte cb = writer.getDirectContent(); final PdfTemplate tp = cb.createTemplate(bounds.width, bounds.height); // Draw the chart. Release resources upon completion final Graphics2D g2 = tp.createGraphics(bounds.width, bounds.height, new DefaultFontMapper()); chart.draw(g2, bounds); g2.dispose(); // Write to file cb.addTemplate(tp, 0, 0); document.close(); }
Example 3
Source File: ChartExportUtil.java From mzmine3 with GNU General Public License v2.0 | 5 votes |
/** * 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(); } }
Example 4
Source File: ChartExportUtil.java From old-mzmine3 with GNU General Public License v2.0 | 5 votes |
/** * 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(); } }
Example 5
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(); } } }
Example 6
Source File: ChartExportUtil.java From mzmine2 with GNU General Public License v2.0 | 5 votes |
/** * 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(); } }
Example 7
Source File: InsertPage.java From testarea-itext5 with GNU Affero General Public License v3.0 | 4 votes |
/** * <a href="http://stackoverflow.com/questions/28911509/how-to-retain-page-labels-when-concatenating-an-existing-pdf-with-a-pdf-created"> * How to retain page labels when concatenating an existing pdf with a pdf created from scratch? * </a> * <p> * A proposal how to implement the task using a {@link PdfStamper}. */ @Test public void testInsertTitlePage() throws IOException, DocumentException { try ( InputStream documentStream = getClass().getResourceAsStream("Labels.pdf"); InputStream titleStream = getClass().getResourceAsStream("Cover.pdf"); OutputStream outputStream = new FileOutputStream(new File(RESULT_FOLDER, "labels-with-cover-page.pdf")) ) { PdfReader titleReader = new PdfReader(titleStream); PdfReader reader = new PdfReader(documentStream); PdfStamper stamper = new PdfStamper(reader, outputStream); PdfImportedPage page = stamper.getImportedPage(titleReader, 1); stamper.insertPage(1, titleReader.getPageSize(1)); PdfContentByte content = stamper.getUnderContent(1); content.addTemplate(page, 0, 0); copyLinks(stamper, 1, titleReader, 1); PdfDictionary root = reader.getCatalog(); PdfDictionary labels = root.getAsDict(PdfName.PAGELABELS); if (labels != null) { PdfArray newNums = new PdfArray(); newNums.add(new PdfNumber(0)); PdfDictionary coverDict = new PdfDictionary(); coverDict.put(PdfName.P, new PdfString("Cover Page")); newNums.add(coverDict); PdfArray nums = labels.getAsArray(PdfName.NUMS); if (nums != null) { for (int i = 0; i < nums.size() - 1; ) { int n = nums.getAsNumber(i++).intValue(); newNums.add(new PdfNumber(n+1)); newNums.add(nums.getPdfObject(i++)); } } labels.put(PdfName.NUMS, newNums); stamper.markUsed(labels); } stamper.close(); } }
Example 8
Source File: ReportFactory.java From graylog-plugin-aggregates with GNU General Public License v3.0 | 2 votes |
@SuppressWarnings("deprecation") public static void writeChartsToPDF(List<JFreeChart> charts, int width, int height, OutputStream outputStream, String hostname, Date date) { PdfWriter writer = null; Document document = new Document(); try { writer = PdfWriter.getInstance(document, outputStream); writer.setPageEmpty(false); document.open(); document.setPageSize(PageSize.A4); document.add(new Paragraph("Aggregates Report generated by " + hostname + " on " + date.toString())); document.newPage(); writer.newPage(); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template; Graphics2D graphics2d; int position = 0; Rectangle2D rectangle2d; for (JFreeChart chart : charts){ LOG.debug("Writing chart to PDF"); if (writer.getVerticalPosition(true)-height+(height*position) < 0){ position = 0; document.newPage(); writer.newPage(); } template = contentByte.createTemplate(width, height); graphics2d = template.createGraphics(width, height, new DefaultFontMapper()); rectangle2d = new Rectangle2D.Double(0, 0, width, height); chart.draw(graphics2d, rectangle2d); graphics2d.dispose(); contentByte.addTemplate(template, 38, writer.getVerticalPosition(true)-height+(height*position)); position--; } } catch (Exception e) { e.printStackTrace(); } document.close(); }