itext7.NET库中单元格上的SetMargin不工作

itext7.NET库中单元格上的SetMargin不工作,itext,itext7,Itext,Itext7,我正在尝试使用C#重新创建这里的CellMarginAdding示例: 除了利润外,一切正常。调用SetMargin()、SetMarginBottom()或SetMarginTop()的单元格上根本没有设置边距 我的C#代码是一个直接端口,如下所示: //Initialize PDF document PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); // Initialize d

我正在尝试使用C#重新创建这里的CellMarginAdding示例:

除了利润外,一切正常。调用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库中可能有bug吗?

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();
}
受保护的无效操作EPDF(字符串dest)引发异常{
PdfDocument pdfDoc=新PdfDocument(新PdfWriter(dest));
单据单据=新单据(pdfDoc);
PdfCanvas cb=newpdfcanvas(pdfDoc.addNewPage());
cb.moveTo(36842);
cb.lineTo(36,0);
cb.stroke();
表=新表(8);
表.setHorizontalAlignment(HorizontalAlignment.LEFT);
表1.设置宽度(150);
用于(int-aw=0;aw<16;aw++){
表.addCell(新单元格().add(新段落(“hi”));
}
表2.setMarginLeft(25);
单据新增(表);
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>
当然,如果希望获得原始行为,可以覆盖
CellRenderer
。这就是我在示例中所做的:

私有类MarginCellRenderer扩展CellRenderer{
公共MarginCellRenderer(单元模型元素){
超级(模型元素);
}
@凌驾
受保护的矩形应用边距(矩形矩形、浮点[]边距、布尔反向){
返回矩形应用边距(边距[0]、边距[1]、边距[2]、边距[3]、反向);
}
}
私有类MarginCell扩展单元{
公共MarginCell(){
超级();
}
公共MarginCell(int rowspan,int colspan){
超级(rowspan、colspan);
}
@凌驾
受保护的IRenderer makeNewRenderer(){
返回新的MarginCellRenderer(此);
}
}
公共静态void main(字符串args[])引发IOException{
文件文件=新文件(DEST);
文件.getParentFile().mkdirs();
新的C05E05_CellMarginPadding2().createPdf(DEST);
}
public void createPdf(字符串dest)引发IOException{
//初始化PDF文档
PdfDocument pdf=新PdfDocument(新PdfWriter(dest));
//初始化文档
文件=新文件(pdf);
Table Table=新表(UnitValue.createPercentArray(新浮点[]{2,1,1}));
表1.setBackgroundColor(橙色);
表1.设定宽度百分比(80);
表.setHorizontalAlignment(HorizontalAlignment.CENTER);
表1.addCell(
新MarginCell(1,3)。添加(“带有colspan 3的单元格”)
.setPadding(10)、setMargin(5)、setBackgroundColor(Color.GREEN));
表.addCell(新MarginCell(2,1).添加(“行跨度为2的单元格”)
.setMarginTop(5).setMarginBottom(5).setPaddingLeft(30)
.setFontColor(颜色.白色).setBackgroundColor(颜色.蓝色));
table.addCell(新MarginCell().add(“行1;单元格1”)
.setFontColor(颜色.白色).setBackgroundColor(颜色.红色));
table.addCell(新MarginCell().add(“第1行;第2单元格”);
table.addCell(新MarginCell().add(“第2行;单元格1”).setMargin(10)
.setFontColor(颜色.白色).setBackgroundColor(颜色.红色));
table.addCell(新MarginCell().add(“第2行;第2单元格”).setPadding(10)
.setFontColor(颜色.白色).setBackgroundColor(颜色.红色));
文件。添加(表);
document.close();
}
我创建了一个
MarginCell
类,该类使用
MarginCellRenderer
渲染单元格。在该渲染器中,我覆盖了
applyMargins()
方法,这样边距就不再被忽略


这就解决了你的问题。对本教程第5章的更改将尽快应用(取决于有多少人要求我在堆栈溢出上花费时间:D)。

恕我直言,我不需要样本。我了解图书馆的工作原理。我只是指出,在表格单元格上设置边距不适用于.NET版本,我希望a)也许其他人可以复制这种行为,或者b)具体告诉我我做错了什么,如果有什么问题的话。我假设您会从代码示例中受益。因为整个代码库都通过集成测试和单元测试进行了彻底的测试。我已经测试了代码,我发现教程中的屏幕截图和我今天得到的结果之间存在差异。该教程是在iText 7正式发布之前编写的。我认为在最近的版本中,这种行为已经发生了变化。我使用CSS中与C#示例相同的
边距
填充
值创建了一个HTML文件。HTML看起来像我今天得到的PDF。我认为@MissingLinq所期望的行为是一个已修复的bug的结果。本教程应该重写以反映iText的当前版本。请注意,当我将
setMargin(10)
替换为
SetBorderLeft(新的SolidBorder(Color.BLACK,10,0))
时,得到了更好的结果(虽然还不完美)。我有一个更新供您参考。事实上,你的问题很有趣。因为你的问题,我发现原本应该复习的教程没有复习得足够透彻。现在我自己来复习。事实上,如果您有兴趣,我已经将所有教程示例转换为C#at。在复习时,您可能会注意到第5章的一些教程仍然使用过时的方法进行表实例化……哇,您做得很好。我已要求iText的某个人与您联系,并使这些示例更为正式,例如,将其添加到官方网站,并提供正确的确认和指向您网站的链接。是的,我们git存储库中的示例已更新为最新版本,包括cmp(“比较”)文件(因为所有示例也都是测试
@Override
protected Rectangle applyMargins(Rectangle rect, float[] margins, boolean reverse) {
    // Do nothing here. Margins shouldn't be processed on cells.
    return rect;
}
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();
}