Set Attributes in code (unit WPCreateDemoText.pas)

<< Click to Display Table of Contents >>

Navigation:  Programming > Create text under program control > Low level text creation >

Set Attributes in code (unit WPCreateDemoText.pas)

WPTools can be perfectly used to create text and tables in code. This text can be saved in RTF or WPTOOLS format, printed and (with wPDF) exported to PDF.

 

In unit WPCreateDemoText you will find a procedure which creates text with different character and paragraph attributes. We have included this procedure in this chapter and inserted screenshots of the output which is created by each part of the code.

 

 

If your program manipulates the text by direct access to the TParagraph objects it is required to call ReformatAll before the change becomes visible.

 

 

In this demo we wanted to show how to use the low level methods. You can of course also create text using the procedure InputString and modify the current writing mode using the interface 'CurrAttr' - the demo GridMode uses the InputString technology. But this demo creates the text a bit differently. Please don't read on if you find this confusing. Using the AttrHelper interface ist usually much easier.

 

First we need a buffer for our character attributes:

 ca : TWPCharAttr;

 

Then we create the paragraph:

 par := WPRichText1.ActiveText.AppendPar;

 

The buffer is initialized with

 FillChar(ca, SizeOf(ca), 0);  

 

Now we fill the paragraph by appending text.

 

 pos := par.Insert(0,'4) DEFAULT "FIRST" ',0);

 par.RTFProps.AttrInterface.SetCharStyles(ca, WPSTY_ITALIC, WPSTY_ITALIC);

 par.RTFProps.AttrInterface.SetFontSize(ca, 12);

 pos := par.Insert(pos,'12ptITALIC ',ca);

 

"pos" is an integer value which is the index into the paragraph. The insert() function always returns the position after the inserted text, so pos is always incremented.

 

The lines with par.RTFProps.AttrInterface.... are used to modify ca - which in the end will be used as parameter to insert().

 

AttrInterface is an object of class TWPCharAttrInterface. This class is not used to store attributes but to modify buffer variables of type TWPCharAttr!

 

The procedure starts with the variable declaration. Most important is par: TParagraph which is used as reference to the current paragraph while the text is created. Each new paragraph (or table) is created after this paragraph and the reference is changed to this new object.

 

cha: TWPStoredCharAttrInterface is used to create character attribute index values which are then used in par.SetText and par.Append methods.

 

 

procedure CreateDemoText(RTFMemo : TWPCustomRtfEdit; Mode : TWPCreateDemoText);

var par: TParagraph; // The current paragraph or table

 row, cell: TParagraph; // the current rows and cells

 cha: TWPStoredCharAttrInterface;

 HeadlineA, TextA, RedTextA: Cardinal;

clip0113

procedure CreateEmptyPar;

begin

   par := par.AppendNewPar(true);

   par.LoadedCharAttr := TextA; // Default attribute for empty paragraphs

end;

begin

 

clip0114

 

 cha := RTFMemo.AttrHelper;

 cha.Clear;

 cha.SetFontName('Times New Roman');

 cha.SetFontSize(12);

 HeadlineA := cha.CharAttr;

 

 cha.Clear;

 cha.SetFontName('Arial');

 cha.SetFontSize(11);

 TextA := cha.CharAttr;

 

 cha.SetColor(clRed);

 RedTextA := cha.CharAttr;

 

clip0115

 

if Mode=wpNewText then // Clear and set page size and margins

begin

 RTFMemo.Clear;

 

 RTFMemo.Header.SetPageWH(

   WPCentimeterToTwips(15),

   WPCentimeterToTwips(29.7),

   WPCentimeterToTwips(1.5),

   WPCentimeterToTwips(1.5),

   WPCentimeterToTwips(1.5),

   WPCentimeterToTwips(1.5)

   );

 RTFMemo.CheckHasBody;

 par := RTFMemo.FirstPar;

end

else if Mode=wpInsertText then // insert at current position

begin

   par := RTFMemo.InsertPar;

end

else if Mode=wpAppendText then // append at end of text

begin

   par := RTFMemo.ActiveText.LastPar;

   CreateEmptyPar;

end;

 

clip0068

 

// Set the text of the first paragraph

 par.SetText('A paragraph with a green background:', HeadlineA);

 

// append a new, clean paragraph and set attributes

 par := par.AppendNewPar(true);

 par.ASetColor(WPAT_BGColor, clGreen);

 par.ASet(WPAT_ShadingValue, 30);

 par.SetText('This is 30 % green', TextA);

 

 CreateEmptyPar;

 

Please see reference for supported WPAT_ codes.

 

 

clip0069

 

 par := par.AppendNewPar(true);

 par.SetText('A paragraph with a red background and borders:', HeadlineA);

// append a new, clean paragraph and set attributes

 par := par.AppendNewPar(true);

// Indents

 par.ASet(WPAT_IndentLeft, WPCentimeterToTwips(2));

 par.ASet(WPAT_IndentRight, WPCentimeterToTwips(2));

// Border

 par.ASet(WPAT_BorderFlags,

   WPBRD_DRAW_Left + WPBRD_DRAW_Right + WPBRD_DRAW_Top + WPBRD_DRAW_Bottom); // = WPBRD_DRAW_All4

 par.ASet(WPAT_BorderWidth, WPCentimeterToTwips(0.05)); // 0,5 mm

 

 par.ASetColor(WPAT_BGColor, clRed);

 par.ASet(WPAT_ShadingValue, 50);

 par.SetText('This is 50 % red, indented', TextA);

 CreateEmptyPar;

 

clip0070

 

 par := par.AppendNewPar(true);

 par.SetText('A paragraph with colored borders and spacing', HeadlineA);

 par := par.AppendNewPar(true);

 par.ASet(WPAT_SpaceBefore, WPCentimeterToTwips(0.1)); // 1 mm  padding

 par.ASet(WPAT_SpaceAfter, WPCentimeterToTwips(0.1)); // 1 mm  padding

 par.ASet(WPAT_IndentLeft, WPCentimeterToTwips(0.1)); // 1 mm  padding

 par.ASet(WPAT_SpaceBefore, WPCentimeterToTwips(0.1)); // 1 mm  padding

 

 par.ASet(WPAT_BorderFlags, WPBRD_DRAW_All4); // all lines

 par.ASet(WPAT_BorderWidth, WPCentimeterToTwips(0.05)); // 0,5 mm

 par.ASetColor(WPAT_BorderColorL, clRed); // Left

 par.ASetColor(WPAT_BorderColorT, clGreen); // Top

 par.ASetColor(WPAT_BorderColorR, clBlue); // Right

 par.ASetColor(WPAT_BorderColorB, clYellow); // Bottom

 par.SetText('Red, Green, Blue, Yellow', TextA);

 CreateEmptyPar;

 

clip0071

 

 par := par.AppendNewPar(true);

 par.SetText('A paragraph with tabstops', HeadlineA);

 par := par.AppendNewPar(true);

 par.TabstopAdd(WPCentimeterToTwips(3), tkLeft);

 par.TabstopAdd(WPCentimeterToTwips(9), tkRight, tkDots);

 par.TabstopAdd(WPCentimeterToTwips(12), tkRight, tkNoFill);

 

 par.Append('START', RedTextA);

 par.Append(#9 + 'from' + #9 + 'to' + #9, TextA);

 par.Append('END', RedTextA);

 CreateEmptyPar;

 

clip0072

 // CREATE A HORIZONTAL LINE

 CreateEmptyPar;

with par.AppendNewObject(wpobjHorizontalLine) do

begin

   IParam := clGreen;

  // Width  := WPCentimeterToTwips(10);

   CParam := -WPCentimeterToTwips(1);

end;

 CreateEmptyPar;

 

 horizontal lines can use the following parameters:

Width:        width in twips, if 0 the printable area is used

CParam:the offset to the margins of the printable area. Native values are possible

IParam:        the color as RGB value

Height:        the height in twips

 

 Note: AppendNewObject() and AppendNewObjectPair() can also be used to create hyperlinks.

clip0073

 

 par := par.AppendNewPar(true);

 par.SetText('A table with colored borders', HeadlineA);

 

 par := par.AppendNewTable; // a new table

 par.ASet(WPAT_BoxWidth, WPCentimeterToTwips(9)); // 9 cm wide

 par.ASet(WPAT_BoxMarginLeft, WPCentimeterToTwips(2)); // 2 cm from left margin

 

 row := par.AppendNewRow(true); // the first row

 row.ASet(WPAT_PaddingAll, WPCentimeterToTwips(0.2)); // 2 mm padding

 

 cell := row.AppendNewCell;

 pacell.ASet(WPAT_BorderFlags, WPBRD_DRAW_All4); // all lines

 cell.ASet(WPAT_BorderWidth, WPCentimeterToTwips(0.05)); // 0,5 mm

 cell.ASetColor(WPAT_BorderColorL, clRed); // Left

 cell.ASetColor(WPAT_BorderColorT, clGreen); // Top

 cell.ASetColor(WPAT_BorderColorR, clBlue); // Right

 cell.ASetColor(WPAT_BorderColorB, clYellow); // Bottom

 cell.SetText('Cell A', RedTextA);

 cell.ASet(WPAT_COLWIDTH, WPCentimeterToTwips(3)); // 3 cm wide

 

 cell := cell.AppendNewCell(false); // new cell, same colors

 cell.ASet(WPAT_COLWIDTH, WPCentimeterToTwips(3)); // 3 cm wide

 cell.SetText('Cell B', RedTextA);

 

 cell := cell.AppendNewCell(false); // new cell, same colors

 cell.ASet(WPAT_COLWIDTH, WPCentimeterToTwips(3)); // 3 cm wide

 cell.SetText('Line 1', TextA);

 cell.AppendNewPar(true).SetText('Line 2',TextA);

 

// and a second row

 row := row.AppendNewRow(true); // the first row

 cell := row.AppendNewCell;

 cell.ASet(WPAT_BorderFlags, WPBRD_DRAW_All4);

 cell.ASet(WPAT_COLWIDTH, WPCentimeterToTwips(9));

 cell.SetText('second cell', HeadlineA);

 

clip0116

 

 RTFMemo.DelayedReformat;

end;