Oh, OK. It is much easier with layers. Please try this method:
// Supports TImage, TShape, TLabel, and TMemo
// OutputTextAsImg: Useful to support non-ANSI character sets and custom fonts
function TForm1.OutputPageWithControls(PdfBuilder: TIEPDFBuilder;
Page: TPanel;
PaperSize: TIOPDFPaperSize;
Compression: TIOPDFCompression;
OutputTextAsImg: Boolean): Integer;
const
Rounding_Error_Prevention = 1.05;
Text_Margin = 2;
var
bmp: TIEBitmap;
borderWidth: Integer;
bw: Integer;
destShape: TIEShape;
I: Integer;
pageIndex: integer;
paperSizePts: TPoint;
objScale: Double;
lbl: TLabel;
mem: TMemo;
img: TImage;
rct: TRect;
shp: TShape;
begin
pageIndex := PdfBuilder.AddPage( PaperSize );
paperSizePts := IEPaperSizeToPoints( PaperSize );
objScale := dmin(( paperSizePts.X - 2 * PDF_Page_Margin ) / Page.Width,
( paperSizePts.Y - 2 * PDF_Page_Margin ) / Page.Height );
for I := 0 to Page.ControlCount - 1 do
if Page.Controls[I].Visible then
begin
// TLABEL
if Page.Controls[I] is TLabel then
begin
lbl := TLabel( Page.Controls[I] );
if OutputTextAsImg AND lbl.Transparent then
begin
// Convert text to image
bmp := TIEBitmap.Create( lbl.Width, lbl.Height, ie32RGB ); // Use 32bit for RGBA support
bmp.IECanvas.TextRendering := ietrTextRenderingHintAntiAlias;
bmp.Fill( clWhite );
bmp.AlphaChannel.Fill(0);
bmp.IECanvas.SetCompositingMode(ieCompositingModeSourceOver, ieCompositingQualityDefault);
bmp.IECanvas.Font.Assign( lbl.Font );
bmp.IECanvas.DrawText( lbl.Caption, Rect( 0, 0, bmp.Width, bmp.Height ));
bmp.SynchronizeRGBA( True, True );
PdfBuilder.AddImage( Round( PDF_Page_Margin + objScale * lbl.Left ),
Round( PDF_Page_Margin + objScale * lbl.Top ),
Round( objScale * lbl.Width ),
Round( objScale * lbl.Height ),
bmp,
0, 1.0,
Compression );
bmp.Free;
end
else
if OutputTextAsImg then
begin
// Convert text to image
bmp := TIEBitmap.Create( lbl.Width, lbl.Height );
bmp.Fill( clWhite );
bmp.IECanvas.Font.Assign( lbl.Font );
bmp.IECanvas.DrawText( lbl.Caption, Rect( 0, 0, bmp.Width, bmp.Height ));
PdfBuilder.AddImage( Round( PDF_Page_Margin + objScale * lbl.Left ),
Round( PDF_Page_Margin + objScale * lbl.Top ),
Round( objScale * lbl.Width ),
Round( objScale * lbl.Height ),
bmp,
0, 1.0,
Compression );
bmp.Free;
end
else
begin
// Output text
PdfBuilder.AddText( Rect( Round( PDF_Page_Margin + objScale * lbl.Left ),
Round( PDF_Page_Margin + objScale * lbl.Top ),
Round( PDF_Page_Margin + objScale * ( lbl.Left + lbl.Width * Rounding_Error_Prevention )),
Round( PDF_Page_Margin + objScale * ( lbl.Top + lbl.Height * Rounding_Error_Prevention ))),
AnsiString( lbl.Caption ),
objScale * lbl.Font.Size * lbl.Font.PixelsPerInch / 72,
lbl.Font );
end;
end
// TMEMO
ELSE
if Page.Controls[I] is TMemo then
begin
mem := TMemo( Page.Controls[I] );
if OutputTextAsImg then
begin
// Convert text to image
bmp := TIEBitmap.Create( mem.Width, mem.Height );
bmp.IECanvas.Brush.Color := mem.Color;
bmp.IECanvas.Pen.Color := clBlack;
if mem.BorderStyle <> bsNone then
bmp.IECanvas.Rectangle( 0, 0, mem.Width, mem.Height )
else
bmp.Fill( mem.Color );
bmp.IECanvas.Font.Assign( mem.Font );
bmp.IECanvas.DrawText( mem.Text, Rect( Text_Margin, Text_Margin, bmp.Width - Text_Margin, bmp.Height - Text_Margin ));
PdfBuilder.AddImage( Round( PDF_Page_Margin + objScale * mem.Left ),
Round( PDF_Page_Margin + objScale * mem.Top ),
Round( objScale * mem.Width ),
Round( objScale * mem.Height ),
bmp,
0, 1.0,
Compression );
bmp.Free;
end
else
begin
// Output text
// Background
bw := 0;
if mem.BorderStyle <> bsNone then
bw := 1;
PdfBuilder.AddShape( PDF_Page_Margin + objScale * mem.Left,
PDF_Page_Margin + objScale * mem.Top,
PDF_Page_Margin + objScale * ( mem.Left + mem.Width ),
PDF_Page_Margin + objScale * ( mem.Top + mem.Height ),
iesRectangle,
0,
clBlack,
objScale * bw,
1.0,
True,
mem.Color,
1.0 );
// Text
PdfBuilder.AddText( Rect( Round( PDF_Page_Margin + objScale * ( mem.Left + 2 * bw )),
Round( PDF_Page_Margin + objScale * ( mem.Top + 2 * bw )),
Round( PDF_Page_Margin + objScale * ( mem.Left + mem.Width - 2 * bw )),
Round( PDF_Page_Margin + objScale * ( mem.Top + mem.Height - 2 * bw ))),
AnsiString( mem.Text ),
objScale * mem.Font.Size * mem.Font.PixelsPerInch / 72,
mem.Font );
end;
end
ELSE
// TTIMAGE
if Page.Controls[I] is TImage then
begin
img := TImage( Page.Controls[I] );
bmp := TIEBitmap.create( img.Picture.Bitmap );
PdfBuilder.AddImage( Round( PDF_Page_Margin + objScale * img.Left ),
Round( PDF_Page_Margin + objScale * img.Top ),
Round( objScale * img.Width ),
Round( objScale * img.Height ),
bmp,
0, 1.0,
Compression );
bmp.Free;
end
ELSE
if Page.Controls[I] is TShape then
begin
shp := TShape( Page.Controls[I] );
rct := Rect( Round( PDF_Page_Margin + objScale * shp.Left ),
Round( PDF_Page_Margin + objScale * shp.Top ),
Round( PDF_Page_Margin + objScale * ( shp.Left + shp.Width * Rounding_Error_Prevention )),
Round( PDF_Page_Margin + objScale * ( shp.Top + shp.Height * Rounding_Error_Prevention )));
if shp.Shape in [ stCircle, stSquare, stRoundSquare ] then
// Make Aspect Ratio 1:1
rct := GetImageRectWithinArea( 1, 1, rct );
if shp.Shape in [ stCircle, stEllipse ] then
destShape := iesEllipse
else
if shp.Shape in [ stRoundRect, stRoundSquare ] then
destShape := iesRoundRect
else
destShape := iesRectangle;
borderWidth := 0;
if shp.Pen.Style <> psClear then
borderWidth := shp.Pen.Width;
PdfBuilder.AddShape( rct.Left, rct.Top, rct.Right, rct.Bottom,
destShape, 0, shp.Pen.Color, borderWidth, 1.0,
shp.Brush.Style <> bsClear, shp.Brush.Color, 1.0 );
end
end;
result := pageIndex;
end;
Nigel
Xequte Software
www.imageen.com