Skip to content

Commit 451e131

Browse files
fix textWithLink alignment (#3026)
fix #2938: textWithLink Co-authored-by: Lukas Hollaender <[email protected]>
1 parent c0f6c54 commit 451e131

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/modules/annotations.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,13 @@ import { jsPDF } from "../jspdf.js";
363363
//TODO We really need the text baseline height to do this correctly.
364364
// Or ability to draw text on top, bottom, center, or baseline.
365365
y += height * 0.2;
366+
//handle x position based on the align option
367+
if (options.align === "center") {
368+
x = x - width / 2; //since starting from center move the x position by half of text width
369+
}
370+
if (options.align === "right") {
371+
x = x - width;
372+
}
366373
this.link(x, y - height, width, height, options);
367374
return width;
368375
};
3.73 KB
Binary file not shown.

test/specs/annotations.spec.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe("Module: Annotations", () => {
6363
});
6464

6565
doc.textWithLink("Click me!", 10, 10, {
66-
url: "https://parall.ax/",
66+
url: "https://parall.ax/"
6767
});
6868

6969
doc.addPage("a4");
@@ -72,4 +72,38 @@ describe("Module: Annotations", () => {
7272

7373
comparePdf(doc.output(), "insertLinkAddPage.pdf", "annotations");
7474
});
75+
it("should align text link based on the align option", () => {
76+
var doc = new jsPDF({
77+
unit: "px",
78+
format: [200, 300],
79+
floatPrecision: 2
80+
});
81+
82+
doc.textWithLink(
83+
"Left aligned Link",
84+
doc.internal.pageSize.getWidth() / 2,
85+
10,
86+
{ align: "left", url: "https://www.google.com" }
87+
);
88+
doc.textWithLink(
89+
"Center aligned Link",
90+
doc.internal.pageSize.getWidth() / 2,
91+
30,
92+
{ align: "center", url: "https://www.google.com" }
93+
);
94+
doc.textWithLink(
95+
"Justify aligned Link",
96+
doc.internal.pageSize.getWidth() / 2,
97+
50,
98+
{ align: "justify", url: "https://www.google.com" }
99+
);
100+
doc.textWithLink(
101+
"Right aligned Link",
102+
doc.internal.pageSize.getWidth() / 2,
103+
70,
104+
{ align: "right", url: "https://www.google.com" }
105+
);
106+
107+
comparePdf(doc.output(), "textLinkWithAlignOptions.pdf", "annotations");
108+
});
75109
});

0 commit comments

Comments
 (0)