Teknik makale

PDFlibPas: print preview and device-context output in Delphi

losLab PDF Library, Delphi ve C++Builder ekiplerine masaüstü, sunucu, DLL, ActiveX ve Dylib iş akışları için kaynak kodlu bir PDF motoru sağlar; dahili PDF/A ve PDF/UA kontrolleri, PAdES imzalama ve belgeleri harici PDF servisine göndermeden renderer seçimi sunar.

Bu yazı teams building print-preview, hard-copy approval, label, or controlled-output workflows in Delphi için hazırlanmıştır. print preview and device-context output konusunu tek bir bileşen çağrısı olarak değil, üretim düzeyinde belge mühendisliği olarak ele alır.

Pratik risk şudur: print output can differ from preview when device margins, scaling, rotation, duplex settings, and driver behavior are not modeled explicitly. Bu nedenle akışın yazılı sözleşmeye, gözlemlenebilir tanılara ve gerçekçi regresyon dosyalarına ihtiyacı vardır.

Mimari kararlar

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

Uygulama akışı

Preview with the same print contract used for output. Aşağıdaki sıra, iş akışını Delphi ve C++Builder ekipleri için incelenebilir tutar.

  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

Doğrulama kanıtı

Print evidence that helps support reproduce issues. Bu alanları çıktı veya destek kaydıyla birlikte saklayın.

  • 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

Mühendislik inceleme notları: print preview and device-context output

Özelliğin bir demoyu aşıp sürüm, destek ve müşteri eskalasyonu sırasında savunulabilir olduğunu doğrulamak için bu inceleme notlarını kullanın.

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

Sınır durumları

  • 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 kod örneği

Aşağıdaki Delphi taslağı bu konu için pratik bir servis sınırını gösterir. Politika kontrollerini, günlüklemeyi ve doğrulamayı dar ürün çağrısı bölümünün dışında tutarak akışı test edilebilir bırakın.

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;

Üretim kontrol listesi

  • İş akışını boş bir dosyada, normal bir müşteri dosyasında ve en kötü durum dosyasında çalıştırın
  • Oluşturulan PDF'yi hedef görüntüleyici, doğrulayıcı, yazıcı veya aşağı akış uygulamasıyla açın
  • Ürün sürümünü, profil sürümünü, giriş karmasını, çıktı yolunu, geçen süreyi ve uyarı sayısını kaydedin
  • Parolaları, sertifikaları, geçici dosyaları ve müşteri verilerini açık saklama kuralları altında tutun
  • Bir müşteri dosyası yeni bir uç durum ortaya çıkardığında regresyon belgeleri ekleyin

Ürün belgeleri

PDFlibPas

Ek kod örnekleri

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;