Class PDFCanvas

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, ByteArrayResult, Canvas

    public class PDFCanvas
    extends AbstractCanvas
    implements ByteArrayResult
    Canvas for generating PDF files.

    The PDF generator currently only supports the Helvetica font.

    • Nested Class Summary

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int LAST_PAGE
      Add the QR bill on the last page of the PDF document.
      static int NEW_PAGE_AT_END
      Add the QR bill on a new page at the end of the PDF document.
    • Constructor Summary

      Constructors 
      Constructor Description
      PDFCanvas​(byte[] pdfDocument, int pageNo)
      Creates a new instance for adding the QR bill to an exiting PDF document.
      PDFCanvas​(double width, double height)
      Creates a new instance using the specified page size.
      PDFCanvas​(java.nio.file.Path path, int pageNo)
      Creates a new instance for adding the QR bill to an exiting PDF document.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addRectangle​(double x, double y, double width, double height)
      Adds a rectangle to the path
      void close()  
      void closeSubpath()
      Closes the current subpath
      void cubicCurveTo​(double x1, double y1, double x2, double y2, double x, double y)
      Adds a cubic Beziér curve to the open path going from the previous point to the specified position.
      void fillPath​(int color)
      Fills the current path and ends it
      void lineTo​(double x, double y)
      Adds a line segment to the open path from the previous point to the specified position.
      void moveTo​(double x, double y)
      Moves the current point of the open path to the specified position.
      void putText​(java.lang.String text, double x, double y, int fontSize, boolean isBold)
      Adds text to the graphics.
      void putTextLines​(java.lang.String[] lines, double x, double y, int fontSize, double leading)
      Adds several lines of text to the graphics.
      void saveAs​(java.nio.file.Path path)
      Saves the resulting PDF document to the specified path.
      void setTransformation​(double translateX, double translateY, double rotate, double scaleX, double scaleY)
      Sets a translation, rotation and scaling for the subsequent operations
      void startPath()
      Starts a path that can be filled or stroked
      void strokePath​(double strokeWidth, int color)
      Strokes the current path and ends it
      void strokePath​(double strokeWidth, int color, Canvas.LineStyle lineStyle)
      Strokes the current path and ends it
      byte[] toByteArray()
      Gets the resulting graphics as a byte array.
      void writeTo​(java.io.OutputStream os)
      Writes the resulting PDF document to the specified output stream.
      • Methods inherited from class java.lang.Object

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

      • LAST_PAGE

        public static final int LAST_PAGE
        Add the QR bill on the last page of the PDF document.
        See Also:
        Constant Field Values
      • NEW_PAGE_AT_END

        public static final int NEW_PAGE_AT_END
        Add the QR bill on a new page at the end of the PDF document.
        See Also:
        Constant Field Values
    • Constructor Detail

      • PDFCanvas

        public PDFCanvas​(double width,
                         double height)
                  throws java.io.IOException
        Creates a new instance using the specified page size.
        Parameters:
        width - page width, in mm
        height - page height, in mm
        Throws:
        java.io.IOException - thrown if the creation fails
      • PDFCanvas

        public PDFCanvas​(java.nio.file.Path path,
                         int pageNo)
                  throws java.io.IOException
        Creates a new instance for adding the QR bill to an exiting PDF document.

        The QR bill can either be added to an existing page by specifying the page number of an existing page (or LAST_PAGE), or it can be added to a new page at the end of the document (see NEW_PAGE_AT_END).

        The created instance assumes that the page for the QR bill has A4 format and will add the QR bill at the bottom of the page.

        Parameters:
        path - path to exiting document
        pageNo - the zero-based number of the page the QR bill should be added to
        Throws:
        java.io.IOException - thrown if the creation fails
      • PDFCanvas

        public PDFCanvas​(byte[] pdfDocument,
                         int pageNo)
                  throws java.io.IOException
        Creates a new instance for adding the QR bill to an exiting PDF document.

        The QR bill can either be added to an existing page by specifying the page number of an existing page (or LAST_PAGE), or it can be added to a new page at the end of the document (see NEW_PAGE_AT_END).

        The created instance assumes that the page for the QR bill has A4 format and will add the QR bill at the bottom of the page.

        Parameters:
        pdfDocument - binary array contianing PDF document
        pageNo - the zero-based number of the page the QR bill should be added to
        Throws:
        java.io.IOException - thrown if the creation fails
    • Method Detail

      • setTransformation

        public void setTransformation​(double translateX,
                                      double translateY,
                                      double rotate,
                                      double scaleX,
                                      double scaleY)
                               throws java.io.IOException
        Description copied from interface: Canvas
        Sets a translation, rotation and scaling for the subsequent operations

        Before a new translation is applied, the coordinate system is reset to it's original state.

        The transformations are applied in the order translation, rotation, scaling.

        Specified by:
        setTransformation in interface Canvas
        Parameters:
        translateX - translation in x direction (in mm)
        translateY - translation in y direction (in mm)
        rotate - rotation angle, in radians
        scaleX - scale factor in x direction (1.0 = no scaling)
        scaleY - scale factor in y direction (1.0 = no scaling)
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • putText

        public void putText​(java.lang.String text,
                            double x,
                            double y,
                            int fontSize,
                            boolean isBold)
                     throws java.io.IOException
        Description copied from interface: Canvas
        Adds text to the graphics.

        The text position refers to the left most point on the text's baseline.

        Specified by:
        putText in interface Canvas
        Parameters:
        text - the text
        x - x position of the text's start (in mm)
        y - y position of the text's top (in mm)
        fontSize - the font size (in pt)
        isBold - indicates if the text is in bold or regular weight
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • putTextLines

        public void putTextLines​(java.lang.String[] lines,
                                 double x,
                                 double y,
                                 int fontSize,
                                 double leading)
                          throws java.io.IOException
        Description copied from interface: Canvas
        Adds several lines of text to the graphics.

        The text position refers to the left most point on the baseline of the first text line. Additional lines then follow below.

        Specified by:
        putTextLines in interface Canvas
        Overrides:
        putTextLines in class AbstractCanvas
        Parameters:
        lines - the text lines
        x - x position of the text's start (in mm)
        y - y position of the text's top (in mm)
        fontSize - the font size (in pt)
        leading - additional vertical space between text lines (in mm)
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • startPath

        public void startPath()
        Description copied from interface: Canvas
        Starts a path that can be filled or stroked
        Specified by:
        startPath in interface Canvas
      • moveTo

        public void moveTo​(double x,
                           double y)
                    throws java.io.IOException
        Description copied from interface: Canvas
        Moves the current point of the open path to the specified position.
        Specified by:
        moveTo in interface Canvas
        Parameters:
        x - x-coordinate of position
        y - y-coordinate of position
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • lineTo

        public void lineTo​(double x,
                           double y)
                    throws java.io.IOException
        Description copied from interface: Canvas
        Adds a line segment to the open path from the previous point to the specified position.
        Specified by:
        lineTo in interface Canvas
        Parameters:
        x - x-coordinate of position
        y - y-coordinate of position
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • cubicCurveTo

        public void cubicCurveTo​(double x1,
                                 double y1,
                                 double x2,
                                 double y2,
                                 double x,
                                 double y)
                          throws java.io.IOException
        Description copied from interface: Canvas
        Adds a cubic Beziér curve to the open path going from the previous point to the specified position. Two control points control the curve
        Specified by:
        cubicCurveTo in interface Canvas
        Parameters:
        x1 - x-coordinate of first control point
        y1 - y-coordinate of first control point
        x2 - x-coordinate of second control point
        y2 - y-coordinate of second control point
        x - x-coordinate of position
        y - y-coordinate of position
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • addRectangle

        public void addRectangle​(double x,
                                 double y,
                                 double width,
                                 double height)
                          throws java.io.IOException
        Description copied from interface: Canvas
        Adds a rectangle to the path
        Specified by:
        addRectangle in interface Canvas
        Parameters:
        x - the rectangle's left position (in mm)
        y - the rectangle's top position (in mm)
        width - the rectangle's width (in mm)
        height - rectangle's height (in mm)
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • closeSubpath

        public void closeSubpath()
                          throws java.io.IOException
        Description copied from interface: Canvas
        Closes the current subpath
        Specified by:
        closeSubpath in interface Canvas
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • fillPath

        public void fillPath​(int color)
                      throws java.io.IOException
        Description copied from interface: Canvas
        Fills the current path and ends it
        Specified by:
        fillPath in interface Canvas
        Parameters:
        color - the fill color (expressed similar to HTML, e.g. 0xffffff for white)
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • strokePath

        public void strokePath​(double strokeWidth,
                               int color)
                        throws java.io.IOException
        Description copied from interface: Canvas
        Strokes the current path and ends it

        The path is stroked with a solid line.

        Specified by:
        strokePath in interface Canvas
        Parameters:
        strokeWidth - the stroke width (in pt)
        color - the stroke color (expressed similar to HTML, e.g. 0xffffff for white)
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • strokePath

        public void strokePath​(double strokeWidth,
                               int color,
                               Canvas.LineStyle lineStyle)
                        throws java.io.IOException
        Description copied from interface: Canvas
        Strokes the current path and ends it
        Specified by:
        strokePath in interface Canvas
        Parameters:
        strokeWidth - the stroke width (in pt)
        color - the stroke color (expressed similar to HTML, e.g. 0xffffff for white)
        lineStyle - the line style
        Throws:
        java.io.IOException - thrown if the graphics cannot be generated
      • toByteArray

        public byte[] toByteArray()
                           throws java.io.IOException
        Description copied from interface: ByteArrayResult
        Gets the resulting graphics as a byte array.
        Specified by:
        toByteArray in interface ByteArrayResult
        Returns:
        the byte array
        Throws:
        java.io.IOException - thrown if the construction of the byte array fails
      • writeTo

        public void writeTo​(java.io.OutputStream os)
                     throws java.io.IOException
        Writes the resulting PDF document to the specified output stream.
        Parameters:
        os - the output stream
        Throws:
        java.io.IOException - thrown if the image cannot be written
      • saveAs

        public void saveAs​(java.nio.file.Path path)
                    throws java.io.IOException
        Saves the resulting PDF document to the specified path.
        Parameters:
        path - the path to write to
        Throws:
        java.io.IOException - thrown if the image cannot be written
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException