Word Automation:检测是否需要分页?

发布于 2024-07-19 03:17:32 字数 469 浏览 2 评论 0原文

我正在开发一个 C# 项目,该项目将使用 Word Automation API 生成 Word 文档。

我想在生成的文档中的特定点插入分页符,并且我目前正在使用以下代码成功完成此操作:

// Generate page break
object pageBreak = WdBreakType.wdPageBreak;
wordApp.Selection.InsertBreak(ref pageBreak);

但是,如果文档在上一页上的空间耗尽后自然地换行到下一页,那么我我真的不想生成分页符,否则我最终会得到一个空白页。

我真正想要的是能够准确地找出光标所在的位置,以及它是否位于当前页面的第一行和第一列,那么我可以放心地假设没有必要插入分页符。

有没有办法访问光标的位置? 或者另一个可以做同样事情的解决方案? 这似乎是一个简单的要求,所以如果我错过了显而易见的事情,我提前道歉。

I am working on a project in C# that will produce a Word document using the Word Automation API.

I would like to insert page breaks at specific points in the generated document and I am currently doing this successfully with the following code:

// Generate page break
object pageBreak = WdBreakType.wdPageBreak;
wordApp.Selection.InsertBreak(ref pageBreak);

However, if the document has naturally wrapped onto the next page anyway after running out of room on the previous page then I don't really want to be generating a page break or else I will end up with a blank page.

What I would really like is the ability to find out exactly where the cursor is and if it is on the first line and column of the current page then I can safely assume that it is not necessary to insert a page break.

Is there a way to access the position of the cursor? Or another solution that would do the same thing? It seems like a simple requirement so I apologize in advance if I have missed the obvious.

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

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

发布评论

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

评论(2

私野 2024-07-26 03:17:32

假设您以编程方式构建文档,使 wordApp.Selection 正确反映您在文档中的实际(和相关)位置,您可以使用其 Information 属性和以下两个来确定其起始页上的行和列WdInformation 枚举(此处显示为 VBA;不确定 .NET PIA 语法是什么):

line = wordApp.Selection.Information(wdFirstCharacterLineNumber)
col = wordApp.Selection.Information(wdFirstCharacterColumnNumber)

这些值对应于 Word 状态栏中看到的 Ln 和 Col 值。 如果它们都等于 1,则您位于选择开始的页面的第一个位置。

祝你好运!

Assuming that you are programmatically building the document in a way that would cause wordApp.Selection to properly reflect your actual (and relevant) position in the document, you can determine its line and column on its starting page using its Information property and the following two WdInformation enums (shown here as VBA; not sure what the .NET PIA syntax is offhand):

line = wordApp.Selection.Information(wdFirstCharacterLineNumber)
col = wordApp.Selection.Information(wdFirstCharacterColumnNumber)

These values correspond to the Ln and Col values seen in Word's status bar. If they are both equal to 1, then you are in the first position of the page on which the Selection begins.

Good luck!

绮烟 2024-07-26 03:17:32

您可以尝试在段落上设置属性,使其前面有一个分页符。 我不知道 Word Automation API 是否允许设置它,但这似乎正是您所需要的。

You could try setting property on the paragraph that causes it to have a page break before it. I don't if Word Automation API allows it to be set, but it seems exactly what you need.

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