-
Notifications
You must be signed in to change notification settings - Fork 382
RTL support when using Apache POI with openPdf #729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -738,6 +738,12 @@ else if ( FoBreakBeforeAttribute.Value.COLUMN.toString().equals( breakBefore ) ) | |||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // writing-mode | ||||||||
| String writingMode = ele.getStyleWritingModeAttribute(); | ||||||||
| if ( StringUtils.isNotEmpty( writingMode ) ){ | ||||||||
| paragraphProperties.setWritingMode( writingMode ); | ||||||||
| } | ||||||||
|
|
||||||||
| super.visit( ele ); | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -1134,6 +1140,12 @@ else if ( TableAlignAttribute.Value.RIGHT.toString().equals( align ) ) | |||||||
| tableProperties.setMayBreakBetweenRows( mayBreakBetweenRows ); | ||||||||
| } | ||||||||
|
|
||||||||
| // writing-mode | ||||||||
| String writingMode = ele.getStyleWritingModeAttribute(); | ||||||||
| if ( StringUtils.isNotEmpty( writingMode ) ){ | ||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| tableProperties.setWritingMode( writingMode ); | ||||||||
| } | ||||||||
|
|
||||||||
| super.visit( ele ); | ||||||||
| } | ||||||||
|
|
||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| import com.lowagie.text.Element; | ||
|
|
||
| import com.lowagie.text.pdf.PdfPCell; | ||
| import com.lowagie.text.pdf.PdfWriter; | ||
| import fr.opensagres.odfdom.converter.pdf.internal.styles.Style; | ||
| import fr.opensagres.odfdom.converter.pdf.internal.styles.StyleTableProperties; | ||
| import fr.opensagres.odfdom.converter.pdf.internal.styles.StyleTableRowProperties; | ||
|
|
@@ -221,6 +222,13 @@ public void applyStyles( Style style ) | |
| { | ||
| super.setKeepTogether( !mayBreakBetweenRows ); | ||
| } | ||
| // Support for writing mode i.e. LTR OR RTL | ||
| String writingMode = tableProperties.getWritingMode(); | ||
| if ( writingMode != null && writingMode.equals("rl-tb")) | ||
| { | ||
| // RTL | ||
| super.setRunDirection(PdfWriter.RUN_DIRECTION_RTL); | ||
| } | ||
|
Comment on lines
+225
to
+231
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no else block compared to |
||
| } | ||
| StyleTableRowProperties tableRowProperties = style.getTableRowProperties(); | ||
| if ( tableRowProperties != null ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -48,6 +48,8 @@ public class StyleTableProperties | |||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| private Float width; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| private String writingMode; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public StyleTableProperties() | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
@@ -98,6 +100,10 @@ public void merge( StyleTableProperties tableProperties ) | |||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| width = tableProperties.getWidth(); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| if ( tableProperties.getWritingMode() != null ) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| writingMode = tableProperties.getWritingMode(); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public int getAlignment() | ||||||||||||||||||||||||||||||||||
|
|
@@ -189,4 +195,12 @@ public void setWidth( Float width ) | |||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| this.width = width; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public String getWritingMode() { | ||||||||||||||||||||||||||||||||||
| return writingMode; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public void setWritingMode(String writingMode) { | ||||||||||||||||||||||||||||||||||
| this.writingMode = writingMode; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+199
to
+205
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -35,6 +35,8 @@ | |||||||
| import java.util.List; | ||||||||
| import java.util.Map; | ||||||||
|
|
||||||||
| import fr.opensagres.poi.xwpf.converter.core.styles.paragraph.ParagraphRunDirectionProvider; | ||||||||
| import fr.opensagres.poi.xwpf.converter.core.styles.table.TableRunDirectionValueProvider; | ||||||||
|
Comment on lines
+38
to
+39
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could the import be moved next to other |
||||||||
| import org.apache.poi.ooxml.POIXMLDocumentPart; | ||||||||
| import org.apache.poi.xwpf.usermodel.ParagraphAlignment; | ||||||||
| import org.apache.poi.xwpf.usermodel.UnderlinePatterns; | ||||||||
|
|
@@ -53,6 +55,7 @@ | |||||||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocDefaults; | ||||||||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFont; | ||||||||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumPr; | ||||||||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff; | ||||||||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; | ||||||||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr; | ||||||||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPrBase; | ||||||||
|
|
@@ -448,6 +451,11 @@ public Color getBackgroundColor( CTPPrBase pPr ) | |||||||
| return ParagraphBackgroundColorValueProvider.INSTANCE.getValue( pPr ); | ||||||||
| } | ||||||||
|
|
||||||||
| /*enabling bidirectional support*/ | ||||||||
| public CTOnOff getParagraphRunDirection(XWPFParagraph docxParagraph) { | ||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| return ParagraphRunDirectionProvider.INSTANCE.getValue( docxParagraph, this ); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * @param paragraph | ||||||||
| * @return | ||||||||
|
|
@@ -786,6 +794,12 @@ public TableWidth getTableWidth( CTTbl table ) | |||||||
| this ); | ||||||||
| } | ||||||||
|
|
||||||||
| /*enabling bidirectional support*/ | ||||||||
| public CTOnOff getTableRunDirection(XWPFTable table ) | ||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| { | ||||||||
| return TableRunDirectionValueProvider.INSTANCE.getValue( table, this ); | ||||||||
| } | ||||||||
|
|
||||||||
| public ParagraphAlignment getTableAlignment( XWPFTable table ) | ||||||||
| { | ||||||||
| return TableAlignmentValueProvider.INSTANCE.getValue( table, this ); | ||||||||
|
|
||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a Copyright header like other files and a new line at the end of the file? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package fr.opensagres.poi.xwpf.converter.core.styles.paragraph; | ||
|
|
||
|
|
||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff; | ||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPrBase; | ||
|
|
||
| /*enabling bidirectional support -> paragraph direction*/ | ||
| public class ParagraphRunDirectionProvider extends AbstractParagraphValueProvider<CTOnOff> { | ||
|
|
||
| public static final ParagraphRunDirectionProvider INSTANCE = new ParagraphRunDirectionProvider(); | ||
|
|
||
| @Override | ||
| public CTOnOff getValue(CTPPrBase ppr) { | ||
| return ppr == null || !ppr.isSetBidi() ? null : ppr.getBidi(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package fr.opensagres.poi.xwpf.converter.core.styles.table; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a Copyright header like other files and a new line at the end of the file? |
||
|
|
||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff; | ||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr; | ||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPrBase; | ||
|
|
||
| /*enabling bidirectional support -> table run direction*/ | ||
| public class TableRunDirectionValueProvider extends AbstractTableValueProvider<CTOnOff> { | ||
|
|
||
| public static final TableRunDirectionValueProvider INSTANCE = new TableRunDirectionValueProvider(); | ||
|
|
||
| @Override | ||
| public CTOnOff getValue(CTTblPr tblPr) { | ||
| return getTableRunDirection(tblPr); | ||
| } | ||
|
|
||
| @Override | ||
| public CTOnOff getValue(CTTblPrBase tblPr) { | ||
| return getTableRunDirection(tblPr); | ||
| } | ||
|
|
||
| private CTOnOff getTableRunDirection(CTTblPrBase tblPr) { | ||
| return tblPr == null || !tblPr.isSetBidiVisual() ? null : tblPr.getBidiVisual(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ | |
| import java.util.Map; | ||
| import java.util.logging.Logger; | ||
|
|
||
| import com.lowagie.text.pdf.PdfWriter; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could it be moved next to other |
||
| import org.apache.poi.xwpf.usermodel.IBodyElement; | ||
| import org.apache.poi.xwpf.usermodel.ParagraphAlignment; | ||
| import org.apache.poi.xwpf.usermodel.UnderlinePatterns; | ||
|
|
@@ -61,6 +62,7 @@ | |
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr; | ||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHdrFtrRef; | ||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTLvl; | ||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff; | ||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPrBase; | ||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPTab; | ||
| import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; | ||
|
|
@@ -104,6 +106,7 @@ | |
| import fr.opensagres.poi.xwpf.converter.core.styles.paragraph.ParagraphIndentationLeftValueProvider; | ||
| import fr.opensagres.poi.xwpf.converter.core.utils.DxaUtil; | ||
| import fr.opensagres.poi.xwpf.converter.core.utils.StringUtils; | ||
| import fr.opensagres.poi.xwpf.converter.core.utils.XWPFUtils; | ||
| import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions; | ||
| import fr.opensagres.poi.xwpf.converter.pdf.internal.elements.StylableAnchor; | ||
| import fr.opensagres.poi.xwpf.converter.pdf.internal.elements.StylableDocument; | ||
|
|
@@ -333,13 +336,25 @@ protected IITextContainer startVisitParagraph( XWPFParagraph docxParagraph, List | |
|
|
||
| } | ||
|
|
||
| /*enabling bidirectional support -> paragraph direction*/ | ||
| CTOnOff bidi = stylesDocument.getParagraphRunDirection(docxParagraph); | ||
| if(isRTLLayoutSet(bidi)){ | ||
| pdfParagraph.setRunDirection(PdfWriter.RUN_DIRECTION_RTL); | ||
| }else{ | ||
| pdfParagraph.setRunDirection(PdfWriter.RUN_DIRECTION_LTR); | ||
| } | ||
|
|
||
| // text-align | ||
| ParagraphAlignment alignment = stylesDocument.getParagraphAlignment( docxParagraph ); | ||
| if ( alignment != null ) | ||
| { | ||
| switch ( alignment ) | ||
| { | ||
| /* please refer -> https://docs.microsoft.com/en-us/globalization/input/text-justification*/ | ||
| case LEFT: | ||
| case LOW_KASHIDA: | ||
| case MEDIUM_KASHIDA: | ||
| case HIGH_KASHIDA: | ||
| pdfParagraph.setAlignment( Element.ALIGN_LEFT ); | ||
| break; | ||
| case RIGHT: | ||
|
|
@@ -354,6 +369,9 @@ protected IITextContainer startVisitParagraph( XWPFParagraph docxParagraph, List | |
| default: | ||
| break; | ||
| } | ||
| } else { | ||
| /*enabling bidirectional support -> if by default Alignment of Paragraph is Undefined, set it to left*/ | ||
| pdfParagraph.setAlignment(Element.ALIGN_LEFT); | ||
| } | ||
|
|
||
| // background-color | ||
|
|
@@ -449,6 +467,10 @@ protected IITextContainer startVisitParagraph( XWPFParagraph docxParagraph, List | |
| return pdfParagraph; | ||
| } | ||
|
|
||
| private boolean isRTLLayoutSet(CTOnOff bidiProperty){ | ||
| return XWPFUtils.isCTOnOff(bidiProperty); | ||
| } | ||
|
|
||
| @Override | ||
| protected void endVisitParagraph( XWPFParagraph docxParagraph, IITextContainer pdfParentContainer, | ||
| IITextContainer pdfParagraphContainer ) | ||
|
|
@@ -1105,6 +1127,14 @@ private StylableTable createPDFTable( XWPFTable table, float[] colWidths, IIText | |
| } | ||
| pdfPTable.setLockedWidth( true ); | ||
|
|
||
| boolean isRightToLeftTable = false; | ||
| /*enabling bidirectional support -> Table layout/run direction(based on layout)*/ | ||
| CTOnOff bidiVisual = stylesDocument.getTableRunDirection(table); | ||
| if(isRTLLayoutSet(bidiVisual)){ | ||
| isRightToLeftTable = true; | ||
| pdfPTable.setRunDirection(PdfWriter.RUN_DIRECTION_RTL); | ||
| } | ||
|
|
||
| // Table alignment | ||
| ParagraphAlignment alignment = stylesDocument.getTableAlignment( table ); | ||
| if ( alignment != null ) | ||
|
|
@@ -1126,13 +1156,23 @@ private StylableTable createPDFTable( XWPFTable table, float[] colWidths, IIText | |
| default: | ||
| break; | ||
| } | ||
| } else { | ||
| if (isRightToLeftTable) { | ||
| pdfPTable.setHorizontalAlignment(Element.ALIGN_RIGHT); | ||
| } else { | ||
| pdfPTable.setHorizontalAlignment(Element.ALIGN_LEFT); | ||
| } | ||
| } | ||
|
|
||
| // Table indentation | ||
| Float indentation = stylesDocument.getTableIndentation( table ); | ||
| if ( indentation != null ) | ||
| { | ||
| pdfPTable.setPaddingLeft( indentation ); | ||
| if (isRightToLeftTable) { | ||
| pdfPTable.setPaddingRight(indentation); | ||
| } else { | ||
| pdfPTable.setPaddingLeft(indentation); | ||
| } | ||
| } | ||
| return pdfPTable; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.