计算段落的负垂直偏移,使图像浮动在锚点上方? [英] Calculate negative vertical offset from paragraph so image floats just above anchor?

查看:35
本文介绍了计算段落的负垂直偏移,使图像浮动在锚点上方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MS Word 函数来简化浮动图像布局.考虑有一个普通的单列文本页并且图像浮动在右侧的情况.当我们想要段落下方的图像时,这很简单"(删除了错误处理):

I'm working on an MS Word function to simplify floating image layout.   Consider the case where there's a normal, single column, page of text and the image floats on the right. When we want the image just below the paragraph, that's 'easy' (error handling removed):

Dim myShape As Shape
Set myShape = Selection.ShapeRange(1)
With myShape
    .WrapFormat.Type = wdWrapSquare
    .RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
    .Top = 0
    .RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
    .Left = wdShapeRight
End With

这通常就是所需要的.但是,当锚点靠近页面末尾并且会使图像低于页面底部时,这可能会导致页面底部出现难看的空白区域:

That's usually all that's needed. But when the anchor is near the end of a page and that would make the image go below the bottom of the page, that can cause an ugly blank space at the bottom of the page:

我想避免移动锚点.相反,将图像放在锚点上方而不是下方通常就足够了.但我不知道如何在代码中做到这一点.在此示例中,图像高度为 2".但如果我将相对垂直位置"设置为 -2",图像会浮动约半英寸高:

I want to avoid moving the anchor. Instead, it's often enough to put the image just above the anchor instead of just below it. But I cannot work out how to do that in code. In this example, the image height is 2". But if I set the 'relative vertical position' to -2", the image floats about half an inch too high:

我应该将图像的相对垂直位置设置为什么值以浮动在锚点上方?

To what value should I set the relative vertical position for the image to float just above the anchor?

附言规则在 http://www.tug.org/TUGboat/tb35-3/tb111mitt-float.pdf,第一步是文本框中的图像https://www.securedevelopment.org/2019/08/18/three-powerful-techniques-to-position-images-and-字中表/ .如果有兴趣,我会开源函数和示例.

P.s. the rules are in http://www.tug.org/TUGboat/tb35-3/tb111mitt-float.pdf and the first step is images in textboxes https://www.securedevelopment.org/2019/08/18/three-powerful-techniques-to-position-images-and-tables-in-word/ . If there's interest I'll open source the functions and examples.

推荐答案

非常感谢,@yokki.这是我想出的.似乎够快了.它假定段落间距是一致的,并且形状不在页面顶部附近.注意:这不是一个完整的答案,因为图像不会随段落一起移动.

Many thanks, @yokki. This is what I've come up with. It seems fast enough. It assumes that the para spacing is consistent and that the shape is not near the top of the page. Note: this is not a complete answer, as the image doesn't move with the paragraph afterwards.

Dim myShape As Shape
Set myShape = Selection.ShapeRange(1)
With myShape
    .WrapFormat.Type = wdWrapSquare
    .RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
    .Left = wdShapeRight
    Set AnchorParagraph = .Anchor.Paragraphs(1)
    ParaSpacing = AnchorParagraph.SpaceAfter
    Set AnchorParagraph = .Anchor.Paragraphs(1)
    ParaSpacing = AnchorParagraph.SpaceAfter
    .RelativeVerticalPosition = wdRelativeVerticalPositionPage
    .Top = AnchorParagraph.Range.Information(wdVerticalPositionRelativeToPage) - .Height - ParaSpacing
    Do Until .Top + .Height + 1 + ParaSpacing > AnchorParagraph.Range.Information(wdVerticalPositionRelativeToPage)
        .IncrementTop (1)
    Loop
End With

为了获得适当的浮动,我找到了另一种解决方案:使用相对于线居中的垂直定位.

To get proper floating, I've found another solution: to use vertical positioning centered relative to line.

这篇关于计算段落的负垂直偏移,使图像浮动在锚点上方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆