Teknisk artikel

PDFlibPas: print preview and device-context output in Delphi

losLab PDF Library ger Delphi- och C++Builder-team en PDF-motor med tillgänglig källkod för skrivbord, server, DLL, ActiveX och Dylib, med inbyggda PDF/A- och PDF/UA-kontroller, PAdES-signering och valbara renderare utan extern PDF-tjänst.

Den här artikeln är skriven för teams building print-preview, hard-copy approval, label, or controlled-output workflows in Delphi. Den behandlar print preview and device-context output som produktionsnära dokumentteknik, inte som ett isolerat komponentanrop.

Den praktiska risken är att print output can differ from preview when device margins, scaling, rotation, duplex settings, and driver behavior are not modeled explicitly. Därför behöver flödet ett skrivet kontrakt, observerbar diagnostik och realistiska regressionsfiler.

Arkitekturbeslut

Separate PDF page geometry from printer geometry. fit, actual size, shrink-only, center, rotation, and crop rules / paper selection, printable margins, tray, duplex, and color mode policy

  • fit, actual size, shrink-only, center, rotation, and crop rules
  • paper selection, printable margins, tray, duplex, and color mode policy
  • preview fidelity requirements compared with printer-driver output
  • whether annotations, form fields, watermarks, and backgrounds are printed

Implementeringsflöde

Preview with the same print contract used for output. Ordningen nedan gör arbetsflödet granskbart för Delphi- och C++Builder-team.

  1. read page size and rotation before asking the printer for device capabilities
  2. compute the target rectangle from paper, printable area, and scaling policy
  3. render preview using the same geometry contract as the print job
  4. record driver and device context details when output is generated
  5. test high-value documents on the printer families customers actually use

Valideringsbevis

Print evidence that helps support reproduce issues. Behåll dessa fält tillsammans med utdata eller supportunderlaget.

  • printer name, driver version, paper size, printable area, scaling, and rotation
  • PDF page box, page number, rendered rectangle, and DPI
  • annotation and form-field print policy
  • operator selection and print result or driver error

A device context is a target, not a neutral canvas

A production print workflow should know the PDF page box, target paper size, printable area, scaling rule, rotation decision, and driver settings before rendering to a device context.

Operational metrics to watch

The first release should expose enough metrics to prove the workflow is healthy under real files, not only under curated samples.

  • count and rate for printer name, driver version, paper size, printable area, scaling, and rotation
  • warning trend for printer drivers can change printable margins after paper selection
  • latency of the stage that must read page size and rotation before asking the printer for device capabilities
  • profile usage for fit, actual size, shrink-only, center, rotation, and crop rules

Tekniska granskningsnoteringar för print preview and device-context output

Använd dessa granskningsnoteringar för att säkerställa att funktionen har passerat demo-nivån och kan försvaras under leverans, support och kundeskalering.

  • Beslut: fit, actual size, shrink-only, center, rotation, and crop rules. Implementeringspresspunkt: compute the target rectangle from paper, printable area, and scaling policy. Acceptansbevis: annotation and form-field print policy. Regressionsutlösare: preview should disclose when it cannot match a driver-specific feature
  • Beslut: paper selection, printable margins, tray, duplex, and color mode policy. Implementeringspresspunkt: render preview using the same geometry contract as the print job. Acceptansbevis: operator selection and print result or driver error. Regressionsutlösare: printer drivers can change printable margins after paper selection
  • Beslut: preview fidelity requirements compared with printer-driver output. Implementeringspresspunkt: record driver and device context details when output is generated. Acceptansbevis: printer name, driver version, paper size, printable area, scaling, and rotation. Regressionsutlösare: landscape pages need a clear auto-rotation policy
  • Beslut: whether annotations, form fields, watermarks, and backgrounds are printed. Implementeringspresspunkt: test high-value documents on the printer families customers actually use. Acceptansbevis: PDF page box, page number, rendered rectangle, and DPI. Regressionsutlösare: duplex output can expose odd-page and blank-page assumptions
  • Beslut: fit, actual size, shrink-only, center, rotation, and crop rules. Implementeringspresspunkt: read page size and rotation before asking the printer for device capabilities. Acceptansbevis: annotation and form-field print policy. Regressionsutlösare: preview should disclose when it cannot match a driver-specific feature

Gränsfall

  • printer drivers can change printable margins after paper selection
  • landscape pages need a clear auto-rotation policy
  • duplex output can expose odd-page and blank-page assumptions
  • preview should disclose when it cannot match a driver-specific feature

Delphi / C++Builder notes

PDFlibPas should sit behind a small service boundary that receives files, streams, profiles, and credentials, then returns output paths, warnings, metrics, and validation status. Important terms include device context, print preview, DPI, printable area, scaling, duplex.

Delphi-kodexempel

Följande Delphi-skiss visar en praktisk servicegräns för detta ämne. Håll policykontroller, loggning och validering utanför det smala produktanropet så att arbetsflödet går att testa.

procedure PaintPreviewPage(Canvas: TCanvas; const FileName: string; PageRef: Integer; Dpi: Double);
var
  Pdf: TPDFlib;
  FileHandle: Integer;
begin
  Pdf := TPDFlib.Create;
  try
    FileHandle := Pdf.DAOpenFileReadOnly(FileName, '');
    try
      Pdf.DARenderPageToDC(FileHandle, PageRef, Dpi, Canvas.Handle);
      DrawPreviewOverlay(Canvas, PageRef, Dpi);
    finally
      Pdf.DACloseFile(FileHandle);
    end;
  finally
    Pdf.Free;
  end;
end;

Produktionschecklista

  • Kör arbetsflödet på en tom fil, en normal kundfil och en värstafallfil
  • Öppna den genererade PDF-filen med rätt visare, validator, skrivare eller nedströmsapplikation
  • Logga produktversion, profilversion, inmatningshash, utdatasökväg, förfluten tid och antal varningar
  • Håll lösenord, certifikat, tillfälliga filer och kunddata under tydliga lagringsregler
  • Lägg till regressionsdokument när en kundfil avslöjar ett nytt gränsfall

Produktdokumentation

PDFlibPas

Fler kodexempel

var
  Pdf: TPDFlib;
  Virt: WideString;
  Opt: Integer;
begin
  Pdf := TPDFlib.Create;
  try
    if Pdf.LoadFromFile('report.pdf', '') <> 1 then
      raise Exception.Create('load failed');
    Virt := Pdf.NewCustomPrinter(Pdf.GetDefaultPrinterName);
    Pdf.SetupPrinter(Virt, 1, 9);        // setting 1 = paper, DMPAPER_A4
    Pdf.SetupPrinter(Virt, 11, 1);       // setting 11 = orientation, 1 = portrait
    Opt := Pdf.PrintOptions(1, 1, 'Monthly Report');  // fit to paper, auto-rotate + center
    Pdf.PrintDocument(Virt, 1, Pdf.PageCount, Opt);
  finally
    Pdf.Free;
  end;
end;
procedure ShowPrinterTruePreview(Pdf: TPDFlib; const Virt: WideString; Opt: Integer);
var
  Data: AnsiString;
  Strm: TMemoryStream;
  Bmp: TBitmap;
begin
  Data := Pdf.GetPrintPreviewBitmapToString(Virt, 1, Opt, 1200, 0);
  Strm := TMemoryStream.Create;
  try
    Strm.WriteBuffer(PAnsiChar(Data)^, Length(Data));
    Strm.Position := 0;
    Bmp := TBitmap.Create;
    try
      Bmp.LoadFromStream(Strm);
      PreviewImage.Picture.Assign(Bmp);
    finally
      Bmp.Free;
    end;
  finally
    Strm.Free;
  end;
end;