jspdf text position
- Published
- Reading time
- 1 min read
- Length
- 248 words
On this page
JSPDF Text Position#
When creating a PDF document using JSPDF, you may want to position the text at a specific location on the page. To do this, you can use the text() method of the JSPDF object.
Using Absolute Positioning#
To position the text at an absolute position on the page, you can use the text() method with the x and y coordinates as arguments. For example:
var doc = new jsPDF();
doc.text(20, 20, 'Hello World!');This will position the text "Hello World!" at coordinates (20, 20) on the page.
Using Relative Positioning#
You can also position the text relative to its current position using the text() method with the options object as an argument. The options object can contain properties such as align, baseline, and angle. For example:
var doc = new jsPDF();
doc.text('Hello World!', 50, 50, {
align: 'center',
baseline: 'middle',
angle: 45
});This will position the text "Hello World!" at coordinates (50, 50), with the text centered horizontally and vertically, and rotated 45 degrees.
Using a Rectangular Area#
You can also position the text within a rectangular area using the text() method with the options object as an argument. The options object can contain properties such as maxWidth and maxLines. For example:
var doc = new jsPDF();
doc.text('Hello World!', 20, 20, {
maxWidth: 50,
maxLines: 2
});This will position the text "Hello World!" within a rectangular area starting at coordinates (20, 20) with a maximum width of 50 and a maximum of 2 lines.