C# iTextSharp xmlworker-如何从html设置虚线表边框

C# iTextSharp xmlworker-如何从html设置虚线表边框,c#,html,css,itext,xmlworker,C#,Html,Css,Itext,Xmlworker,大家好,你们这些聪明的StackOverflow人 昨天我接到一个任务,要把扫描的图像转换成PDF文档。 由于我没有时间学习iText的所有技巧和窍门,我决定使用xmlWorker并创建文档本身的HTML模板。 我很成功,结果是: 然而 并不是一切都顺利。如果您仔细查看扫描文档,您可能会注意到,在文档的中间有一个带有虚线边框的表。这就是我头痛的开始。 在过去的15个小时里,我一直在谷歌上寻找解决方案,但没有成功。我尝试过各种CSS边框定义,如: 左边框样式:虚线 边框样式:虚线 边框:虚线

大家好,你们这些聪明的StackOverflow人

昨天我接到一个任务,要把扫描的图像转换成PDF文档。 由于我没有时间学习iText的所有技巧和窍门,我决定使用xmlWorker并创建文档本身的HTML模板。 我很成功,结果是:

然而

并不是一切都顺利。如果您仔细查看扫描文档,您可能会注意到,在文档的中间有一个带有虚线边框的表。这就是我头痛的开始。 在过去的15个小时里,我一直在谷歌上寻找解决方案,但没有成功。我尝试过各种CSS边框定义,如:

  • 左边框样式:虚线
  • 边框样式:虚线
  • 边框:虚线
这些CSS定义似乎被忽略了

所以我的问题是,有没有合适的方法来定义一个带有虚线边框的HTML表,以便它可以正确地转换为PDF文档

我使用的是Nuget最新的iTextSharp(5.5.12节)

先谢谢你

编辑:

24小时后我想我有了答案。 它是这两个示例的组合:

基本上,我实现了IPdfPCellEvent接口,因此我可以在PdfCell上使用CellEvent:

public class DottedCell : IPdfPCellEvent
{
    private readonly int _border = 0;

    public DottedCell(int border)
    {
        _border = border;
    }

    public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
    {
        var canvas = canvases[PdfPTable.LINECANVAS];
        canvas.SaveState();
        canvas.SetLineDash(0, 2, 2);

        cell.Border = Rectangle.NO_BORDER;

        if ((_border & Rectangle.TOP_BORDER) == Rectangle.TOP_BORDER)
        {
            canvas.MoveTo(position.GetRight(1), position.GetTop(1));
            canvas.LineTo(position.GetLeft(1), position.GetTop(1));
        }
        if ((_border & Rectangle.BOTTOM_BORDER) == Rectangle.BOTTOM_BORDER)
        {
            canvas.MoveTo(position.GetRight(1), position.GetBottom(1));
            canvas.LineTo(position.GetLeft(1), position.GetBottom(1));
        }
        if ((_border & Rectangle.RIGHT_BORDER) == Rectangle.RIGHT_BORDER)
        {
            canvas.MoveTo(position.GetRight(1), position.GetTop(1));
            canvas.LineTo(position.GetRight(1), position.GetBottom(1));
        }
        if ((_border & Rectangle.LEFT_BORDER) == Rectangle.LEFT_BORDER)
        {
            canvas.MoveTo(position.GetLeft(1), position.GetTop(1));
            canvas.LineTo(position.GetLeft(1), position.GetBottom(1));
        }
        canvas.Stroke();
        canvas.RestoreState();
    }
}
之后,我重写了iTextSharp.tool.xml.html.table.TableData类:

public class TableDataProcessor : TableData
{
    bool HasBorderStyle(IDictionary<string, string> attributeMap, string borderPosition, string borderStyle)
    {
        var hasStyle = attributeMap.ContainsKey("style");

        if (!hasStyle)
        {
            return false;
        }

        var borderLeft = attributeMap["style"]
            .Split(';')
            .FirstOrDefault(o => o.Trim().StartsWith("border-style-" + borderPosition + ":"));

        if (borderLeft != null)
        {
            return borderLeft.Split(':').Any(o => o.Trim().ToLower() == borderStyle);
        }

        return false;
    }



    public override IList<IElement> End(IWorkerContext ctx, Tag tag, IList<IElement> currentContent)
    {
        var cells = base.End(ctx, tag, currentContent);

        var attributeMap = tag.Attributes;
        if (HasBorderStyle(attributeMap, "left", "dotted"))
        {
            var pdfPCell = (PdfPCell) cells[0];
            pdfPCell.CellEvent = null;
            pdfPCell.CellEvent = new DottedCell(Rectangle.LEFT_BORDER);
        }

        return cells;
    }
}
它的工作原理是:

HTML标记:

<table class="content-wrapper">
    <tbody>
        <tr>
            <td class="pcnt_60 content-left top" valign="top">
                <table>
                  <tr>
                    <td style='border-left: 0.5px; border-style-left: dotted;'>content goes here</td>
                  </tr>
                </table>
            </td>
        </tr>
    </tbody>
</table>

内容在这里
<table class="content-wrapper">
    <tbody>
        <tr>
            <td class="pcnt_60 content-left top" valign="top">
                <table>
                  <tr>
                    <td style='border-left: 0.5px; border-style-left: dotted;'>content goes here</td>
                  </tr>
                </table>
            </td>
        </tr>
    </tbody>
</table>