diff --git a/src/modules/annotations.js b/src/modules/annotations.js index a9207b667..c65a774fd 100644 --- a/src/modules/annotations.js +++ b/src/modules/annotations.js @@ -363,6 +363,13 @@ import { jsPDF } from "../jspdf.js"; //TODO We really need the text baseline height to do this correctly. // Or ability to draw text on top, bottom, center, or baseline. y += height * 0.2; + //handle x position based on the align option + if (options.align === "center") { + x = x - width / 2; //since starting from center move the x position by half of text width + } + if (options.align === "right") { + x = x - width; + } this.link(x, y - height, width, height, options); return width; }; diff --git a/test/reference/textLinkWithAlignOptions.pdf b/test/reference/textLinkWithAlignOptions.pdf new file mode 100644 index 000000000..abd1be73e Binary files /dev/null and b/test/reference/textLinkWithAlignOptions.pdf differ diff --git a/test/specs/annotations.spec.js b/test/specs/annotations.spec.js index b13f52493..e5e364ebd 100644 --- a/test/specs/annotations.spec.js +++ b/test/specs/annotations.spec.js @@ -63,7 +63,7 @@ describe("Module: Annotations", () => { }); doc.textWithLink("Click me!", 10, 10, { - url: "https://parall.ax/", + url: "https://parall.ax/" }); doc.addPage("a4"); @@ -72,4 +72,38 @@ describe("Module: Annotations", () => { comparePdf(doc.output(), "insertLinkAddPage.pdf", "annotations"); }); + it("should align text link based on the align option", () => { + var doc = new jsPDF({ + unit: "px", + format: [200, 300], + floatPrecision: 2 + }); + + doc.textWithLink( + "Left aligned Link", + doc.internal.pageSize.getWidth() / 2, + 10, + { align: "left", url: "https://www.google.com" } + ); + doc.textWithLink( + "Center aligned Link", + doc.internal.pageSize.getWidth() / 2, + 30, + { align: "center", url: "https://www.google.com" } + ); + doc.textWithLink( + "Justify aligned Link", + doc.internal.pageSize.getWidth() / 2, + 50, + { align: "justify", url: "https://www.google.com" } + ); + doc.textWithLink( + "Right aligned Link", + doc.internal.pageSize.getWidth() / 2, + 70, + { align: "right", url: "https://www.google.com" } + ); + + comparePdf(doc.output(), "textLinkWithAlignOptions.pdf", "annotations"); + }); });