Cell 上的 SetMargin 在 itext7 .NET 库中不起作用
SetMargin on Cell not working in itext7 .NET library
我正在尝试使用 C# 重新创建此处找到的 CellMarginPadding 示例:
一切都按预期工作除了边距。在调用 SetMargin()、SetMarginBottom() 或 SetMarginTop() 的单元格上根本没有设置边距。
我的 C# 代码是一个直接端口,如下所示:
//Initialize PDF document
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
// Initialize document
Document document = new Document(pdf);
Table table = new Table(new float[] {2, 1, 1});
table.SetBackgroundColor(Color.ORANGE);
table.SetWidthPercent(80);
table.SetHorizontalAlignment(HorizontalAlignment.CENTER);
table.AddCell(new Cell(1, 3).Add("Cell with colspan 3")
.SetPadding(10).SetMargin(5).SetBackgroundColor(Color.GREEN));
table.AddCell(new Cell(2, 1).Add("Cell with rowspan 2")
.SetMarginTop(5).SetMarginBottom(5).SetPaddingLeft(30)
.SetFontColor(Color.WHITE).SetBackgroundColor(Color.BLUE));
table.AddCell(new Cell().Add("row 1; cell 1")
.SetFontColor(Color.WHITE).SetBackgroundColor(Color.RED));
table.AddCell(new Cell().Add("row 1; cell 2"));
table.AddCell(new Cell().Add("row 2; cell 1").SetMargin(10)
.SetFontColor(Color.WHITE).SetBackgroundColor(Color.RED));
table.AddCell(new Cell().Add("row 2; cell 2").SetPadding(10)
.SetFontColor(Color.WHITE).SetBackgroundColor(Color.RED));
document.Add(table);
document.Close();
我做错了吗?还是 itext7 .NET 库中可能存在错误?
iText 网站上有这方面的教程。
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
PdfCanvas cb = new PdfCanvas(pdfDoc.addNewPage());
cb.moveTo(36, 842);
cb.lineTo(36, 0);
cb.stroke();
Table table = new Table(8);
table.setHorizontalAlignment(HorizontalAlignment.LEFT);
table.setWidth(150);
for (int aw = 0; aw < 16; aw++) {
table.addCell(new Cell().add(new Paragraph("hi")));
}
table.setMarginLeft(25);
doc.add(table);
doc.close();
}
我认为您希望重新引入 iText 7.0.0 中存在的错误。
我已将您的代码示例转换为 HTML:
<table style="background: orange; text-align: center; width: 80%" border="0" cellspacing="0" align="center">
<tr>
<td colspan="3" style="padding: 10pt; margin: 5pt; background: green;">Cell with colspan 3</td>
</tr>
<tr>
<td rowspan="2" style="color: white; background: blue; margin-top: 5pt; margin-bottom: 30pt; padding-left: 30pt">Cell with rowspan 2</td>
<td style="color: white; background: red">row 1; cell 1</td>
<td>row 1; cell 2</td>
</tr>
<tr>
<td style="color: white; background: red; margin: 10pt;">row 2; cell 1</td>
<td style="color: white; background: red; padding: 10pt;">row 2; cell 2</td>
</tr>
</table>
我将您的 C# 代码中的边距、填充、颜色...的值转换为内联 HTML 中的 CSS 值。然后我将 HTML 转换为 PDF。
您可以在以下屏幕截图中看到 HTML 和 PDF:
您不同意 PDF 看起来像预期的那样吗?
如果您希望我们调查此事,请提供一些 HTML 代码来创建一个 table 来准确显示您在定义边距时想要实现的目标。检查 pdfHTML 是否将 HTML 转换为看起来或多或少相同的 PDF 对我们来说很有趣。如果是这样,并且如果您真的想编写 table 代码,则可以使用 pdfHTML(iText 7 之上的附加组件)使用的代码来模拟相同的结果直接来自代码。
更新:
由于您的问题,我已经开始更新积木教程的第 5 章。我还有很多工作要做,但我已经可以告诉你我的发现了。
正如我在最初的回答中提到的,Cell
对象中的边距被忽略,因为 HTML table 单元格中的 CSS 边距被忽略。 iText 7 CellRenderer
代码已更改为 pdfHTML 附加组件的功能:
@Override
protected Rectangle applyMargins(Rectangle rect, float[] margins, boolean reverse) {
// Do nothing here. Margins shouldn't be processed on cells.
return rect;
}
当然,如果你想得到原来的行为,你可以覆盖CellRenderer
。这就是我在 CellMarginPadding2 示例中所做的:
private class MarginCellRenderer extends CellRenderer {
public MarginCellRenderer(Cell modelElement) {
super(modelElement);
}
@Override
protected Rectangle applyMargins(Rectangle rect, float[] margins, boolean reverse) {
return rect.<Rectangle>applyMargins(margins[0], margins[1], margins[2], margins[3], reverse);
}
}
private class MarginCell extends Cell {
public MarginCell() {
super();
}
public MarginCell(int rowspan, int colspan) {
super(rowspan, colspan);
}
@Override
protected IRenderer makeNewRenderer() {
return new MarginCellRenderer(this);
}
}
public static void main(String args[]) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new C05E05_CellMarginPadding2().createPdf(DEST);
}
public void createPdf(String dest) throws IOException {
//Initialize PDF document
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
// Initialize document
Document document = new Document(pdf);
Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}));
table.setBackgroundColor(Color.ORANGE);
table.setWidthPercent(80);
table.setHorizontalAlignment(HorizontalAlignment.CENTER);
table.addCell(
new MarginCell(1, 3).add("Cell with colspan 3")
.setPadding(10).setMargin(5).setBackgroundColor(Color.GREEN));
table.addCell(new MarginCell(2, 1).add("Cell with rowspan 2")
.setMarginTop(5).setMarginBottom(5).setPaddingLeft(30)
.setFontColor(Color.WHITE).setBackgroundColor(Color.BLUE));
table.addCell(new MarginCell().add("row 1; cell 1")
.setFontColor(Color.WHITE).setBackgroundColor(Color.RED));
table.addCell(new MarginCell().add("row 1; cell 2"));
table.addCell(new MarginCell().add("row 2; cell 1").setMargin(10)
.setFontColor(Color.WHITE).setBackgroundColor(Color.RED));
table.addCell(new MarginCell().add("row 2; cell 2").setPadding(10)
.setFontColor(Color.WHITE).setBackgroundColor(Color.RED));
document.add(table);
document.close();
}
我创建了一个 MarginCell
class,它使用 MarginCellRenderer
渲染单元格。在那个渲染器中,我重写了 applyMargins()
方法,这样边距就不会再被忽略了。
这解决了您的问题。对本教程第 5 章的更改将尽快应用(取决于有多少人需要我在 Stack Overflow 上花时间 :D )。
我正在尝试使用 C# 重新创建此处找到的 CellMarginPadding 示例:
一切都按预期工作除了边距。在调用 SetMargin()、SetMarginBottom() 或 SetMarginTop() 的单元格上根本没有设置边距。
我的 C# 代码是一个直接端口,如下所示:
//Initialize PDF document
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
// Initialize document
Document document = new Document(pdf);
Table table = new Table(new float[] {2, 1, 1});
table.SetBackgroundColor(Color.ORANGE);
table.SetWidthPercent(80);
table.SetHorizontalAlignment(HorizontalAlignment.CENTER);
table.AddCell(new Cell(1, 3).Add("Cell with colspan 3")
.SetPadding(10).SetMargin(5).SetBackgroundColor(Color.GREEN));
table.AddCell(new Cell(2, 1).Add("Cell with rowspan 2")
.SetMarginTop(5).SetMarginBottom(5).SetPaddingLeft(30)
.SetFontColor(Color.WHITE).SetBackgroundColor(Color.BLUE));
table.AddCell(new Cell().Add("row 1; cell 1")
.SetFontColor(Color.WHITE).SetBackgroundColor(Color.RED));
table.AddCell(new Cell().Add("row 1; cell 2"));
table.AddCell(new Cell().Add("row 2; cell 1").SetMargin(10)
.SetFontColor(Color.WHITE).SetBackgroundColor(Color.RED));
table.AddCell(new Cell().Add("row 2; cell 2").SetPadding(10)
.SetFontColor(Color.WHITE).SetBackgroundColor(Color.RED));
document.Add(table);
document.Close();
我做错了吗?还是 itext7 .NET 库中可能存在错误?
iText 网站上有这方面的教程。
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
PdfCanvas cb = new PdfCanvas(pdfDoc.addNewPage());
cb.moveTo(36, 842);
cb.lineTo(36, 0);
cb.stroke();
Table table = new Table(8);
table.setHorizontalAlignment(HorizontalAlignment.LEFT);
table.setWidth(150);
for (int aw = 0; aw < 16; aw++) {
table.addCell(new Cell().add(new Paragraph("hi")));
}
table.setMarginLeft(25);
doc.add(table);
doc.close();
}
我认为您希望重新引入 iText 7.0.0 中存在的错误。
我已将您的代码示例转换为 HTML:
<table style="background: orange; text-align: center; width: 80%" border="0" cellspacing="0" align="center">
<tr>
<td colspan="3" style="padding: 10pt; margin: 5pt; background: green;">Cell with colspan 3</td>
</tr>
<tr>
<td rowspan="2" style="color: white; background: blue; margin-top: 5pt; margin-bottom: 30pt; padding-left: 30pt">Cell with rowspan 2</td>
<td style="color: white; background: red">row 1; cell 1</td>
<td>row 1; cell 2</td>
</tr>
<tr>
<td style="color: white; background: red; margin: 10pt;">row 2; cell 1</td>
<td style="color: white; background: red; padding: 10pt;">row 2; cell 2</td>
</tr>
</table>
我将您的 C# 代码中的边距、填充、颜色...的值转换为内联 HTML 中的 CSS 值。然后我将 HTML 转换为 PDF。
您可以在以下屏幕截图中看到 HTML 和 PDF:
您不同意 PDF 看起来像预期的那样吗?
如果您希望我们调查此事,请提供一些 HTML 代码来创建一个 table 来准确显示您在定义边距时想要实现的目标。检查 pdfHTML 是否将 HTML 转换为看起来或多或少相同的 PDF 对我们来说很有趣。如果是这样,并且如果您真的想编写 table 代码,则可以使用 pdfHTML(iText 7 之上的附加组件)使用的代码来模拟相同的结果直接来自代码。
更新:
由于您的问题,我已经开始更新积木教程的第 5 章。我还有很多工作要做,但我已经可以告诉你我的发现了。
正如我在最初的回答中提到的,Cell
对象中的边距被忽略,因为 HTML table 单元格中的 CSS 边距被忽略。 iText 7 CellRenderer
代码已更改为 pdfHTML 附加组件的功能:
@Override
protected Rectangle applyMargins(Rectangle rect, float[] margins, boolean reverse) {
// Do nothing here. Margins shouldn't be processed on cells.
return rect;
}
当然,如果你想得到原来的行为,你可以覆盖CellRenderer
。这就是我在 CellMarginPadding2 示例中所做的:
private class MarginCellRenderer extends CellRenderer {
public MarginCellRenderer(Cell modelElement) {
super(modelElement);
}
@Override
protected Rectangle applyMargins(Rectangle rect, float[] margins, boolean reverse) {
return rect.<Rectangle>applyMargins(margins[0], margins[1], margins[2], margins[3], reverse);
}
}
private class MarginCell extends Cell {
public MarginCell() {
super();
}
public MarginCell(int rowspan, int colspan) {
super(rowspan, colspan);
}
@Override
protected IRenderer makeNewRenderer() {
return new MarginCellRenderer(this);
}
}
public static void main(String args[]) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new C05E05_CellMarginPadding2().createPdf(DEST);
}
public void createPdf(String dest) throws IOException {
//Initialize PDF document
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
// Initialize document
Document document = new Document(pdf);
Table table = new Table(UnitValue.createPercentArray(new float[]{2, 1, 1}));
table.setBackgroundColor(Color.ORANGE);
table.setWidthPercent(80);
table.setHorizontalAlignment(HorizontalAlignment.CENTER);
table.addCell(
new MarginCell(1, 3).add("Cell with colspan 3")
.setPadding(10).setMargin(5).setBackgroundColor(Color.GREEN));
table.addCell(new MarginCell(2, 1).add("Cell with rowspan 2")
.setMarginTop(5).setMarginBottom(5).setPaddingLeft(30)
.setFontColor(Color.WHITE).setBackgroundColor(Color.BLUE));
table.addCell(new MarginCell().add("row 1; cell 1")
.setFontColor(Color.WHITE).setBackgroundColor(Color.RED));
table.addCell(new MarginCell().add("row 1; cell 2"));
table.addCell(new MarginCell().add("row 2; cell 1").setMargin(10)
.setFontColor(Color.WHITE).setBackgroundColor(Color.RED));
table.addCell(new MarginCell().add("row 2; cell 2").setPadding(10)
.setFontColor(Color.WHITE).setBackgroundColor(Color.RED));
document.add(table);
document.close();
}
我创建了一个 MarginCell
class,它使用 MarginCellRenderer
渲染单元格。在那个渲染器中,我重写了 applyMargins()
方法,这样边距就不会再被忽略了。
这解决了您的问题。对本教程第 5 章的更改将尽快应用(取决于有多少人需要我在 Stack Overflow 上花时间 :D )。