RED-6012 - fixed the text alignment based on its position and orientation

This commit is contained in:
Valentin Mihai 2023-06-09 14:01:04 +02:00
parent cf61b271b2
commit 1505dac3ac

View File

@ -8,6 +8,24 @@ import {
WatermarkVerticalAlignment,
} from '@red/domain';
const HORIZONTAL_TEXT_ALIGNMENT_MATRIX = [
[-1, 0, 1],
[-1, 0, 1],
[-1, 0, 1],
];
const VERTICAL_TEXT_ALIGNMENT_MATRIX = [
[1, 1, 1],
[0, 0, 0],
[-1, -1, -1],
];
const DIAGONAL_TEXT_ALIGNMENT_MATRIX = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
];
async function createPageSet(pdfNet: typeof Core.PDFNet, pages: number[]) {
const pageSet = await pdfNet.PageSet.create();
for (const page of pages) {
@ -93,26 +111,33 @@ export async function stampPDFPage(
await stamper.setAlignment(horizontalAlignment, verticalAlignment);
verticalAlignment = reverseNumberSign(verticalAlignment);
switch (orientation) {
case 'VERTICAL':
await stamper.setRotation(-90);
await stamper.setTextAlignment(VERTICAL_TEXT_ALIGNMENT_MATRIX[verticalAlignment + 1][horizontalAlignment + 1]);
await stamper.setPosition(10, 20);
break;
case 'HORIZONTAL':
break;
case 'TOP_LEFT':
await stamper.setRotation(90);
await stamper.setPosition(20, 20);
await stamper.setTextAlignment(HORIZONTAL_TEXT_ALIGNMENT_MATRIX[verticalAlignment + 1][horizontalAlignment + 1]);
await stamper.setPosition(20, 10);
break;
case 'DIAGONAL':
default:
await stamper.setRotation(-45);
await stamper.setTextAlignment(DIAGONAL_TEXT_ALIGNMENT_MATRIX[verticalAlignment + 1][horizontalAlignment + 1]);
await stamper.setPosition(20, 10);
}
const initialFont = await pdfNet.Font.create(document, convertFont(pdfNet, fontType));
// in case there are japanese characters in the text, we add them to the font
const fontWithAllTextChars = await pdfNet.Font.createFromFontDescriptor(document, initialFont, text);
await stamper.setFont(fontWithAllTextChars);
await stamper.setTextAlignment(1);
await stamper.stampText(document, text, pageSet);
}, licenseKey);
}
function reverseNumberSign(n: number): number {
return n - n * 2;
}