PDFium Delphi Component Docs

FPdfMeasure 單元

單元: FPdfMeasure
在將內容加入頁面之前,為 TPdf 提供文字量測輔助功能

語法

TPdfMeasureHelper = class helper for TPdf
  procedure MeasureText(const Text, Font: WString; FontSize: Single; out Width, Height: Double);
  function MeasureTextWidth(const Text, Font: WString; FontSize: Single): Double;
  function WrapText(const Text, Font: WString; FontSize: Single; MaxWidth: Double): TArray<WString>;
  function WrapText(const Text, Font: WString; FontSize: Single; MaxWidth: Double; BreakLongWords: Boolean): TArray<WString>;
end;

公開方法

MemberDescription
TPdf.MeasureText以選定的字型與大小傳回文字在 PDF 使用者單位下的寬度與高度
TPdf.MeasureTextWidth只傳回文字在 PDF 使用者單位下的寬度
TPdf.WrapText保留明確的換行,將語彙基元折行至 MaxWidth 內,並可在 Unicode 純量界限處拆分超長語彙基元

說明

FPdfMeasure 為 TPdf 加入類別輔助器,讓文字可在繪製前以 PDF 使用者單位量測

TPdfMeasureHelper 就是這個輔助器:它透過一個暫時的原生文字物件量測,該物件會被建立、受限並銷毀,不會把任何內容加到頁面上;未載入文件或無法建立文字物件時,則回傳零範圍

使用 MeasureText 與 MeasureTextWidth 進行版面計算,並使用 WrapText 將文字拆成符合指定寬度的行

原始的 WrapText 多載為了相容性會保持超長的語彙基元完整;傳入 BreakLongWords=True 可拆分 CJK 或其他沒有空格的文字,同時保留 UTF-16 surrogate 配對

範例

var
  Width, Height: Double;
  Lines: TArray<WString>;
begin
  Pdf.MeasureText('Invoice total', 'Arial', 12, Width, Height);
  Lines := Pdf.WrapText(LongText, 'Arial', 10, 240, True);
end;

另請參閱

TPdf.AddText, TPdf.AddText