Create Sections

Top  Previous  Next

 

Sections make it possible to use many different header and footer texts and different page sizes in one document.

 

The editor will display an arrow clip0112 in the left margin where a new section starts.

 

The following code can be used to create a new section:

 

var sectionprops : TWPRTFSectionProps;

begin

// New Page

WPRichText1.InputString(#12);

// New section properties

sectionprops := WPRichText1.ActiveParagraph.StartNewSection;

// Now we can do something with sectionprops

...

end;

 

Each section is defined by an instance of TWPRTFSectionProps. The property WPRichText.Header which stores the default page size for the whole document is consequently implemented using a class which inherits from TWPRTFSectionProps.

 

Usually all instances of TWPRTFSectionProps use the property value defined in the master section property (WPRichText.Header). If certain attribute should be unique for a section the property Selected must be set accordingly.

 

sectionprops.Select := [wpsec_PageSize];

sectionprops.Landscape := TRUE;

 

The following flags exist in property Select:

   wpsec_PageSize         --> Overwrite PageWidth, PageHeight and Landscape

   wpsec_Margins         --> Top, left and other margins

   wpsec_TabDefault        --> Change deftabstop property

   wpsec_PageMirror        --> change the marginmirror property

 

Note: The "speed reformat" feature of WPTools 5 does not work well with different page sizes yet. So please switch this feature off using WPAll.FormatOptions := [wpDisableSpeedReformat];

 

It is also possible to select that certain header or footer texts should be only used for one section. This is done by the property TWPRTFDDataBlock.UsedForSectionID. If this property is changed to sectionprops.SectionID the text block will be only used for that special section.

 

The demo AppendAsSection shows how texts from different editors (WP1,WP2, WP3) can be appended to create one multi section document in the editor WPALL:

 

clip0111

 

The 3 buttons use different approaches:

 

1.) Full implementation - shows how to work with sections

 

DELPHI CODE

procedure TWPALL.AppendClick(Sender: TObject);

  procedure AppendText(Source : TWPRTFDataCollection);

  var sectionprops : TWPRTFSectionProps ;

      i   : Integer;

      textblock : TWPRTFDataBlock;

  begin

    // Create a new Page if required

    if WPAll.IsEmpty then

         WPAll.CheckHasBody

    else WPAll.InputString(#12);

    // Create new section properties

    sectionprops := WPALL.HeaderFooter.AddSectionProps;

    // Assign the default page size

    sectionprops.Assign(Source.Header);

    sectionprops.Select := [wpsec_PageSize,wpsec_Margins];

    // Copy all header + footer into certain section

    for i:=0 to Source.Count-1 do

      if Source[i].Kind in [wpHeader, wpFooter] then

      begin

        textblock := WPALL.HeaderFooter.Append(

          Source[i].Kind,

          Source[i].Range,

          Source[i].Name);

        textblock.UsedForSectionID := sectionprops.SectionID;

        textblock.RtfText.Assign(Source[i].RTFText);

      end;

    // The current paragraph starts this section

    WPAll.ActiveParagraph.SectionID := sectionprops.SectionID;

    include(WPAll.ActiveParagraph.prop,paprNewSection);

    // Copy the text as part of a certain section

    WPAll.CPPosition := MaxInt;

    WPAll.SelectionAsString := Source.AsANSIString('WPTOOLS');

  end;

begin

WPAll.Clear;

AppendText(WP1.HeaderFooter);

AppendText(WP2.HeaderFooter);

AppendText(WP3.HeaderFooter);

end;

 

C++BUILDER Example:

This code appends the text from a different editor to "WPRichText1" as a new section

 

void __fastcall TWPALL::AppendNewSection(TWPRTFDataCollection * Source)

{

TWPRTFSectionProps * sectionprops;

TWPRTFDataBlock * textblock ;

int i;

 

// Create a new Page if required

if (this->WPRichText1->IsEmpty())

this->WPRichText1->CheckHasBody();

else this->WPRichText1->InputString("\f",0);

 

// Create new section properties

sectionprops = this->WPRichText1->HeaderFooter->AddSectionProps();

 

// Assign the default page size

sectionprops->Assign(Source->Header);

 

//sectionprops->Select = [wpsec_PageSize,wpsec_Margins];

sectionprops->Select << wpsec_PageSize << wpsec_Margins ;

 

// Copy all header + footer into certain section

for (i=0; i < Source->Count; i++ )

{

TWPRTFDataBlock * src = Source->Items[i] ;

if ( src->Kind == wpHeader || src->Kind == wpFooter )

       {

       textblock = this->WPRichText1->HeaderFooter->Append(

       Source->Items[i]->Kind ,

       Source->Items[i]->Range ,

       Source->Items[i]->Name );

       textblock->UsedForSectionID = sectionprops->SectionID;

       textblock->RtfText->Assign(Source->Items[i]->RtfText);

       }

}//for() 

 

// The current paragraph starts this section

this->WPRichText1->ActiveParagraph->SectionID = sectionprops->SectionID;

 

//include(WPAll->ActiveParagraph->prop,paprNewSection);

this->WPRichText1->ActiveParagraph->prop << paprNewSection ;

 

// Copy the text as part of a certain section

this->WPRichText1->CPPosition = MaxInt;

this->WPRichText1->SelectionAsString = Source->AsANSIString("WPTOOLS");

}

 

 

 

2.) Use utility procedure AppendAsSection - same functionality as 1)

 

procedure TWPALL.Append2Click(Sender: TObject);

begin

WPAll.HeaderFooter.AppendAsSection(WP1.HeaderFooter);

WPAll.HeaderFooter.AppendAsSection(WP2.HeaderFooter);

WPAll.HeaderFooter.AppendAsSection(WP3.HeaderFooter);

end;

 

 

 

3) Use strings in WPTOOLS format with the <newsection/> tag.

This technique allows it to create a multi section text by simply appending strings or streams without loading the text into an editor. This shows how versatile the WPTOOLS format can be used:

 

procedure TWPALL.AppendWithStringsClick(Sender: TObject);

begin

WPAll.AsString := '<newsection/>' + WP1.AsANSIString('WPTOOLS')

  +'<newsection/>' + WP2.AsANSIString('WPTOOLS')

  +'<newsection/>' + WP3.AsANSIString('WPTOOLS');

// Using <newsection pagebreak=0/> no page break will be inserted

end;

 

 

 

4) Append section with individual footer (or header)

 

You can use WPRichText1.ActiveParagraph.StartNewSection to add a new section property object to the current paragraph. The return object will be an object of class TWPRTFSectionProps. In property 'Select' you can change which properties should be used for this section.

 

Using WPRichText1.HeaderFooter.Append you can append a new RTFDataBlock to be used as footer. 'Get' cannot be used, since this will reuse an existing RTFDataBlock.

 

procedure TForm1.PortraitLandscapeClick(Sender: TObject);

var sect : TWPRTFSectionProps;

   footer : TWPRTFDataBlock;

begin

WPRichText1.CPPosition := MaxInt;

Inc(c);

// start a section

sect := WPRichText1.ActiveParagraph.StartNewSection;

sect.Select := [wpsec_PageSize]; // 1.  (in this order!)

sect.Landscape := Sender = Landscape;  // 2.

 

// Now new page and some text

WPRichText1.InputString(#12+#32+IntToStr(c)+#13);

 

// also add special header+footer for this section

footer := WPRichText1.HeaderFooter.Append(wpIsFooter,wpraOnAllPages,'');

footer.UsedForSectionID := sect.SectionID;

 

WPRichText1.ActiveText := footer;

WPRichText1.InputString('Section ' + IntToStr(c) + #9);

WPRichText1.InputTextFieldName('PAGE');

 

WPRichText1.ActiveParagraph.TabstopAdd(

   sect.PageWidth-sect.LeftMargin-sect.RightMargin,

   tkRight,

   tkUnderline );

// select body again

WPRichText1.ActiveText := WPRichText1.BodyText;

end;