以编程方式调整字体大小以适合 Word 文本框中的文本

发布于 2024-07-11 06:28:22 字数 1057 浏览 4 评论 0原文

我的应用程序之一涉及 MS Word 和文档创建/编辑/格式化。 我正在使用 Office 2007 w/ VS 2008,并且我正在针对 Microsoft.Office.Interop.Word 库进行编码,该库似乎适用于 2003 或 2008。

我使用 Document.Shapes.AddTextbox 在文档中创建一个文本框方法,然后用文本填充它。 我希望能够以编程方式确定文本是否适合文本框,如果不适合,则减小字体大小,直到适合为止。

我尝试了几种不同的方法:

1)使用 bool Shape.TextFrame.Overflowing 属性

while (textbox.TextFrame.Overflowing) // adjust font size

,但是,即使当我打开文档时,我可以看到文本适合框中,这也会返回 TRUE。

2)检查文本最后一个字符的X/Y位置,并查看该坐标是否落在文本框边界内,

lastCharX = System.Convert.ToSingle (tb.TextFrame.TextRange.Characters.Last.get_Information (WdInformation.wdHorizontalPositionRelativeToPage));
lastCharY = System.Convert.ToSingle (tb.TextFrame.TextRange.Characters.Last.get_Information (WdInformation.wdVerticalPositionRelativeToPage));
bool outsideFrameBoundaries = lastCharX + lastCharWidth > frameBoundaryX || lastCharY + lastCharHeight > frameBoundaryY;

但是,这会返回几乎总是在框内的X/Y,尽管当我打开文档时我可以'看不到该角色,因为它不适合盒子。

所以我在这里没有想法了,我想问是否有其他人以前经历过这个,以及他们是否对处理互操作这个词的不准确混乱有建议?

One of my applications deals with MS Word and Document creation/editing/formatting. I am using Office 2007 w/ VS 2008, and i'm coding against the Microsoft.Office.Interop.Word library, which seems to work with either 2003 or 2008.

I create a Textbox in a Document using the Document.Shapes.AddTextbox method, and then filling it with text. I'd like to be able to programmatically determine whether or not the text fits within the textbox, and if it doesn't, then reduce the font size until it does.

I've tried a couple different methods:

1) using the bool Shape.TextFrame.Overflowing property

while (textbox.TextFrame.Overflowing) // adjust font size

however, this returns TRUE even though when I open the document I can see the text fits in the box.

2) checking the X/Y position of the last character of the text, and seeing if that coordinate falls within the textbox boundaries

lastCharX = System.Convert.ToSingle (tb.TextFrame.TextRange.Characters.Last.get_Information (WdInformation.wdHorizontalPositionRelativeToPage));
lastCharY = System.Convert.ToSingle (tb.TextFrame.TextRange.Characters.Last.get_Information (WdInformation.wdVerticalPositionRelativeToPage));
bool outsideFrameBoundaries = lastCharX + lastCharWidth > frameBoundaryX || lastCharY + lastCharHeight > frameBoundaryY;

however, this returns X/Y that are almost always inside the box, though when I open the document I can't see the character because it doesn't fit in the box.

So I'm running out of ideas here, and i'm asking if anybody else has gone through this before and if they have suggestions for dealing with the inaccurate mess that is the word interop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女皇必胜 2024-07-18 06:28:22

我想出了一个解决方案。

当我找到解决Word疯狂问题的方法时,一切就开始了。 当我获取某个字符的 X/Y 坐标,并且该字符存在于文本框区域之外时,Word 实际上会返回正确的 X 值,但 Y 值是文本框中最后一个可见行的 Y 值。

因此,我从末尾开始扫描所有字符,如果发现重复的坐标,那么我就知道存在溢出。 我还必须检查 Y 值 + 字体大小是否大于文本框的底部边界。 但这对于检测文本框是否溢出似乎非常可靠(如果缓慢)。 一旦确定它是否溢出,我就会不断减小字体大小,直到没有溢出为止。

I came up with a solution.

It started when I figured out a method to Word's madness. When I get the X/Y coordinates for a character, and that character exists outside of the textbox area, then Word actually returns the correct X value but the Y value is the Y value of the last visible line on the textbox.

So I scan all characters starting from the end, and if I find duplicate coordinates then I know that there is overflow. I also have to check if the Y-value + the font size is greater than the bottom bound of the textbox. But this seems to work pretty reliably (if slowly) for detecting if a textbox is overflowing. Once I determine if it's overflowing, then I keep decreasing the font size until it is not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文