Skip to content

Commit

Permalink
fix: flip text alignment for RTL multiline text fields
Browse files Browse the repository at this point in the history
otherwise we'd get the opposite of what is defined on the field itself
  • Loading branch information
tomyam1 authored and asturio committed Mar 13, 2022
1 parent a0afd13 commit 21356d0
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions openpdf/src/main/java/com/lowagie/text/pdf/TextField.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,23 @@ public static String obfuscatePassword(String text) {
return new String(pchar);
}

/**
* Flip text alignment for RTL texts Not sure why but this is needed
*/
private int getTextAlignment(int runDirection) {
if (runDirection == PdfWriter.RUN_DIRECTION_RTL) {
if (alignment == Element.ALIGN_LEFT) {
return Element.ALIGN_RIGHT;
} else if (alignment == Element.ALIGN_RIGHT) {
return Element.ALIGN_LEFT;
} else {
return alignment;
}
} else {
return alignment;
}
}

/**
* Get the <code>PdfAppearance</code> of a text or combo field
* @throws IOException on error
Expand Down Expand Up @@ -278,7 +295,7 @@ else if ((options & MULTILINE) == 0)
usize = 12;
float step = Math.max((usize - 4) / 10, 0.2f);
ct.setSimpleColumn(0, -h, width, 0);
ct.setAlignment(alignment);
ct.setAlignment(getTextAlignment(rtl));
ct.setRunDirection(rtl);
for (; usize > 4; usize -= step) {
ct.setYLine(0);
Expand All @@ -299,7 +316,7 @@ else if ((options & MULTILINE) == 0)
float offsetY = offsetX + h - ufont.getFontDescriptor(BaseFont.BBOXURY, usize);
ct.setSimpleColumn(extraMarginLeft + 2 * offsetX, -20000, box.getWidth() - 2 * offsetX, offsetY + leading);
ct.setLeading(leading);
ct.setAlignment(alignment);
ct.setAlignment(getTextAlignment(rtl));
ct.setRunDirection(rtl);
ct.setText(phrase);
ct.go();
Expand Down

0 comments on commit 21356d0

Please sign in to comment.