Class PDF2Output


  • public class PDF2Output
    extends Output

    This class renders the graph to a PDF document defined using the Big Faceless PDF library, our companion product available from http://big.faceless.org/products/pdf. As you would expect, this is our recommended solution for rendering graphs to PDF documents. Here is an example which does just this, rendering a graph to a one-page PDF document:

       Graph g = makeMyGraph();     // Left as exercise for the reader.
    
       PDF p = new PDF();
       PDFPage page = p.newPage(PDF.PAGESIZE_A4);
       Output out = new PDFOutput(page, 100,100,400,400);
       out.render(g);
    
       p.render(new FileOutputStream("graph.pdf"));
     

    Since version 1.0.3, this class no longer requires the AWT classes to run. Rather than being tied to the java.awt.Font class, developers now reference the fonts by name. Recognised fonts are:

    • Helvetica
    • Times
    • Courier
    • Symbol
    • ZapfDingbats
    • STSong
    • MSung
    • MHei
    • HeiseiMin
    • HeiseiKakuGo
    • HYGoThic
    • HYSMyeongJo

    In addition, for backwards compatibility the names "monospaced", "serif" and "sans-serif" are mapped to Courier, Times and Helvetica respectively.

    Custom fonts can be set using the setFontMap method, which replaces the getFontMap method defined in 1.0.2 and earlier.

    • Field Summary

    • Constructor Summary

      Constructors 
      Constructor Description
      PDF2Output​(PDFPage p, float left, float top, float right, float bottom)
      Create a new PDFOutput class to render the graph to the specified page.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean canPattern()
      Return true if this subclass out Output can handle pattern fills.
      boolean canRotate()
      Returns true if this subclass can handle rotated text (it can, so it does)
      BoundingBox getCanvas()
      Return the canvas as a BoundingBox.
      Graph getGraph()
      Return the graph being rendered.
      BoundingBox getTextBox​(java.lang.String text, Style s)
      Return the bounding box for given string in the specified style.
      void line​(double x1, double y1, double x2, double y2)
      Draw a line from x1,y1 to x2,y2 on the canvas, in the current colour
      void poly​(java.lang.String name, double[] x, double[] y, int num)
      Draw and fill a polygon on the canvas, in the current colour.
      void render​(Graph gr)
      Render the Graph to this Output object.
      void setColor​(java.awt.Paint s)
      Set the current pen, font and fill colour
      void setFontMap​(java.lang.String name, int style, PDFFont font)
      Define a custom font for use with this output class.
      void setLineDash​(double on, double off, double phase)
      Set the line dash pattern
      void setLineThickness​(double thickness)
      Set the line thickness (1 being default, 2 twice as thick, etc.) on platforms that support it.
      void text​(java.lang.String text, double x, double y, Style s)
      Draw a line of text at the specified position in the specified style.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • PDF2Output

        public PDF2Output​(PDFPage p,
                          float left,
                          float top,
                          float right,
                          float bottom)

        Create a new PDFOutput class to render the graph to the specified page. This class assumes that the page has the default canvas settings - i.e. it's measured in points from the bottom left of the page.

        Parameters:
        p - a PDFpage object measured in points from the bottom-left
        left - the left margin of the graph, in points from the left of the page
        bottom - the bottom margin of the graph, in points from the bottom of the page
        width - the width of the graph in points
        height - the height of the graph in points
    • Method Detail

      • canRotate

        public final boolean canRotate()
        Returns true if this subclass can handle rotated text (it can, so it does)
        Specified by:
        canRotate in class Output
      • canPattern

        public final boolean canPattern()
        Description copied from class: Output
        Return true if this subclass out Output can handle pattern fills.
        Specified by:
        canPattern in class Output
      • render

        public void render​(Graph gr)
        Description copied from class: Output
        Render the Graph to this Output object.
        Specified by:
        render in class Output
      • getGraph

        public Graph getGraph()
        Description copied from class: Output
        Return the graph being rendered. For internal use.
        Specified by:
        getGraph in class Output
      • getCanvas

        public BoundingBox getCanvas()
        Description copied from class: Output
        Return the canvas as a BoundingBox. The final graph will be sized to fit this box.
        Specified by:
        getCanvas in class Output
      • setLineThickness

        public void setLineThickness​(double thickness)
        Description copied from class: Output
        Set the line thickness (1 being default, 2 twice as thick, etc.) on platforms that support it.
        Specified by:
        setLineThickness in class Output
      • setLineDash

        public void setLineDash​(double on,
                                double off,
                                double phase)
        Description copied from class: Output
        Set the line dash pattern
        Specified by:
        setLineDash in class Output
      • setColor

        public void setColor​(java.awt.Paint s)
        Description copied from class: Output
        Set the current pen, font and fill colour
        Specified by:
        setColor in class Output
      • line

        public void line​(double x1,
                         double y1,
                         double x2,
                         double y2)
        Description copied from class: Output
        Draw a line from x1,y1 to x2,y2 on the canvas, in the current colour
        Specified by:
        line in class Output
      • poly

        public void poly​(java.lang.String name,
                         double[] x,
                         double[] y,
                         int num)
        Description copied from class: Output
        Draw and fill a polygon on the canvas, in the current colour. The corners of the polygon are specified by the arrays of X and Y values. The polygon has no borders, and is filled with the current colour. Subclasses should handle the cases where several of the points are the same.
        Specified by:
        poly in class Output
        Parameters:
        name - The name of the dataset that is being drawn. Not used in the current release, but will eventually be user for interaction with rendered graphs
        x - the array of X co-ordinates
        y - the array of Y co-ordinates
        num - the number of co-ordinates in the array
      • setFontMap

        public void setFontMap​(java.lang.String name,
                               int style,
                               PDFFont font)
        Define a custom font for use with this output class. Here's an example:
         setFontMap("myfont", java.awt.Font.PLAIN, new OpenTypeFont("myfont.ttf", 1));
         
        Since:
        1.0.3
      • getTextBox

        public BoundingBox getTextBox​(java.lang.String text,
                                      Style s)
        Description copied from class: Output
        Return the bounding box for given string in the specified style.
        Specified by:
        getTextBox in class Output
        Parameters:
        text - The string to return the size for.
        s - The style to apply to the text before returning it's size. The aspects of the style that may affect this include font, rotation and alignment
      • text

        public void text​(java.lang.String text,
                         double x,
                         double y,
                         Style s)
        Description copied from class: Output
        Draw a line of text at the specified position in the specified style.
        Specified by:
        text in class Output
        Parameters:
        text - The line of text to draw.
        x - The x position to place the text at, measured from the left of the canvas.
        y - The y position to place the text at, measured from the bottom of the canvas.