LISTSERV mailing list manager LISTSERV 16.5

Help for HPS-SVN Archives


HPS-SVN Archives

HPS-SVN Archives


HPS-SVN@LISTSERV.SLAC.STANFORD.EDU


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

HPS-SVN Home

HPS-SVN Home

HPS-SVN  March 2015

HPS-SVN March 2015

Subject:

r2604 - /java/trunk/monitoring-util/src/main/java/org/hps/monitoring/plotting/ExportPdf.java

From:

[log in to unmask]

Reply-To:

Notification of commits to the hps svn repository <[log in to unmask]>

Date:

Sat, 28 Mar 2015 01:08:59 -0000

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (133 lines)

Author: [log in to unmask]
Date: Fri Mar 27 18:08:54 2015
New Revision: 2604

Log:
Add utilities for exporting plots to PDF file.

Added:
    java/trunk/monitoring-util/src/main/java/org/hps/monitoring/plotting/ExportPdf.java

Added: java/trunk/monitoring-util/src/main/java/org/hps/monitoring/plotting/ExportPdf.java
 =============================================================================
--- java/trunk/monitoring-util/src/main/java/org/hps/monitoring/plotting/ExportPdf.java	(added)
+++ java/trunk/monitoring-util/src/main/java/org/hps/monitoring/plotting/ExportPdf.java	Fri Mar 27 18:08:54 2015
@@ -0,0 +1,117 @@
+package org.hps.monitoring.plotting;
+
+import hep.aida.IPlotter;
+import hep.aida.jfree.plotter.Plotter;
+
+import java.awt.Component;
+import java.awt.Image;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import javax.swing.JPanel;
+
+import org.apache.pdfbox.exceptions.COSVisitorException;
+import org.apache.pdfbox.util.PDFMergerUtility;
+
+import com.itextpdf.text.BadElementException;
+import com.itextpdf.text.Document;
+import com.itextpdf.text.DocumentException;
+import com.itextpdf.text.PageSize;
+import com.itextpdf.text.pdf.PdfWriter;
+
+/**
+ * This is a class for exporting plot graphics to PDF.
+ * 
+ * @author Jeremy McCormick <[log in to unmask]>
+ *
+ */
+public class ExportPdf {
+
+    private ExportPdf() {
+    }
+    
+    /**
+     * Export a JPanel to a single PDF.
+     * @param panel The JPanel.
+     * @param name The output file name.
+     * @throws IOException If there is a problem opening the PDF document.
+     */
+    static void write(JPanel panel, String name) throws IOException {
+        
+        Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);        
+        PdfWriter writer;
+        try {
+            writer = PdfWriter.getInstance(document, new FileOutputStream(name));
+        } catch (DocumentException e) {
+            throw new IOException(e);
+        }
+        document.open();        
+        
+        Image awtImage = getImageFromPanel(panel);
+        
+        com.itextpdf.text.Image iTextImage = null;
+        try {
+            iTextImage = com.itextpdf.text.Image.getInstance(writer, awtImage, 1f);
+        } catch (BadElementException e) {
+            throw new IOException(e);
+        }        
+         
+        iTextImage.setAbsolutePosition(50, 50);
+        iTextImage.scalePercent(60);
+        try {
+            document.add(iTextImage);
+        } catch (DocumentException e) {
+            throw new IOException(e);
+        }
+        
+        document.close();
+    }
+    
+    /**
+     * Get an image from a Swing component.
+     * @param component The Swing component.
+     * @return The image from the component.
+     */
+    static java.awt.Image getImageFromPanel(Component component) {
+        BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB);
+        component.paint(image.getGraphics());
+        return image;
+    }
+        
+    /**
+     * Write a single PDF containing the graphics from the collection of plotters,
+     * with one plotter per page.
+     * 
+     * @param plotters The collection of plotters.
+     * @param fileName The name of the output file.
+     * @throws IOException If there is a problem merging all the plotter PDFs together.
+     */
+    public static void write(Collection<IPlotter> plotters, String fileName) throws IOException {                
+        
+        PDFMergerUtility merge = new PDFMergerUtility();
+        merge.setDestinationFileName(fileName);
+        
+        List<File> tempFiles = new ArrayList<File>();
+        for (IPlotter plotter : plotters) {
+            File tempFile = File.createTempFile("plot", ".pdf");
+            tempFiles.add(tempFile);
+            write(((Plotter)plotter).panel(), tempFile.getPath());
+            merge.addSource(tempFile);
+        }
+        
+        try {
+            merge.mergeDocuments();
+        } catch (COSVisitorException e) {
+            throw new IOException("Error merging documents", e);
+        }
+        
+        for (File tempFile : tempFiles) {
+            tempFile.delete();
+        }
+    }
+}

Top of Message | Previous Page | Permalink

Advanced Options


Options

Log In

Log In

Get Password

Get Password


Search Archives

Search Archives


Subscribe or Unsubscribe

Subscribe or Unsubscribe


Archives

November 2017
August 2017
July 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013

ATOM RSS1 RSS2



LISTSERV.SLAC.STANFORD.EDU

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager

Privacy Notice, Security Notice and Terms of Use