字自动化:检测如果分页符是必要的吗? [英] Word Automation: Detect if page break is necessary?

查看:201
本文介绍了字自动化:检测如果分页符是必要的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在C#项目,将使用Word自动化API生成的Word文档。

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

我想插入在生成的文档中特定点分页符和我目前正在与下面code这样成功的:

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);

不过,如果该文档已自然包裹到下一个页面运行的房间previous然后页面上后,反正我真的不希望被生成分页符,否则我将结束与一个空白页。

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.

推荐答案

假设你编程的方式构建的方式的文件,会导致wordApp.Selection正确反映文档中的实际(和相关的)的位置,你可以确定它的行和列的起始页面上使用其信息属性与以下两个WdInformation枚举(此处显示为VBA;不知道的.NET PIA语法是副手):

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)

这些值对应于Word的状态栏上看到的LN和Col值。如果它们都等于1,那么你在其上选择开始页面的第一位置

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.

祝你好运!

这篇关于字自动化:检测如果分页符是必要的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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