<< Click to Display Table of Contents >> Navigation: Programming > Move cursor / select text > Work directly on paragraphs |
The following properties let you read the references to certain paragraphs:
This is the current paragraph. It is also possible to write this property.
property ActiveParagraph: TParagraph;
This is the current position in the current paragraph. It is also possible to write this property.
property ActivePosInPar: Integer;
This function returns the current paragraph. Unlike ActiveParagraph which can retrun nil, if required a paragraph will be created.
function ActivePar: TParagraph;
This is the first paragraph in the text. It will always use the main layer which is usually the body of the text. Writing to that property will replace the text.
property FirstPar: TParagraph;
This is the last paragraph:
property LastPar: TParagraph;
If you need to modify multiple paragraphs in code it is better to create a loop which locates all paragraphs by using the internal pointer tree.
Also see the properties ActiveText, DisplayedText and TWPRTFDataBlock.
This code will create a footer layer with text and page numbering objects:
var par : TParagraph;
rtf : TWPRTFDataBlock;
begin
rtf := WPRichText1.HeaderFooter.Get(wpIsFooter,wpraOnAllPages);
rtf.Clear(true);
par := rtf.AppendNewPar();
par.Append('Page ');
par.AppendTextObject('PAGE');
par.Append('/');
par.AppendTextObject('NUMPAGES');
par.ASet(WPAT_Alignment, Integer(paralCenter));
par.ASet(WPAT_BorderFlags, WPBRD_DRAW_Top);
par.CharAttrModify(WPAT_CharFont, rtf.RTFProps.AddFontName('Courier New'));
par.CharAttrModify(WPAT_CharFontSize, 1200);
par.CharAttrModify(WPAT_CharStyleON, WPSTY_BOLD);
WPRichText1.ReformatAll(true);
end;
The above code uses the function TParagraph.CharAttrModify() which was added to WPTools 8.
Using this new function it is now possible to modify the attributes of text of a TParagraph directly.
This will not change the attributes which are used by all characters, but the "CharAttr" assoziated with each singular character.
procedure TParagraph.CharAttrModify(
WPAT_Code : Integer;
Value : Integer;
ModifyMode : TWPCharAttrModifyMode = wpSetValue;
StartPos : Integer = 0;
Len : Integer = MaxInt );
TWPCharAttrModifyMode = (wpSetValue, wpSetColorValue, wpDelete, wpAddValue, wpSubtractValue {not negative!} , wpOrValue, wpAndNotValue);
This example will insert Quotation marks as used in emails: '>> ....'
var par : TParagraph;
begin
par := WPRichText1.FirstPar;
while par<>nil do
begin
// Do something with par
par.Insert(0,'>> ',0);
// Next paragraph
par := par.next;
end;
// Make sure the text is formatted
WPRichText1.ReformatAll(false, true);
end;
If you only need to work on the top level of paragraphs, which means table objects are manipulated but not entered) use TParagraph.NextPar instead of TParagraph.Next.
// This code puts all text except for tables in 2-column layout. (Requires WPTools Premium)
var par : TParagraph;
begin
par := WPRichText1.FirstPar;
while (par <> nil) do begin
if par.ParagraphType = wpIsTable then
begin
par.ASet(WPAT_COLUMNS, 1);
par.ADel(WPAT_COLUMNS_X);
WPRichText1.Refresh;
end
else
begin
par.ASet(WPAT_COLUMNS, 2);
par.ASet(WPAT_COLUMNS_X,
WPCentimeterToTwips(0.5));
end; // else ...
par := par.NextPar;
end; // while ...
WPRichText1.ReformatAll(true, true);
end;
This code uses thenew API methods which return a reference to the TParagraph object which they are working on.
Since a reference is returned the call to this functions can be simply append after a '.'.
Example - create a footer with numbering:
WPRichText1.HeaderFooter.Get(wpIsFooter, wpraOnAllPages).Clear
.SetProperty(WPAT_Alignment,Integer(paralCenter))
.Append(wpoPageNumber)
.Append('/')
.Append(wpoNumPages);
WPRichText1.ReformatAll();