Sunday, November 13, 2011

Updating Paragraph Format Properties Using Property List

Once you have created a paragraph format (or other named object), you very likely will want to modify the default properties automatically set when the object was created. You could do so one at a time but this can be tedious. It can also be hard to figure out just the right values for each property.

If your new format is similar to that of an existing format, you can use the properties of the existing format as a starting point. You can then explicitly set only those properties that are different.

This example bases a new (or existing) format on an existing paragraph format. It changes only the underlining property.

This script relies on the GetProps() and SetProps() methods which work with the properties of an object as a group.

NOTE: You can base the properties of a new paragraph format on those of a paragraph. This is the case even though the sets of properties do not exactly line up. Any mismatches are ignored.

The following function was called with:
createPgfFmt(doc, "Heading0", "Heading1")

/* Create or update a paragraph format with the name "newName". Set the format properties based on those of the "basedOnName" format. Change the underlining to be single.
function createPgfFmt(doc, newName, basedOnName)
//determine if the format already exists
var newPgfFmt = doc.GetNamedObject(Constants.FO_PgfFmt, newName);
if (!newPgfFmt.ObjectValid()) {
//create it if necessary
var newPgfFmt = doc.NewNamedObject(Constants.FO_PgfFmt, newName);
if (!newPgfFmt.ObjectValid())
//get the based on format
var existingPgfFmt = doc.GetNamedObject(Constants.FO_PgfFmt, basedOnName);
if (!existingPgfFmt.ObjectValid())
return (0);
//Get all properties of existing format
var props = existingPgfFmt.GetProps();
//find underlining in the array of properties
var index = GetPropIndex(props, Constants.FP_Underlining);
//update the underling value
props[index].propVal.ival = Constants.FV_CB_SINGLE_UNDERLINE;
//update the new format's properties to match the old, save for underlining
newPgfFmt.SetProps (props);

No comments:

Post a Comment