DrawText and DrawImage Compensating Page Rotation
See original GitHub issueHi, I’m trying to add text and images to different pages that may or may not be rotated. I want to be able to define a universal position (top right, top left, etc) and automatically compensate for the page orientation.
I tried the solution offered by @kevinswartz in #65 but it doesn’t seem to work in my case:
For drawText I tried the following:
const page = pdfDoc.getPage(i);
const dimensions = page.getSize();
const pageRotation = page.getRotation();
const topRightPosition = {
x: dimensions.width - 100,
y: dimensions.height - 40
}
const textLocation = compensateRotation(pageRotation, topRightPosition.x, topRightPosition.y, 1, dimensions, 32);
imagePage.drawText(customOptions.label, {
x: textLocation.x,
y: textLocation.y,
size: 32,
rotate: pageRotation, // Tried with and without this
});
// ======== kevinswartz's code =========
const compensateRotation = (pageRotation, x, y, scale, dimensions, fontSize) => {
let rotationRads = (pageRotation.angle * Math.PI) / 180;
let coordsFromBottomLeft = {
x: x / scale,
};
if (pageRotation.angle === 90 || pageRotation.angle === 270) {
coordsFromBottomLeft.y = dimensions.width - (y + fontSize) / scale;
} else {
coordsFromBottomLeft.y = dimensions.height - (y + fontSize) / scale;
}
let drawX = null;
let drawY = null;
if (pageRotation.angle === 90) {
drawX =
coordsFromBottomLeft.x * Math.cos(rotationRads) -
coordsFromBottomLeft.y * Math.sin(rotationRads) +
dimensions.width;
drawY =
coordsFromBottomLeft.x * Math.sin(rotationRads) +
coordsFromBottomLeft.y * Math.cos(rotationRads);
} else if (pageRotation.angle === 180) {
drawX =
coordsFromBottomLeft.x * Math.cos(rotationRads) -
coordsFromBottomLeft.y * Math.sin(rotationRads) +
dimensions.width;
drawY =
coordsFromBottomLeft.x * Math.sin(rotationRads) +
coordsFromBottomLeft.y * Math.cos(rotationRads) +
dimensions.height;
} else if (pageRotation.angle === 270) {
drawX =
coordsFromBottomLeft.x * Math.cos(rotationRads) -
coordsFromBottomLeft.y * Math.sin(rotationRads);
drawY =
coordsFromBottomLeft.x * Math.sin(rotationRads) +
coordsFromBottomLeft.y * Math.cos(rotationRads) +
dimensions.height;
} else {
//no rotation
drawX = coordsFromBottomLeft.x;
drawY = coordsFromBottomLeft.y;
}
return { x: drawX, y: drawY };
};
I’ve also seen the use of low-level operators (pushOperators, translate, etc) and I’m wondering if there is an easier way to accomplish this?
Any advice would be very much appreciated.
Thanks.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:8 (1 by maintainers)
Top Results From Across the Web
How to draw and rotate image along with text in HTML5 canvas
1 Answer 1 · context.translate to set the rotation point to the center of the wheel. · context.rotate to rotate each blade to...
Read more >ASP PDF User Manual Chapter 6: Text and Fonts - AspPDF
Angle: specifies the angle (in degrees) by which the text should be rotated counter-clockwise around the upper-left corner of the bounding box. This...
Read more >Draw directly in the model Coordinates - FlexSim Community
I am trying to draw in the model coordinates without using On draw trigger ... size.z*0.5, size.y*0.5); // compensate for centroid rotation ......
Read more >draw_text_ext_transformed_colour
This will draw the given text with a blue to dark blue downward gradient, at position (200,200) in the room, rotated to be...
Read more >Drawing on Canvas - Eloquent JavaScript
It is even possible to rotate or skew nodes with the transform style. But we'd be using the DOM for something ... We...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@considine have you found the solution for drawImage on rotated Pages?
@considine have you ?
@considine - Not really, I ended up using a different library (ImageMagick) to convert the page into a rotated image and then changed the dimensions of the page to match that of the rotated image. This creates a page that ‘looks’ rotated but in reality, it is not. You can then place your text or whatever on top of the page and the coordinates will work ok.
The HUGE disadvantage of this approach (besides the extra processing and extra libraries) is that you may end up with pages with a lot lower resolution than the original ones. You’ll have to tweak the ImageMagick conversion method to match your ideal resolution.