WPCubed Product Support

How to generate chapter names in the header section?

Hello,

I want to automatically display the appropriate chapter name in the header section of a page. For example: Usually a book consists of number of chapters, the reader wants to know, in which chapter he is and looks at the header to find the current chapter. The chapter name is repeated in the header section on each page, until a new chapter begins.

What is the best way to implement this? I thought of generating a bookmark at the beginning of each chapter. In the OnGetSpecialText event I search from the current page back, until a page with a bookmark is found, then I set the header with the chapter-text belonging to the bookmark.

Is there a better idea, or an already existing solution/sample code to this?

Thanks

#1 ♥ 0
6.2k Views 2 users read 3 min
support

Hi,

your approach souds ok - but make sure you do not modify an existing header but create a new header for each changed text.

Probably easier ir to use textfield objects, but those are one line only:

Create headline object:
WPRichText1.InputTextFieldName('HEADLINE');

Mark paragraph to be a headline
WPRichText1.ActiveParagraph.ASet(WPAT_ParIsOutline,1)

Use event OnTextObjGetTextEx to paint the object

procedure TWPTBXForm.WPRichText1TextObjGetTextEx(RefCanvas: TCanvas;
  TXTObject: TWPTextObj; var PrintString: WideString; var WidthInPix,
  HeightInPix: Integer; var PaintObject: TWPTextObj; Xres, YRes: Integer);
var i: Integer;
  linedata: TWPVirtPageImageLineRef;
  par: TParagraph;
begin
  if TXTObject.Name = 'HEADLINE' then
  begin
    PrintString := '                                                        ';

    i := WPRichText1.BodyText.Pages[WPRichText1.Memo._CurrPaintedObjPageNumber].FindFirstTextLine;
    if i >= 0 then
    begin
      WPRichText1.BodyText.Pages[WPRichText1.Memo._CurrPaintedObjPageNumber].GetLine(i, lineData);
      par := lineData.par;
      while par <> nil do
      begin
        if par.AGet(WPAT_ParIsOutline, i) and (i > 0) then
        begin
          PrintString := Copy(par.GetText(false, true) +
            PrintString, 1, 80);
          break;
        end;
        par := par.prev;
      end;
    end;
  end;
end;
#2 ♥ 0

Hi,

I took your solution and it works fine! My problem now is, that the preview does not display the headlines (The TextObjGetTextEx handler is not called, while displaying the preview, but it should?). They appear only on the created PDF.
Maybe you know the reason. I continue to investigate...

Thank you!
Mike

#3 ♥ 0
support

Hi,

in a later release the preview DLG initailizes the WPPreview component with WPPreviewForm.WPPreView1.AssignPrintProperties(Editor);

AssignPrintProperties also assigns the event OnTextObjGetTextEx.

Julian

#4 ♥ 0

Hi,

Thanks for the tip. I took AssignPrintProperties, and the OnTextObjGetTextEx event is called. Problem now: The headline always shows the same chapter headline! (Actually the name of the last chapter).

When debugging I noticed that _CurrPaintedObjPageNumber was always the number of the last page, no matter what page is displayed in the preview.

i := BodyText.Pages[Memo._CurrPaintedObjPageNumber].FindFirstTextLine;

Any idea?

#5 ♥ 0
support

Hi,

You need to use the correct 'Memo' - that is unfortunately not so easy since the event does not provide a 'Sender' parameter. In the current release I see no way to overcome this. In next release the 'ParentRTFPage' property will work, currently it was only used for certain images. The code can be then written as

var i: Integer;
  linedata: TWPVirtPageImageLineRef;
  par: TParagraph;
begin
  if (TXTObject.Name = 'HEADLINE') then
  begin
    PrintString := '                                                       ';
    if (TXTObject.ParentRTFPage >= 0) and (TXTObject.ParentRTFPage<WPRichText1.BodyText.PageCount) then
    begin
      i := WPRichText1.BodyText.Pages[TXTObject.ParentRTFPage].FindFirstTextLine;
      if i >= 0 then
      begin
        WPRichText1.BodyText.Pages[TXTObject.ParentRTFPage].GetLine(i, lineData);
        par := lineData.par;
        while par <> nil do
        begin
          if par.AGet(WPAT_ParIsOutline, i) and (i > 0) then
          begin
            PrintString := Copy(par.GetText(false, true) +
              PrintString, 1, 80);
            break;
          end;
          par := par.prev;
        end;
      end;
    end;
  end;
end;

Julian

#6 ♥ 0
This category is archived. No new posts are possible.