Class PDFCanvas
- All Implemented Interfaces:
Closeable
,AutoCloseable
,ByteArrayResult
,Canvas
The PDF generator currently only supports the Helvetica font.
-
Nested Class Summary
Nested classes/interfaces inherited from interface net.codecrete.qrbill.canvas.Canvas
Canvas.LineStyle
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Add the QR bill on the last page of the PDF document.static final int
Add the QR bill on a new page at the end of the PDF document.Fields inherited from class net.codecrete.qrbill.canvas.AbstractCanvas
fontMetrics, MM_TO_PT
-
Constructor Summary
ConstructorDescriptionPDFCanvas
(byte[] pdfDocument, int pageNo) Creates a new instance for adding the QR bill to an existing PDF document.PDFCanvas
(byte[] pdfDocument, int pageNo, PDFFontSettings fontSettings) Creates a new instance for adding the QR bill to an existing PDF document.PDFCanvas
(double width, double height) Creates a new instance using the specified page size.PDFCanvas
(double width, double height, PDFFontSettings fontSettings) Creates a new instance using the specified page size and font.Creates a new instance for adding the QR bill to an existing PDF document.PDFCanvas
(Path path, int pageNo, PDFFontSettings fontSettings) Creates a new instance for adding the QR bill to an existing PDF document.PDFCanvas
(org.apache.pdfbox.pdmodel.PDDocument pdfDocument, int pageNo) Creates a new instance for adding the QR bill to the specified PDF document.PDFCanvas
(org.apache.pdfbox.pdmodel.PDDocument pdfDocument, int pageNo, PDFFontSettings fontSettings) Creates a new instance for adding the QR bill to the specified PDF document.PDFCanvas
(org.apache.pdfbox.pdmodel.PDPageContentStream contentStream) Creates a new instance for adding a QR bill to the specified PDF page content stream. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addRectangle
(double x, double y, double width, double height) Adds a rectangle to the pathvoid
close()
void
Closes the current subpathvoid
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, boolean smoothing) Fills the current path and ends itvoid
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
Adds text to the graphics.void
putTextLines
(String[] lines, double x, double y, int fontSize, double leading) Adds several lines of text to the graphics.void
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 operationsvoid
Starts a path that can be filled or strokedvoid
strokePath
(double strokeWidth, int color, Canvas.LineStyle lineStyle, boolean smoothing) Strokes the current path and ends itbyte[]
Gets the resulting graphics as a byte array.void
writeTo
(OutputStream os) Writes the resulting PDF document to the specified output stream.Methods inherited from class net.codecrete.qrbill.canvas.AbstractCanvas
getAscender, getDescender, getLineHeight, getTextWidth, setupFontMetrics, splitLines
-
Field Details
-
LAST_PAGE
public static final int LAST_PAGEAdd the QR bill on the last page of the PDF document.- See Also:
-
NEW_PAGE_AT_END
public static final int NEW_PAGE_AT_ENDAdd the QR bill on a new page at the end of the PDF document.- See Also:
-
-
Constructor Details
-
PDFCanvas
Creates a new instance using the specified page size.A new PDF file with a single page will be created. It can later be retrieved as a byte array (see
toByteArray()
) or written to an output stream (seewriteTo(OutputStream)
).Call
QRBill.draw(Bill, Canvas)
to draw the QR bill to this canvas. It will be drawn at the origin of the page, i.e. the bottom left corner of the bill will be in the bottom left corner of the page.For text, the PDF standard font Helvetica will be used. It does not need to be embedded into the file and is available on all PDF viewers. But it is restricted to the WinANSI character set.
- Parameters:
width
- page width, in mmheight
- page height, in mm- Throws:
IOException
- thrown if the creation fails
-
PDFCanvas
Creates a new instance using the specified page size and font.A new PDF file with a single page will be created. It can later be retrieved as a byte array (see
toByteArray()
) or written to an output stream (seewriteTo(OutputStream)
).Call
QRBill.draw(Bill, Canvas)
to draw the QR bill to this canvas. It will be drawn at the origin of the page, i.e. the bottom left corner of the bill will be in the bottom left corner of the page.Font settings specify what font to use and whether to embed the font in the PDF file.
- Parameters:
width
- page width, in mmheight
- page height, in mmfontSettings
- font settings- Throws:
IOException
- thrown if the creation fails
-
PDFCanvas
Creates a new instance for adding the QR bill to an existing 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 (seeNEW_PAGE_AT_END
). If a new page is added, it will have A4 portrait format.Call
QRBill.draw(Bill, Canvas)
to draw the QR bill to this canvas. It will be drawn at the origin of the page, i.e. the bottom left corner of the bill will be in the bottom left corner of the page.The new PDF file can later be retrieved as a byte array (see
toByteArray()
) or written to an output stream (seewriteTo(OutputStream)
).For text, the PDF standard font Helvetica will be used. It does not need to be embedded into the file and is available on all PDF viewers. But it is restricted to the WinANSI character set.
- Parameters:
path
- path to existing PDF documentpageNo
- the zero-based number of the page the QR bill should be added to- Throws:
IOException
- thrown if the creation fails
-
PDFCanvas
Creates a new instance for adding the QR bill to an existing 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 (seeNEW_PAGE_AT_END
). If a new page is added, it will have A4 portrait format.Call
QRBill.draw(Bill, Canvas)
to draw the QR bill to this canvas. It will be drawn at the origin of the page, i.e. the bottom left corner of the bill will be in the bottom left corner of the page.The new PDF file can later be retrieved as a byte array (see
toByteArray()
) or written to an output stream (seewriteTo(OutputStream)
).Font settings specify what font to use and whether to embed the font in the PDF file.
- Parameters:
path
- path to existing PDF documentpageNo
- the zero-based number of the page the QR bill should be added tofontSettings
- font settings- Throws:
IOException
- thrown if the creation fails
-
PDFCanvas
Creates a new instance for adding the QR bill to an existing 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 (seeNEW_PAGE_AT_END
). If a new page is added, it will have A4 portrait format.Call
QRBill.draw(Bill, Canvas)
to draw the QR bill to this canvas. It will be drawn at the origin of the page, i.e. the bottom left corner of the bill will be in the bottom left corner of the page.The new PDF file can later be retrieved as a byte array (see
toByteArray()
) or written to an output stream (seewriteTo(OutputStream)
).For text, the PDF standard font Helvetica will be used. It does not need to be embedded into the file and is available on all PDF viewers. But it is restricted to the WinANSI character set.
- Parameters:
pdfDocument
- binary array containing PDF documentpageNo
- the zero-based number of the page the QR bill should be added to- Throws:
IOException
- thrown if the creation fails
-
PDFCanvas
Creates a new instance for adding the QR bill to an existing 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 (seeNEW_PAGE_AT_END
). If a new page is added, it will have A4 portrait format.Call
QRBill.draw(Bill, Canvas)
to draw the QR bill to this canvas. It will be drawn at the origin of the page, i.e. the bottom left corner of the bill will be in the bottom left corner of the page.The new PDF file can later be retrieved as a byte array (see
toByteArray()
) or written to an output stream (seewriteTo(OutputStream)
).Font settings specify what font to use and whether to embed the font in the PDF file.
- Parameters:
pdfDocument
- binary array containing PDF documentpageNo
- the zero-based number of the page the QR bill should be added tofontSettings
- font settings- Throws:
IOException
- thrown if the creation fails
-
PDFCanvas
Creates a new instance for adding the QR bill to the specified 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 (seeNEW_PAGE_AT_END
). If a new page is added, it will have A4 portrait format.Call
QRBill.draw(Bill, Canvas)
to draw the QR bill to this canvas. It will be drawn at the origin of the page, i.e. the bottom left corner of the bill will be in the bottom left corner of the page.The PDF document must have been opened with the appropriate PDFBox method, and it must be saved with a PDFBox method. Before saving it, this instance must be closed (see
close()
). The instance methodstoByteArray()
andwriteTo(OutputStream)
may not be used and will throw an exception.For text, the PDF standard font Helvetica will be used. It does not need to be embedded into the file and is available on all PDF viewers. But it is restricted to the WinANSI character set.
- Parameters:
pdfDocument
- PDF documentpageNo
- the zero-based number of the page the QR bill should be added to- Throws:
IOException
- thrown if the creation fails
-
PDFCanvas
public PDFCanvas(org.apache.pdfbox.pdmodel.PDDocument pdfDocument, int pageNo, PDFFontSettings fontSettings) throws IOException Creates a new instance for adding the QR bill to the specified 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 (seeNEW_PAGE_AT_END
). If a new page is added, it will have A4 portrait format.Call
QRBill.draw(Bill, Canvas)
to draw the QR bill to this canvas. It will be drawn at the origin of the page, i.e. the bottom left corner of the bill will be in the bottom left corner of the page.The PDF document must have been opened with the appropriate PDFBox method, and it must be saved with a PDFBox method. Before saving it, this instance must be closed (see
close()
). The instance methodstoByteArray()
andwriteTo(OutputStream)
may not be used and will throw an exception.Font settings specify what font to use and whether to embed the font in the PDF file.
- Parameters:
pdfDocument
- PDF documentpageNo
- the zero-based number of the page the QR bill should be added tofontSettings
- font settings- Throws:
IOException
- thrown if the creation fails
-
PDFCanvas
public PDFCanvas(org.apache.pdfbox.pdmodel.PDPageContentStream contentStream) Creates a new instance for adding a QR bill to the specified PDF page content stream.Call
QRBill.draw(Bill, Canvas)
to draw the QR bill to this canvas. It will be drawn at the current origin of the page, i.e. the bottom left corner of the bill will be at the origin. The current transformation matrix (CTM) is also applied.The PDF document and PDF content stream must have been opened with the appropriate PDFBox method. Do not access the PDF document or page content stream until this instance has been closed. (see
close()
). Closing it will also reset the graphics state to the state before creating this instance.For text, the PDF standard font Helvetica will be used. It does not need to be embedded into the file and is available on all PDF viewers. But it is restricted to the WinANSI character set.
- Parameters:
contentStream
- PDF page content stream
-
-
Method Details
-
setTransformation
public void setTransformation(double translateX, double translateY, double rotate, double scaleX, double scaleY) throws IOException Description copied from interface:Canvas
Sets a translation, rotation and scaling for the subsequent operationsBefore 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 interfaceCanvas
- Parameters:
translateX
- translation in x direction (in mm)translateY
- translation in y direction (in mm)rotate
- rotation angle, in radiansscaleX
- scale factor in x direction (1.0 = no scaling)scaleY
- scale factor in y direction (1.0 = no scaling)- Throws:
IOException
- thrown if the graphics cannot be generated
-
putText
public void putText(String text, double x, double y, int fontSize, boolean isBold) throws 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 interfaceCanvas
- Parameters:
text
- the textx
- 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:
IOException
- thrown if the graphics cannot be generated
-
putTextLines
public void putTextLines(String[] lines, double x, double y, int fontSize, double leading) throws 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 interfaceCanvas
- Overrides:
putTextLines
in classAbstractCanvas
- Parameters:
lines
- the text linesx
- 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:
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 -
moveTo
Description copied from interface:Canvas
Moves the current point of the open path to the specified position.- Specified by:
moveTo
in interfaceCanvas
- Parameters:
x
- x-coordinate of positiony
- y-coordinate of position- Throws:
IOException
- thrown if the graphics cannot be generated
-
lineTo
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 interfaceCanvas
- Parameters:
x
- x-coordinate of positiony
- y-coordinate of position- Throws:
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 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 interfaceCanvas
- Parameters:
x1
- x-coordinate of first control pointy1
- y-coordinate of first control pointx2
- x-coordinate of second control pointy2
- y-coordinate of second control pointx
- x-coordinate of positiony
- y-coordinate of position- Throws:
IOException
- thrown if the graphics cannot be generated
-
addRectangle
Description copied from interface:Canvas
Adds a rectangle to the path- Specified by:
addRectangle
in interfaceCanvas
- 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:
IOException
- thrown if the graphics cannot be generated
-
closeSubpath
Description copied from interface:Canvas
Closes the current subpath- Specified by:
closeSubpath
in interfaceCanvas
- Throws:
IOException
- thrown if the graphics cannot be generated
-
fillPath
Description copied from interface:Canvas
Fills the current path and ends it- Specified by:
fillPath
in interfaceCanvas
- Parameters:
color
- the fill color (expressed similar to HTML, e.g. 0xffffff for white)smoothing
-true
for using smoothing techniques such as antialiasing,false
otherwise- Throws:
IOException
- thrown if the graphics cannot be generated
-
strokePath
public void strokePath(double strokeWidth, int color, Canvas.LineStyle lineStyle, boolean smoothing) throws IOException Description copied from interface:Canvas
Strokes the current path and ends it- Specified by:
strokePath
in interfaceCanvas
- Parameters:
strokeWidth
- the stroke width (in pt)color
- the stroke color (expressed similar to HTML, e.g. 0xffffff for white)lineStyle
- the line stylesmoothing
-true
for using smoothing techniques such as antialiasing,false
otherwise- Throws:
IOException
- thrown if the graphics cannot be generated
-
toByteArray
Gets the resulting graphics as a byte array.This method may only be called if the PDF document has been opened or created by this instance. It may not be called if this instance was created by
PDFCanvas(PDDocument, int)
orPDFCanvas(PDPageContentStream)
.- Specified by:
toByteArray
in interfaceByteArrayResult
- Returns:
- the byte array
- Throws:
IOException
- thrown if the construction of the byte array fails
-
writeTo
Writes the resulting PDF document to the specified output stream.This method may only be called if the PDF document has been opened or created by this instance. It may not be called if this instance was created by
PDFCanvas(PDDocument, int)
orPDFCanvas(PDPageContentStream)
.- Parameters:
os
- the output stream- Throws:
IOException
- thrown if the image cannot be written
-
saveAs
Saves the resulting PDF document to the specified path.This method may only be called if the PDF document has been opened or created by this instance. It may not be called if this instance was created by
PDFCanvas(PDDocument, int)
orPDFCanvas(PDPageContentStream)
.- Parameters:
path
- the path to write to- Throws:
IOException
- thrown if the image cannot be written
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-