Saturday, October 15, 2011

Counting Paragraphs in the Main Flow

While there may be times when you need to work with all of the paragraphs in a document, it is much more likely that you want to get the paragraphs in a few in the order in reading order. You could, if you wish, get all of the paragraphs in all of the flows in order. This post deals with the most likely scenario: getting all of the paragraphs in the main flow in the order in which they would print.

What is the main flow? The main flow is just like any other named flow in a document. It is the default flow for the language version use. Thus:
  • When a table of contents or index is generated, the generated text is put into the main flow of the the generated document.
  • When the Compare Documents command is run, the summary text is placed in the main flow.
For English documents, the default flow is Flow A. (If there are several autoconnect flows in the document with the default flow tag, the main flow is the one in the backmost text frame on a Right master page.)

The FDK provides a document property that allows plug-in to get the main flow easily: FP_MainFlowInDoc. In ExtendScript, it is thus MainFlowInDoc.

The FirstPgf and LastPgf properties allow scripts to locate the first and last paragraphs in a flow but, oddly enough, these are not flow properties but text frame properties. So to find the start (or end) of this list, you need to get the first text frame within the main flow (or the last text frame if you wish to navigate from bottom to top.) Use the FirstTextFrameInFlow flow  property (or LastTextFrameInFlow flow  property).

Note:  Text frames do not span pages. When a text frame is full, FrameMaker creates a new text frame on the next page. That frame is connected to and remains within the same flow as the original text frame.

To get successive paragraphs, you need not worry about text frame boundaries (assuming they are of no interest to you). You can simply get the next paragraph (or previous) in the flow you are traversing.


var doc = app.ActiveDoc;
var count = 0;

var mainflow = doc.MainFlowInDoc;
var tframe = mainflow.FirstTextFrameInFlow;
var pgf = tframe.FirstPgf;
while (pgf.ObjectValid()) {
    count++;
    pgf = pgf.NextPgfInFlow;
    }

count = count+''; //Concatenate empty string to convert
Alert(count, Constants.FF_ALERT_CONTINUE_NOTE);

Now if I run the script on the empty Portrait template I get the following:


The end of flow marker counts as a paragraph. The paragraphs on the master and reference pages as well as in headers and footers, are no longer counted.

Try it yourself on a longer document with multiple paragraphs of text in the main flow.












No comments:

Post a Comment