Dev*_*per 3 .net c# wpf height flowdocument
我发现Run还是Paragraph在FlowDocument现在我需要知道HEIGHT它.
即
while (navigator.CompareTo(flowDocViewer.Document.ContentEnd) < 0)
{
TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
Run run = navigator.Parent as Run;
// I need to get HEIGHT of Run in pixels somehow
Run Code Online (Sandbox Code Playgroud)
事实上可以吗?

谢谢!
我正在使用的一个小功能.输入是包含Section的字符串.您可以轻松渲染其他块元素,如段落.
您也可以省略Parse方法的第二个参数.
诀窍不是测量段落,而是包含RichTextBox的ViewBox.这是实际呈现Flowdocument所必需的.ViewBox动态获取rtb的大小.也许你甚至可以在没有ViewBox的情况下做到这一点.我花了一些时间来解决这个问题,它对我有用.
注意Width的RichTextBox是设定double.MaxValue.这意味着当您想要测量单个段落时,它必须非常长或者所有内容都在一行中.所以这只有在你知道输出设备的宽度时才有意义.由于这是一个的FlowDocument没有宽度,它流动;)我使用它来分页一个的FlowDocument其中i知道的纸张尺寸.
返回的高度是与设备无关的单位.
private double GetHeaderFooterHeight(string headerFooter)
{
var section = (Section)XamlReader.Parse(headerFooter, _pd.ParserContext);
var flowDoc = new FlowDocument();
flowDoc.Blocks.Add(section);
var richtextbox = new RichTextBox { Width = double.MaxValue, Document = flowDoc };
var viewbox = new Viewbox { Child = richtextbox };
viewbox.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
viewbox.Arrange(new Rect(viewbox.DesiredSize));
var size = new Size() { Height = viewbox.ActualHeight, Width = viewbox.ActualWidth };
return size.Height;
}
Run Code Online (Sandbox Code Playgroud)