代码文件:
--------------------------------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo; {用于输入要保存的文本}
ComboBox1: TComboBox; {字体}
LabeledEdit1: TLabeledEdit; {字号}
LabeledEdit2: TLabeledEdit; {页面边距}
LabeledEdit3: TLabeledEdit; {行距}
Button1: TButton;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{初始化数据}
procedure TForm1.FormCreate(Sender: TObject);
begin
ComboBox1.Items := Screen.Fonts;
ComboBox1.Text := '宋体';
LabeledEdit1.Text := '12';
LabeledEdit2.Text := '10';
LabeledEdit3.Text := '4';
end;
procedure TForm1.Button1Click(Sender: TObject);
const
FilePath = 'c:\temp\Word.bmp'; {要保存的图片路径}
var
str: string;
bit: TBitmap;
Page_edge: Integer; {页面边距变量}
Word_h,Line_h: Integer; {字体高度和行间距}
w,x,y: Integer; {文本宽度与输出位置}
i: Integer;
begin
str := Memo1.Text;
bit := TBitmap.Create;
bit.Canvas.Font.Name := ComboBox1.Text;
bit.Canvas.Font.Size := StrToIntDef(LabeledEdit1.Text, 9);
Page_edge := StrToIntDef(LabeledEdit2.Text, 0);
Word_h := bit.Canvas.TextHeight('a');
Line_h := StrToIntDef(LabeledEdit3.Text, 0);
{设置页面高度}
bit.Height := (Word_h + Line_h) * Memo1.Lines.Count - Line_h + Page_edge * 2;
w := 0;
x := Page_edge;
y := Page_edge;
for i := 0 to Memo1.Lines.Count - 1 do
begin
{设置页面宽度}
if bit.Canvas.TextWidth(Memo1.Lines[i]) > w then
begin
w := bit.Canvas.TextWidth(Memo1.Lines[i]);
bit.Width := w + Page_edge * 2;
end;
{画文本}
bit.Canvas.TextOut(x, y, Memo1.Lines[i]);
{下一行}
Inc(y, Word_h + Line_h);
end;
{保存为图片}
bit.SaveToFile(FilePath);
bit.Free;
end;
end.