type
TDeviceCaps =
record
Name: WideString;
Index: Integer;
Width,
Height,
DPIX,
DPIY,
OffsetX,
OffsetY: Integer;
PhysicalWidth,
PhysicalHeight: Integer;
PixelWidth,
PixelHeight: Real;
Orientation: TPrinterOrientation;
end;
function GetPrinterCaps: TDeviceCaps;
var
DC: THandle;
Caps: TDeviceCaps;
begin
DC := Printer.Handle;
Caps.Orientation := Printer.Orientation;
Caps.
Index := Printer.PrinterIndex;
Caps.
Name := Printer.Printers[Caps.
Index];
Caps.Width := GetDeviceCaps(
DC, HorzSize);
// mm
Caps.Height := GetDeviceCaps(
DC, VertSize);
// mm
Caps.DPIX := GetDeviceCaps(
DC, LogPixelSX);
// DPI
Caps.DPIY := GetDeviceCaps(
DC, LogPixelSY);
// DPI
Caps.OffsetX := GetDeviceCaps(
DC, PhysicalOffsetX);
// Pixel
Caps.OffsetY := GetDeviceCaps(
DC, PhysicalOffsetY);
// Pixel
Caps.PhysicalWidth := GetDeviceCaps(
DC, PhysicalWidth);
// Pixel
Caps.PhysicalHeight := GetDeviceCaps(
DC, PhysicalHeight);
// Pixel
Caps.PixelWidth := (Caps.Width / Printer.PageWidth);
Caps.PixelHeight := (Caps.Height / Printer.PageHeight);
Result := Caps;
end;
function GetPageRect(
const Width, Height: Integer;
const Caps: TDeviceCaps; Center: Boolean = True): TRect;
begin
Result.Left := -Caps.OffsetX;
Result.Top := -Caps.OffsetY;
Result.Right := Result.Left + Round(Width / Caps.PixelWidth);
Result.Bottom := Result.Top + Round(Height / Caps.PixelHeight);
if Center
then
begin
if Result.Right < Caps.PhysicalWidth
then
begin
Result.Left := (Caps.PhysicalWidth - Result.Right)
div 2;
Inc(Result.Right, Result.Left);
end;
if Result.Bottom < Caps.PhysicalHeight
then
begin
Result.Top := (Caps.PhysicalHeight - Result.Bottom)
div 2;
Inc(Result.Bottom, Result.Top);
end;
end;
end;
procedure TForm1.Print;
var
R: TRect;
Bounds: TGPRectF;
Graphics: TGPGraphics;
Caps: TDeviceCaps;
begin
Caps := GetPrinterCaps;
R := GetPageRect(Round(SVG.Width), Round(SVG.Height), Caps, False);
Printer.BeginDoc;
Bounds.X := R.Left;
Bounds.Y := R.Top;
Bounds.Width := R.Right - R.Left;
Bounds.Height := R.Bottom - R.Top;
Graphics := TGPGraphics.Create(Printer.Canvas.Handle);
try
Graphics.SetPageUnit(UnitPixel);
Graphics.SetSmoothingMode(SmoothingModeAntiAlias);
SVG.PaintTo(Graphics, Bounds,
nil, 0);
finally
Graphics.Free;
end;
Printer.EndDoc;
end;