使用 iText 的 XMLWorker 将 table 水平居中
Center a table horizontally with iText's XMLWorker
我试过以两种方式使 table 居中:
尝试 1 代码:
<h1>Attempt 1</h1>
<table style="width: 50%; margin: 0 auto;">
<tr>
<th>foo</th>
<td>bar</td>
</tr>
<tr>
<th>foo</th>
<td>bar</td>
</tr>
</table>
尝试 2 代码:
<h1>Attempt 2</h1>
<table style="width: 50%;" align="center">
<tr>
<th>foo</th>
<td>bar</td>
</tr>
<tr>
<th>foo</th>
<td>bar</td>
</tr>
</table>
使用 XMLWorker 演示:http://demo.itextsupport.com/xmlworker/
这是 HTML 预览:
如您所见,两个 table 都居中。
虽然,当我点击 "transform" 时,我得到了这个:
我后来也试过将 table 包裹在 <div style="text-align: center"></div>
中,但没有用
如评论部分所述,XML Worker 尚不支持此功能。我们会将其添加到下一个版本中。如果您等不及下一个版本,请应用此补丁:
diff --git a/src/main/java/com/itextpdf/tool/xml/html/table/Table.java b/src/main/java/com/itextpdf/tool/xml/html/table/Table.java
index 541818bfc9..e262b4a406 100644
--- a/src/main/java/com/itextpdf/tool/xml/html/table/Table.java
+++ b/src/main/java/com/itextpdf/tool/xml/html/table/Table.java
@@ -165,6 +165,19 @@ public class Table extends AbstractTagProcessor {
table.setHeaderRows(headerRows + footerRows);
table.setFooterRows(footerRows);
+ if ( tag.getAttributes().containsKey(HTML.Attribute.ALIGN)) {
+ String value = tag.getAttributes().get(HTML.Attribute.ALIGN);
+ if ( value != null ) {
+ if (value.equalsIgnoreCase(CSS.Value.RIGHT)) {
+ table.setHorizontalAlignment(Element.ALIGN_RIGHT);
+ } else if ( value.equalsIgnoreCase(CSS.Value.LEFT)) {
+ table.setHorizontalAlignment(Element.ALIGN_LEFT);
+ } else if ( value.equalsIgnoreCase(CSS.Value.CENTER)) {
+ table.setHorizontalAlignment(Element.ALIGN_CENTER);
+ }
+ }
+ }
+
int direction = getRunDirection(tag);
if (direction != PdfWriter.RUN_DIRECTION_DEFAULT) {
table.setRunDirection(direction);
我试过以两种方式使 table 居中:
尝试 1 代码:
<h1>Attempt 1</h1>
<table style="width: 50%; margin: 0 auto;">
<tr>
<th>foo</th>
<td>bar</td>
</tr>
<tr>
<th>foo</th>
<td>bar</td>
</tr>
</table>
尝试 2 代码:
<h1>Attempt 2</h1>
<table style="width: 50%;" align="center">
<tr>
<th>foo</th>
<td>bar</td>
</tr>
<tr>
<th>foo</th>
<td>bar</td>
</tr>
</table>
使用 XMLWorker 演示:http://demo.itextsupport.com/xmlworker/
这是 HTML 预览:
如您所见,两个 table 都居中。
虽然,当我点击 "transform" 时,我得到了这个:
我后来也试过将 table 包裹在 <div style="text-align: center"></div>
中,但没有用
如评论部分所述,XML Worker 尚不支持此功能。我们会将其添加到下一个版本中。如果您等不及下一个版本,请应用此补丁:
diff --git a/src/main/java/com/itextpdf/tool/xml/html/table/Table.java b/src/main/java/com/itextpdf/tool/xml/html/table/Table.java
index 541818bfc9..e262b4a406 100644
--- a/src/main/java/com/itextpdf/tool/xml/html/table/Table.java
+++ b/src/main/java/com/itextpdf/tool/xml/html/table/Table.java
@@ -165,6 +165,19 @@ public class Table extends AbstractTagProcessor {
table.setHeaderRows(headerRows + footerRows);
table.setFooterRows(footerRows);
+ if ( tag.getAttributes().containsKey(HTML.Attribute.ALIGN)) {
+ String value = tag.getAttributes().get(HTML.Attribute.ALIGN);
+ if ( value != null ) {
+ if (value.equalsIgnoreCase(CSS.Value.RIGHT)) {
+ table.setHorizontalAlignment(Element.ALIGN_RIGHT);
+ } else if ( value.equalsIgnoreCase(CSS.Value.LEFT)) {
+ table.setHorizontalAlignment(Element.ALIGN_LEFT);
+ } else if ( value.equalsIgnoreCase(CSS.Value.CENTER)) {
+ table.setHorizontalAlignment(Element.ALIGN_CENTER);
+ }
+ }
+ }
+
int direction = getRunDirection(tag);
if (direction != PdfWriter.RUN_DIRECTION_DEFAULT) {
table.setRunDirection(direction);