MigraDoc TextFrame Wrapstyle on Page Break

风流意气都作罢 提交于 2019-12-11 17:26:57

问题


I am using TextFrames in MigraDoc to be able to display in-line tables (2 per line) - this seems to work a treat:

... until the end of the page approaches.

(The red boxes are the TextFrame borders - I have included to help visualize things)

There seems to be a threshold where the top row's second table decides to break onto the next page - despite it actually being shorter than the first table. I want both tables to stay on the same page if there is room.

The code I am using is as follows. It exists in a method that is called for each table in a row:

                        TextFrame newTF = new TextFrame();
                        newTF.Add(newTable.Clone());

                        Unit tfWidth = 0;
                        foreach (Column tableCol in newTable.Columns)
                            tfWidth += tableCol.Width;

                        newTF.Width = Unit.FromPoint(tfWidth);
                        newTF.WrapFormat.Style = WrapStyle.Through;
                        newTF.LineFormat.Color = GetColourFromHex("#FF0000");

                        int currentTableHeight = 14 * newTable.Rows.Count;
                        double currentPosition = GetMigraHeightPosition();

                        if (currentPosition + currentTableHeight > pageActiveHeight)
                            this.document.LastSection.AddPageBreak();

                        if (sourceTable.InLineRootTableId == sourceTable.Id) // If this is the first table of the row...
                        {
                            currentInlineTableCount = 1;
                            newTF.RelativeVertical = RelativeVertical.Line;
                            newTF.RelativeHorizontal = RelativeHorizontal.Margin;
                            maxInlineTableCount = sourceTable.InlineTablesNumber;
                            maxInlineTableHeight = currentTableHeight;
                            newTF.Height = Unit.FromPoint(maxInlineTableHeight);

                            this.document.LastSection.Add(newTF);
                        }
                        else
                        {
                            newTF.RelativeHorizontal = RelativeHorizontal.Character;

                            currentInlineTableCount++;
                            newTF.Left = Unit.FromPoint(newTF.Width + 50);

                            if (maxInlineTableHeight < currentTableHeight)
                                maxInlineTableHeight = currentTableHeight;

                            newTF.Height = Unit.FromPoint(maxInlineTableHeight);

                            if (currentInlineTableCount == maxInlineTableCount)   // If this is the LAST table in the row
                            {
                                newTF.WrapFormat.Style = WrapStyle.TopBottom;
                                currentInlineTableCount = 1;
                            }

                            this.document.LastSection.Add(newTF);
                        }


    public double GetMigraHeightPosition()
    {
        MigraDoc.Rendering.DocumentRenderer docRenderer = new DocumentRenderer(this.document);
        docRenderer.PrepareDocument();

        RenderInfo[] RenderInfos = docRenderer.GetRenderInfoFromPage(docRenderer.FormattedDocument.PageCount);
        RenderInfo r = RenderInfos[RenderInfos.Count() - 1];
        return r.LayoutInfo.ContentArea.Y + r.LayoutInfo.ContentArea.Height;
    }

If I keep forcing the tables down so they are all on the next page they go back to being on the same line?!

Does anyone have any idea where I might be going wrong here please?

来源:https://stackoverflow.com/questions/55953600/migradoc-textframe-wrapstyle-on-page-break

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!