开发者

Using Delphi to fill out a table template made using a rich text (rtf) file

开发者 https://www.devze.com 2023-03-22 15:56 出处:网络
I have a table in rtf (rich text format) file format. I wonder if it is possible to place different content into the cells at runtime.

I have a table in rtf (rich text format) file format.

I wonder if it is possible to place different content into the cells at runtime.

Here is a screenshot. Some开发者_运维技巧 blank fields need to be filled in with a value.

Using Delphi to fill out a table template made using a rich text (rtf) file


You wrote "Some blank fields need to be edited" - if this means that the RTF is some kind of template, where empty fields need to be filled in programmatically, here is what I would do:

  • use your text editor to open the RTF document (OpenOffice or MS Word for example)
  • insert special placeholder strings in the empty fields like $field1$, $field2$ ...
  • save the document
  • in your Delphi application, load the RTF into a string (using TStringlist for example)
  • use Delphi String functions to find and replace the special placeholder strings with the actual values


To print a form on a sheet of paper, simply draw on the printer's canvas!

procedure TForm1.Button1Click(Sender: TObject);
var
  y, Margin, Col2: integer;
  LineHeight: integer;
begin
  with TPrintDialog.Create(nil) do
    try
      if not Execute then
        Exit;
    finally
      Free;
    end;

  Printer.BeginDoc;

  Printer.Title := 'Sample Form';
  Printer.Canvas.Font.Name := 'Arial';
  Printer.Canvas.Font.Size := 11;

  Margin := 5*Printer.Canvas.TextWidth('M');
  Col2 := 35*Printer.Canvas.TextWidth('M');
  LineHeight := 3 * Printer.Canvas.TextHeight('M') div 2;

  y := Margin;

  Printer.Canvas.Font.Style := [fsBold];
  Printer.Canvas.TextOut(MARGIN, y, 'Name: ');
  Printer.Canvas.Font.Style := [];
  Printer.Canvas.TextOut(Col2, y, 'Andreas Rejbrand');

  inc(y, LineHeight);

  Printer.Canvas.Font.Style := [fsBold];
  Printer.Canvas.TextOut(MARGIN, y, 'Age: ');
  Printer.Canvas.Font.Style := [];
  Printer.Canvas.TextOut(Col2, y, '23');

  inc(y, LineHeight);

  Printer.Canvas.Font.Style := [fsBold];
  Printer.Canvas.TextOut(MARGIN, y, 'Nationality: ');
  Printer.Canvas.Font.Style := [];
  Printer.Canvas.TextOut(Col2, y, 'Swedish');

  Printer.EndDoc;

end;

The result: http://privat.rejbrand.se/sampledrawing.pdf


To use existing RTF content in a TRichEdit, load it into the Text or Lines properties of the component:

RichEdit1.Lines.LoadFromFile(rtfFilename);

-or-

RichEdit1.Text := StringILoadedFromAnRtfFileOnDisk;

I am not sure what you want to do (programmatically create a or modify a table based on some existing unspecified RTF content, print it, etc). By asking about a rich edit, and also asking about a regular edit, it's very hard to understand.

You asked in a comment, how you can create tables in a TRichEdit:

procedure TForm1.PutTableIntoRichEdit;
begin
  RichEdit1.Text := '{\rtf1\ansi\deff0'#13#10+
'\trowd'#13#10+
'\cellx1000'#13#10+
'\cellx2000'#13#10+
'\cellx3000'#13#10+
'cell 1\intbl\cell'#13#10+
'cell 2\intbl\cell'#13#10+
'cell 3\intbl\cell'#13#10+
'\row'#13#10+
'}' ;
end;

If you want to use that RTF content in the screenshot you showed above, inside your delphi program, just load it up and try something, and ask a specific question. Showing us a screenshot of microsoft word isn't helping your question be clear.

0

精彩评论

暂无评论...
验证码 换一张
取 消