【问题标题】:Calculating rowspan and colspan for html table (Algorithm)计算 html 表的行跨度和列跨度(算法)
【发布时间】:2025-12-18 11:30:01
【问题描述】:

我已经工作了大约 18 个小时,我变成了一个僵尸。没有你的帮助,我无法解决这个问题。

项目

我正在开发一个将 pdf 文件转换为 epub 格式的 pdf 到 epub 转换器应用程序。

问题

由于 EPub 的限制,EPub xhtml 上不允许绝对定位。因此,我决定对页面 (html) 上的所有元素(文本和图像)使用表格布局。

但我无法想象如何计算单元格的 rowspan 和 colspan 值..

我做了什么

    class TableItem
    {
        public IRenderable Item;
        public Rectangle Bounds;
        public int RowSpan, ColSpan;
        public TableItem(Rectangle bounds, IRenderable item)
        {
            this.Bounds = bounds;
            this.Item = item;
        }
    }

    class Table
    {
        Rectangle bounds;
        List<TableItem> items;

        public Table(int width, int height)
        {
            bounds = new Rectangle(0, 0, width, height);
            items = new List<TableItem>();
        }

        public void Add(IRenderable item)
        {
            Rectangle rect = Rectangle.Intersect(bounds, item.GetBounds());
            if (rect.IsEmpty)
                return;
            items.Add(new TableItem(rect, item));
        }

        List<TableItem> Slice(List<TableItem> list, int startIndex, int endIndex)
        {
            TableItem[] tmp = new TableItem[endIndex - startIndex];
            list.CopyTo(startIndex, tmp, 0, tmp.Length);
            return new List<TableItem>(tmp);
        }

        List<TableItem>[] Analyze(out int maxCol)
        {
            maxCol = 0;
            if (items.Count < 1)
                return new List<TableItem>[0];

            /*
            items.Sort
                (
                    delegate(TableItem x, TableItem y)
                    {
                        if (x.Bounds.Y == y.Bounds.Y)
                            return x.Bounds.X - y.Bounds.X;
                        return x.Bounds.Y - y.Bounds.Y;
                    }
                );
             */ 

            List<Rectangle> freeAreas = new List<Rectangle>();
            freeAreas.Add(bounds);

            foreach (TableItem item in items)
            {
                for (int i = 0; i < freeAreas.Count; i++)
                {
                    Rectangle area;
                    if ((area = freeAreas[i]).Contains(item.Bounds))
                    {
                        freeAreas.RemoveAt(i);
                        freeAreas.InsertRange(i, SplitRect(area, item.Bounds));
                    }
                }
            }

            foreach (Rectangle rt in freeAreas)
                items.Add(new TableItem(rt, null));

            items.Sort
                (
                    delegate(TableItem x, TableItem y)
                    {
                        if (x.Bounds.Y == y.Bounds.Y)
                            return x.Bounds.X - y.Bounds.X;
                        return x.Bounds.Y - y.Bounds.Y;
                    }
                );

            List<List<TableItem>> xlist = new List<List<TableItem>>();
            int lasty = items[0].Bounds.Y;
            int startIndex = 0;
            for(int i=0;i<items.Count;i++)
                if (lasty < items[i].Bounds.Y)
                {
                    lasty = items[i].Bounds.Y;
                    List<TableItem> xl = Slice(items, startIndex, i);
                    xlist.Add(xl);
                    startIndex = i;
                    if (maxCol < xl.Count)
                        maxCol = xl.Count;
                }

            if (startIndex < items.Count - 1)
            {
                xlist.Add(Slice(items, startIndex, items.Count));
                int t = xlist[xlist.Count - 1].Count;
                if (maxCol < t)
                    t = maxCol;
            }

            return xlist.ToArray();
        }

        private Rectangle[] SplitRect(Rectangle a, Rectangle r)
        {
            if (a == r)
                return new Rectangle[0];

            if (!Rectangle.Inflate(a, -2, -2).Contains(r))
                return new Rectangle[0];

            #region MyRegion
            if (r.X == a.X)
            {
                if (r.Height == a.Height)
                {
                    #region MyRegion
                    return new Rectangle[]
                    {
                        new Rectangle(r.Right, a.Y, a.Width - r.Width, a.Height)
                    };
                    #endregion
                }
                else if (r.Y == a.Y)
                {
                    #region MyRegion
                    if (r.Width == a.Width)
                    {
                        return new Rectangle[]
                        {
                            new Rectangle(a.X, r.Bottom, a.Width, a.Height - r.Height)
                        };
                    }
                    return new Rectangle[]
                    {
                        new Rectangle(r.Right, a.Y, a.Width - r.Width, r.Height),
                        new Rectangle(a.X, r.Bottom, a.Width, a.Height - r.Height)
                    };
                    #endregion
                }
                else if (r.Bottom == a.Bottom)
                {
                    #region MyRegion
                    if (r.Width == a.Width)
                    {
                        return new Rectangle[]
                        {
                            new Rectangle(a.X, a.Y, a.Width, r.Y - a.Y)
                        };
                    }
                    return new Rectangle[]
                    {
                        new Rectangle(a.X, a.Y, a.Width, r.Y - a.Y),
                        new Rectangle(r.Right, r.Y, a.Width - r.Width, r.Height)
                    };
                    #endregion
                }
                else
                {
                    #region MyRegion
                    if (a.Width == r.Width)
                    {
                        return new Rectangle[]
                        {
                            new Rectangle(a.X, a.Y, a.Width, a.Height - (r.Y - a.Y)),
                            new Rectangle(a.X, r.Bottom, a.Width, a.Height - (r.Bottom - a.Y))
                        };
                    }
                    return new Rectangle[]
                    {
                        new Rectangle(a.X, a.Y, a.Width, a.Height - (r.Y - a.Y)),
                        new Rectangle(r.Right, r.Y, a.Width - r.Right, r.Height),
                        new Rectangle(a.X, r.Bottom, a.Width, a.Height - (r.Bottom - a.Y))
                    };
                    #endregion
                }
            }
            else if (r.Y == a.Y)
            {
                if (r.Height == a.Height)
                {
                    #region MyRegion
                    if (r.Right == a.Right)
                    {
                        return new Rectangle[]
                        {
                            new Rectangle(a.X, a.Y, a.Width - r.Width, a.Height)
                        };
                    }
                    return new Rectangle[]
                    {
                        new Rectangle(a.X, a.Y, r.X - a.X, a.Height),
                        new Rectangle(r.Right, a.Y, a.Right - r.Right, a.Height)
                    };
                    #endregion
                }
                else
                {
                    #region MyRegion
                    if (r.Right == a.Right)
                    {
                        return new Rectangle[]
                        {
                            new Rectangle(a.X, a.Y, a.Width, r.Y - a.Y),
                            new Rectangle(a.X, r.Y, r.X - a.X, r.Height),
                            new Rectangle(a.X, r.Bottom, a.Width, a.Bottom - r.Bottom)
                        };
                    }
                    return new Rectangle[]
                    {
                        new Rectangle(a.X, a.Y, r.X - a.X, r.Height),
                        Rectangle.FromLTRB(r.Right, a.Y, a.Right, r.Bottom),
                        Rectangle.FromLTRB(a.X, r.Bottom, a.Right, a.Bottom)
                    };
                    #endregion
                }
            }
            else if (r.Right == a.Right)
            {
                #region MyRegion
                if (a.Bottom == r.Bottom)
                {
                    return new Rectangle[]
                    {
                        Rectangle.FromLTRB(a.X, a.Y, a.Right, r.Y),
                        Rectangle.FromLTRB(a.X, r.Y, r.X, r.Bottom)
                    };
                }
                return new Rectangle[]
                {
                    Rectangle.FromLTRB(a.X, a.Y, a.Right, r.Y),
                    Rectangle.FromLTRB(a.X, r.Y, r.X, r.Bottom),
                    Rectangle.FromLTRB(a.X, r.Bottom, a.Right, a.Bottom)
                };
                #endregion
            }
            else if (r.Bottom == a.Bottom)
            {
                #region MyRegion
                return new Rectangle[]
                {
                    Rectangle.FromLTRB(a.X, a.Y, a.Right, r.Y),
                    Rectangle.FromLTRB(a.X, r.Y, r.X, r.Bottom),
                    Rectangle.FromLTRB(r.Right, r.Y, a.Right, a.Bottom)
                };
                #endregion
            } 
            #endregion

            return new Rectangle[]
            {
                Rectangle.FromLTRB(a.X, a.Y, a.Right, r.Y),
                Rectangle.FromLTRB(a.X, r.Y, r.X, r.Bottom),
                Rectangle.FromLTRB(r.Right, r.Y, a.Right, r.Bottom),
                Rectangle.FromLTRB(a.X, r.Bottom, a.Right, a.Bottom)
            };
        }

        public void Compose(EPubWriter writer)
        {
            int mc;
            List<TableItem>[] all = Analyze(out mc);
            if (all.Length < 1)
                return;
            foreach (List<TableItem> item in all)
            {
                writer.BeginRow();

                for (int i = 0; i < item.Count; i++)
                {
                    TableItem ti = item[i];

                    ti.RowSpan = 0; //???? Should Calculate
                    ti.ColSpan = 0; //???? Should Calculate

                    writer.Render(ti.Item, ti.RowSpan, ti.ColSpan);
                }

                writer.EndRow();
            }
        }
    }

我需要什么

    public void Compose(EPubWriter writer)
    {
        int mc;
        // Analyze: Calculates item positions, creates
        // empty TableItem s if needs, sorts by y ASC,x ASC
        // Returns array of rows (TR) what contains cells (TD)
        List<TableItem>[] all = Analyze(out mc);
        if (all.Length < 1)
            return;
        foreach (List<TableItem> item in all)
        {
            writer.BeginRow();

            for (int i = 0; i < item.Count; i++)
            {
                TableItem ti = item[i];

                ti.RowSpan = 0; //???? Should Calculate
                ti.ColSpan = 0; //???? Should Calculate

                writer.Render(ti.Item, ti.RowSpan, ti.ColSpan);
            }

            writer.EndRow();
        }
    }
                ti.RowSpan = 0; //???? Should Calculate
                ti.ColSpan = 0; //???? Should Calculate

谢谢你..

【问题讨论】:

  • 天哪! :) 在我在这里发布我的代码后,我发现我的代码上有一个错误 :))... 你能找到它吗?
  • 哎呀!这个问题真的很傻吗?还是真的很难……?

标签: c# algorithm html-table


【解决方案1】:

好的..我找不到任何好的表格布局解决方案..所以我通过 divs 更改了布局样式..

我为所有空白区域使用了空 div,并为它们提供了与空白区域所需的一样多的固定宽度和高度。并将它们的 float 属性设置为 left。

最后我将所有 div、span 和图像放入一个容器 div 中,该容器 div 的宽度属性设置为页面宽度..

【讨论】: