PDFium Delphi Component Docs

PDFium VCL -ohjelmointiesimerkit

Perusdokumenttitoiminnot

PDF-dokumentin lataaminen ja näyttäminen

Pdf1.FileName := 'C:\MyDocument.pdf';
Pdf1.Active := True;
ShowMessage('Pages: ' + IntToStr(Pdf1.PageCount));
ShowMessage('Title: ' + Pdf1.Title);
ShowMessage('Author: ' + Pdf1.Author);

var
  Stream: TMemoryStream;
begin
  Stream := TMemoryStream.Create;
  try
    Stream.LoadFromFile('C:\MyDocument.pdf');
    Pdf1.LoadDocument(Stream, True);
    Pdf1.Active := True;
    ShowMessage('PDF loaded from stream');
  finally
    Stream.Free;
  end;
end;

Uuden PDF-dokumentin luominen

Pdf1.CreateDocument;
Pdf1.Active := True;
Pdf1.AddPage(0, 612, 792);
Pdf1.PageNumber := 1;
Pdf1.AddText('Hello World!', 'Arial', 12, 100, 700, clBlack, $FF, 0.0);
Pdf1.UpdatePage;
Pdf1.SaveAs('C:\NewDocument.pdf');
Pdf1.Active := False;

Tekstitoiminnot

Tekstin poimiminen PDF:stä

var
  PageText: WString;
  RectText: WString;
begin
  Pdf1.PageNumber := 1;
  PageText := Pdf1.Text;
  Memo1.Text := PageText;
  RectText := Pdf1.TextInRectangle(100, 100, 500, 200);
  ShowMessage('Text in rectangle: ' + RectText);
end;

Tekstin hakeminen

var
  Position: Integer;
begin
  Position := Pdf1.FindFirst('Hello', [seCaseSensitive]);
  while Position >= 0 do
  begin
    ShowMessage('Found at character: ' + IntToStr(Position));
    Position := Pdf1.FindNext;
  end;
end;

Tekstin lisääminen muotoilulla

Pdf1.PageNumber := 1;
Pdf1.AddText('Document Title', 'Times New Roman', 16, 100, 750, clBlue);
Pdf1.AddText('This is the document content.', 'Arial', 12, 100, 700, clBlack);
Pdf1.UpdatePage;

Merkkien ja fonttien metatietojen tarkasteleminen

var
  I: Integer;
  MetricSize: Single;
  FontBytes: TBytes;
begin
  Pdf1.PageNumber := 1;
  for I := 0 to Pdf1.ObjectCount - 1 do
    if Pdf1.ObjectType[I] = otText then
    begin
      MetricSize := 12.0;
      FontBytes := Pdf1.FontData[I];
      Memo1.Lines.Add('Object ' + IntToStr(I));
      Memo1.Lines.Add('Base name: ' + Pdf1.FontBaseName[I]);
      Memo1.Lines.Add('Family: ' + Pdf1.FontFamilyName[I]);
      Memo1.Lines.Add('Embedded: ' + BoolToStr(Pdf1.FontIsEmbedded[I], True));
      Memo1.Lines.Add('Weight: ' + IntToStr(Pdf1.FontWeight[I]));
      Memo1.Lines.Add('Italic angle: ' + IntToStr(Pdf1.FontItalicAngle[I]));
      Memo1.Lines.Add('Ascent: ' + FloatToStr(Pdf1.FontAscent[I, MetricSize]));
      Memo1.Lines.Add('Descent: ' + FloatToStr(Pdf1.FontDescent[I, MetricSize]));
      Memo1.Lines.Add('Font data bytes: ' + IntToStr(Length(FontBytes)));
    end;

  if Pdf1.CharacterCount > 0 then
  begin
    Memo1.Lines.Add('First character size: ' + FloatToStr(Pdf1.CharacterFontSize[0]));
    Memo1.Lines.Add('First character weight: ' + IntToStr(Pdf1.CharacterFontWeight[0]));
    Memo1.Lines.Add('First character angle: ' + FloatToStr(Pdf1.CharacterAngle[0]));
  end;
end;

Demo\Delphi\FontProperties-, Demo\CBuilder\FontProperties- ja Demo\Lazarus\FontProperties-projektit näyttävät samat metatiedot sekä TPdf- että TPdfView-komponenttien kautta

Monikategoriaisen sisällönpoimintaraportin rakentaminen

Demo\Delphi\ContentExtractionLab-, Demo\CBuilder\ContentExtractionLab- ja Demo\Lazarus\ContentExtractionLab-projektit muuttavat sisällönpoiminnan yhdeksi työnkuluksi. Ne avaavat tai luovat PDF:n, antavat käyttäjän valita metatiedot, sivutekstin, sivuobjektit, kuvat, liitteet, linkit, merkinnät, kirjanmerkit sekä fontti- ja merkkimittarit, ja tuottavat sen jälkeen yhteenvedon sekä yksityiskohtaisen TXT- tai JSON-raportin. Lab sisältää myös kaikki / ei yhtään -kategoriavalinnan, raportin kopioinnin leikepöydälle ja lähde-PDF:n tallennustoiminnot, jotta käyttäjä voi pitää luodun esimerkkipdf:n poimintaraportin rinnalla. Vanhemmat ExtractText-, ExtractImages-, Attachment- ja FontProperties-demot pysyvät tarkkarajaisina esimerkkeinä; ExtractText tukee sivuväli- ja sivunerotinvaihtoehtoja sivukohtaisen tekstitulosteen tuottamiseksi Delphi-, C++Builder- ja Lazarus-ympäristöissä, kun taas ContentExtractionLab on kokoava inventaarioesimerkki

Taulukoiden tunnistus ja poiminta

Projektit Demo\Delphi\TableExtractionLab, Demo\CBuilder\TableExtractionLab ja Demo\Lazarus\TableExtractionLab tekevät taulukoiden tunnistuksesta yhden työnkulun. Laboratorio rakentaa esimerkkiasiakirjan, jossa on viivoitettu taulukko yhdistettyine otsikkosoluineen, tyhjän tilan mukaan tasattu taulukko ilman viivoitusta ja pitkä taulukko, joka katkeaa sivun rajalle, ja suorittaa sitten ExtractTables-metodin nykyiselle sivulle tai ExtractDocumentTables-metodin koko asiakirjalle sekä raportoi rivit, sarakkeet, RowSpan- ja ColumnSpan-arvot, otsikkoliput, tunnistustilan, luottamusarvon ja jatkoaryhmät. Valinnat DetectRuledTables, DetectWhitespaceTables, DetectContinuations, DetectFilledRulings ja MinConfidence ovat muokattavissa käyttöliittymässä, ja valittu taulukko viedään TPdfTable.ToCsv- ja TPdfTable.ToJson-muodoissa. Laboratorio käsittelee myös kokonaisen kansion eräajona: Batch folder suorittaa ExtractDocumentTables-metodin jokaiselle PDF-tiedostolle, raportoi taulukkomäärät asiakirjoittain, merkitse skannaukset, joilta tekstikerros puuttuu, ja yrittää tyhjiä asiakirjoja uudelleen tiivistetyn sarakkeiston asetuksella; Export all CSV kirjoittaa jokaisen tunnistetun taulukon lähdetiedostojen viereen. Jokainen asiakirja avataan omaan TPdf-instanssiinsa, jotta yksi lukukelvoton tiedosto ei voi vaikuttaa erän muihin tiedostoihin

Lomaketoiminnot

Lomakekenttien käsittely

var
  I: Integer;
  FieldInfo: TPdfFormFieldInfo;
begin
  Pdf1.FormField[0] := 'John';
  for I := 0 to Pdf1.FormFieldCount - 1 do
  begin
    FieldInfo := Pdf1.FormFieldInfo[I];
    ShowMessage(FieldInfo.Name + ': ' + Pdf1.FormField[I]);
  end;
end;

Täytettyjen lomakearvojen litistys sivulle

// Without flattening, FormField[i] := value writes only the /V entry —
// the visible appearance comes from the /AP stream and may not refresh
// GenerateFormAppearances regenerates /AP for every widget, then
// FlattenAllPages bakes every page's annotations and widgets into
// permanent, non-editable content
Pdf1.FileName := 'C:\Application.pdf';
Pdf1.Active := True;
Pdf1.FormField[0] := 'John Smith';
Pdf1.FormField[1] := 'john@example.com';
Pdf1.GenerateFormAppearances;
if Pdf1.FlattenAllPages then
  Pdf1.SaveAs('C:\Application.flat.pdf');
Pdf1.Active := False;

Kuvatoiminnot

Kuvien poimiminen

var
  I: Integer;
  Bitmap: TBitmap;
begin
  for I := 0 to Pdf1.BitmapCount - 1 do
  begin
    Bitmap := Pdf1.Bitmap[I];
    try
      Bitmap.SaveToFile('C:\ExtractedImage' + IntToStr(I) + '.bmp');
    finally
      Bitmap.Free;
    end;
  end;
end;

Kuvien lisääminen

var
  JpegStream: TFileStream;
begin
  JpegStream := TFileStream.Create('C:\MyImage.jpg', fmOpenRead or fmShareDenyWrite);
  try
    Pdf1.AddJpegImage(JpegStream, 100, 500, 200, 150);
  finally
    JpegStream.Free;
  end;
  Pdf1.AddPicture(Image1.Picture, 100, 300, 150, 100);
  Pdf1.UpdatePage;
end;

Merkintätoiminnot

Merkintöjen käsittely

var
  I: Integer;
  Annotation: TPdfAnnotation;
begin
  for I := 0 to Pdf1.AnnotationCount - 1 do
  begin
    Annotation := Pdf1.Annotation[I];
    ShowMessage('Annotation ' + IntToStr(I) + ': ' + Annotation.ContentsText);
  end;
end;

Piirtotoiminnot

Muotojen piirtäminen

Pdf1.CreatePath(100, 100, 200, 150, fmAlternate, clYellow, $FF, True, clBlack, $FF, 2.0);
Pdf1.AddPath;

Pdf1.CreatePath(100, 100, fmNone, clBlack, $FF, True, clRed, $FF, 3.0);
Pdf1.LineTo(200, 100);
Pdf1.LineTo(150, 200);
Pdf1.ClosePath;
Pdf1.AddPath;
Pdf1.UpdatePage;

Navigointi ja kirjanmerkit

Kirjanmerkkien käsittely

var
  I: Integer;
  Bookmarks: TBookmarks;
begin
  Bookmarks := Pdf1.Bookmarks;
  for I := 0 to Length(Bookmarks) - 1 do
    ShowMessage(Bookmarks[I].Title + ' -> Page ' + IntToStr(Bookmarks[I].PageNumber));
end;

Nimettyjen kohteiden käsittely

var
  I: Integer;
  Destination: TDestination;
begin
  for I := 0 to Pdf1.DestinationCount - 1 do
  begin
    Destination := Pdf1.Destination[I];
    if Destination.Name = 'Chapter1' then
    begin
      Pdf1.PageNumber := Destination.PageNumber;
      Break;
    end;
  end;
end;

Liitetiedostotoiminnot

Liitteiden käsittely

var
  I: Integer;
  AttachmentData: TBytes;
  AttachmentName: WString;
  FileStream: TFileStream;
begin
  for I := 0 to Pdf1.AttachmentCount - 1 do
  begin
    AttachmentName := Pdf1.AttachmentName[I];
    AttachmentData := Pdf1.Attachment[I];
    FileStream := TFileStream.Create('C:\Extracted_' + string(AttachmentName), fmCreate);
    try
      if Length(AttachmentData) > 0 then
        FileStream.WriteBuffer(AttachmentData[0], Length(AttachmentData));
    finally
      FileStream.Free;
    end;
  end;
  if Pdf1.CreateAttachment('MyFile.txt') then
    Pdf1.Attachment[Pdf1.AttachmentCount - 1] := TFile.ReadAllBytes('C:\MyFile.txt');
end;

Sivujen hallinta

Sivutoiminnot

Pdf1.AddPage(Pdf1.PageCount + 1, 612, 792);
Pdf1.AddPage(Pdf1.PageCount + 1, 595, 842);
Pdf1.AddPage(Pdf1.PageCount + 1, 400, 600);

var
  SourcePdf: TPdf;
begin
  SourcePdf := TPdf.Create(nil);
  try
    SourcePdf.FileName := 'C:\SourceDocument.pdf';
    SourcePdf.Active := True;
    Pdf1.ImportPages(SourcePdf, '1', Pdf1.PageCount + 1);
  finally
    SourcePdf.Free;
  end;
end;

if Pdf1.PageCount > 1 then
  Pdf1.DeletePage(Pdf1.PageCount);

Tallennus ja pakkaus

Tallennus pakkauksella

// Create a compressed PDF document
Pdf1.CreateDocument;
Pdf1.Compressed := True; // ensure all streams are FlateDecode compressed (default)
Pdf1.Active := True;
Pdf1.AddPage(0, 595, 842); // A4 size
Pdf1.AddText('Hello World!', 'Arial', 24, 100, 700, clBlack, $FF, 0.0);
Pdf1.SaveAs('C:\Compressed.pdf');
Pdf1.Active := False;

// Save with options
Pdf1.SaveAs('C:\Output.pdf', saIncremental); // incremental save
Pdf1.SaveAs('C:\NoSecurity.pdf', saRemoveSecurity); // remove security

// Save to a specific PDF version target. The PDF version validator rejects
// any feature introduced after the chosen target (JBIG2Decode, MarkInfo,
// AES encryption, Polygon / Caret / Watermark / Redact annotations, ...)
if not Pdf1.SaveAs('C:\Legacy15.pdf', saNone, pv15) then
  ShowMessage('Save rejected: document contains features newer than PDF 1.5');

PDF/A-1b-arkistotulosteen luominen

// SaveAsPdfA post-processes the base save with an incremental update that
// injects an XMP metadata stream, an sRGB ICC OutputIntent, and an updated
// document catalog so the result is PDF/A-1b conformant. The bundled
// sRGB IEC61966-2.1 profile is used automatically when no custom profile
// is supplied
Pdf1.FileName := 'C:\Report.pdf';
Pdf1.Active := True;
if Pdf1.SaveAsPdfA('C:\Report.pdfa.pdf', pac1b) then
  ShowMessage('Saved PDF/A-1b archive');

// PDF/A with a custom ICC profile (CMYK printing pipelines for example)
var
  Opts: TPdfASaveOptions;
begin
  Opts := Default(TPdfASaveOptions);
  Opts.Conformance    := pac1b;
  Opts.IccProfileData := TFile.ReadAllBytes('C:\Profiles\Coated_GRACoL_2006.icc');
  Pdf1.SaveAsPdfA('C:\Report.cmyk.pdfa.pdf', Opts);
end;

// In-memory PDF/A output
var
  Stream: TMemoryStream;
begin
  Stream := TMemoryStream.Create;
  try
    Opts := Default(TPdfASaveOptions);
    Opts.Conformance := pac1b;
    if Pdf1.SaveAsPdfAToStream(Stream, Opts) then
      UploadToArchive(Stream);
  finally
    Stream.Free;
  end;
end;

PDF/A-yhteensopivuuden vahvistaminen

var
  Validation: TPdfAValidationResult;
  Issue: TPdfAValidationIssue;
begin
  Pdf1.FileName := 'C:\Incoming\report.pdf';
  Pdf1.Active   := True;

  // Cheap header-style probe — reads the conformance without iterating
  // the full validator. Result: pacNone, pac1a, pac1b, pac2b, pac3b, or
  // pacUnknown
  case Pdf1.PdfAConformance of
    pac1b: Status.Caption := 'PDF/A-1b';
    pac2b: Status.Caption := 'PDF/A-2b';
    pacNone: Status.Caption := 'Not PDF/A';
  else
    Status.Caption := 'Unknown conformance';
  end;

  // Full validator with per-issue diagnostics
  Validation := Pdf1.ValidatePdfA;
  Memo1.Lines.Add('Conformance: ' + GetEnumName(TypeInfo(TPdfAConformance),
    Ord(Validation.Conformance)));
  for Issue in Validation.Issues do
    Memo1.Lines.Add('- ' + Issue.Description);
end;

Useiden PDF-standardien tarkasteleminen StandardsLabissa

Demo\Delphi\StandardsLab-, Demo\CBuilder\StandardsLab- ja Demo\Lazarus\StandardsLab-projektit lataavat tai luovat PDF:n, ajavat ValidatePdfA-, ValidatePdfUa-, ValidatePdfE-, ValidatePdfX-, ValidatePdfR- ja ValidatePdfVT-toiminnot ja näyttävät sitten jokaisen havaitun vaatimustenmukaisuustason sekä ongelmien määrän yhdessä ruudukossa. Niiden tallennuspainikkeet kutsuvat SaveAsPdfA-, SaveAsPdfUa-, SaveAsPdfE-, SaveAsPdfX-, SaveAsPdfR- ja SaveAsPdfVT-toimintoja, joten merkkitulosteen työnkulku näkyy ilman omaa kehystä

Preflight-raporttien luominen

FPdfPreflightReport-yksikkö kapseloi ValidatePdfA-, ValidatePdfUa-, ValidatePdfE-, ValidatePdfX-, ValidatePdfR- ja ValidatePdfVT-toiminnot yhdeksi kyseltäväksi raporttiobjektiksi, jossa on Info / Warning / Error -vakavuus, tilan ja ongelmien koostaminen, haku, suodatus sekä vakaita SHA-256-sormenjälkiä. Raportit voi tuottaa tekstinä, HTML:nä, Markdownina, JSONina, CSV:nä, SARIF 2.1.0:nä, JUnit XML:nä, NDJSONina tai natiivina XML:nä. Demo\Delphi\PreflightReport, Demo\Lazarus\PreflightReport ja Demo\CBuilder\PreflightReport tarjoavat vuorovaikutteiset valinta- ja esikatselutyönkulut, ja Demo\Delphi\PreflightReportCli lisää skriptattavat syöttö-/tulostusargumentit, standardisuodatuksen, valinnaisen raporttiliitteen upottamisen, CI:n paluukoodiportit, eräluetteloiden ja hakemistojen käsittelyn, törmäysturvallisen reitityksen, koosteyhteenvedot sekä CSV-manifestit. Luotu raportti toteaa, että sisäänrakennetut validaattorit kattavat merkkitason ja valitut tiedostotason tarkistukset, joten koko sisällön kattavan preflightin voi edelleen delegoida erilliselle validointimoottorille

Virtautettu lataus ja suuret asiakirjat

PDF:n lataaminen tarvittaessa LoadCustomDocumentilla

// LoadCustomDocument lets PDFium pull blocks from any seekable TStream on
// demand. The stream is NOT copied into memory, so multi-GB PDFs, remote
// HTTP body streams, and database-backed sources are all viable without
// an up-front buffer
var
  FileStream: TFileStream;
begin
  // Read-from-file streaming (no in-memory copy)
  FileStream := TFileStream.Create('C:\Huge.pdf',
    fmOpenRead or fmShareDenyWrite);
  Pdf1.LoadCustomDocument(FileStream, True{AOwnsStream});
  // ^^ ownership transferred — Pdf1 frees FileStream on UnloadDocument
  Pdf1.Active := True;
  ShowMessage('Pages: ' + IntToStr(Pdf1.PageCount));
end;

// Externally-owned stream (caller frees it after UnloadDocument)
var
  Stream: TFileStream;
begin
  Stream := TFileStream.Create('C:\Live.pdf',
    fmOpenRead or fmShareDenyNone);
  try
    Pdf1.LoadCustomDocument(Stream, False);
    Pdf1.Active := True;
    DoWork;
    Pdf1.Active := False;
  finally
    Stream.Free;
  end;
end;

Asynkroninen peruutus ja progressiivinen renderöinti

Yhteistoiminnalliset peruutustokenit

// FPdfAsync exposes IPdfCancellationToken /
// IPdfCancellationTokenSource for cancel-from-UI semantics that work
// across the long-running export and render paths
uses FPdfAsync;

var
  CancelSource: IPdfCancellationTokenSource;
  CancelToken:  IPdfCancellationToken;
begin
  CancelSource := PdfCancellationTokenSource;
  CancelToken  := CancelSource.Token;

  // user clicks Cancel
  CancelSource.Cancel;

  // The worker periodically checks CancelToken.IsCancelled, or the render
  // path polls it through PDFium's IFSDK_PAUSE callback
  if CancelToken.IsCancelled then
    Memo1.Lines.Add('Cancelled by user');
end;

Peruutettava progressiivinen sivun renderöinti

// RenderPageProgressive lets a long high-DPI render abort mid-page when
// the user clicks Cancel, instead of blocking the caller until done
var
  Status: TPdfProgressiveStatus;
  Bitmap: TBitmap;
begin
  Bitmap := TBitmap.Create;
  try
    Bitmap.PixelFormat := pf32bit;
    Bitmap.SetSize(2480, 3508); // A4 @ 300 DPI

    Status := Pdf1.RenderPageProgressive(Bitmap, 0, 0, Bitmap.Width,
      Bitmap.Height, CancelToken, ro0, [reAnnotations], clWhite);

    case Status of
      prsDone:      SaveBitmap(Bitmap);
      prsCancelled: Memo1.Lines.Add('Render cancelled — partial result available');
      prsFailed:    Memo1.Lines.Add('PDFium reported FPDF_RENDER_FAILED');
    end;
  finally
    Bitmap.Free;
  end;
end;

Renderöintien suorittaminen taustasäikeessä TPdfFuturella

// TPdfFuture<T> spins up a worker on a background thread and posts a
// TPdfFutureResult<T> envelope back to the main thread
//
// Important: TPdf serializes render calls per instance, but the worker still
// owns dispatch and document lifetime. Do not mutate a TPdf while a render is
// in flight; batch export usually keeps one TPdf per file
type
  TRenderResult = record
    Bitmap : TBitmap;
    PageNo : Integer;
  end;

var
  Future: TPdfFuture<TRenderResult>;
begin
  Future := TPdfFuture<TRenderResult>.Create(
    function (const ACancelToken: IPdfCancellationToken): TRenderResult
    var
      LocalPdf: TPdf;
    begin
      LocalPdf := TPdf.Create(nil);
      try
        LocalPdf.FileName := 'C:\Huge.pdf';
        LocalPdf.Active   := True;
        LocalPdf.PageNumber := 1;
        Result.Bitmap := LocalPdf.RenderPage(0, 0, 2480, 3508);
        Result.PageNo := 1;
      finally
        LocalPdf.Free;
      end;
    end,
    procedure (const AResult: TPdfFutureResult<TRenderResult>)
    begin
      case AResult.State of
        pfsSuccess:   PaintBitmapToView(AResult.Value.Bitmap);
        pfsCancelled: Log.Add('Cancelled');
        pfsFailed:    Log.Add('Worker failed: ' + AResult.ErrorMessage);
      end;
    end);
end;

Muunnosmatriisit

Translate / Scale / Rotate / Skew -toimintojen yhdistäminen

// FPdfMatrix wraps PDFium's FS_MATRIX so callers can build a transform
// declaratively and hand it to FPDFPageObj_SetMatrix
uses FPdfMatrix, FPdfView;

var
  M: TPdfMatrix;
  RawMatrix: FS_MATRIX;
  PageObj: FPDF_PAGEOBJECT;
begin
  M := TPdfMatrix.Create;
  try
    M.Translate(72, 200);       // move 1in right, ~2.8in up
    M.Scale(0.5, 0.5);          // half-size stamp
    M.Rotate(15);               // 15-degree CCW rotation
    // M.HorizontalFlip;
    // M.VerticalFlip;
    // M.CentralFlip;
    // M.Skew(10, 5);
    // M.Multiply(Other);       // post-multiply with another matrix

    // Apply to any PDFium page object (text / path / image / form XObject)
    RawMatrix := M.Handle;
    FPDFPageObj_SetMatrix(PageObj, RawMatrix);
  finally
    M.Free;
  end;
end;

Sivujen sommittelu

Sivujen osajoukon tuominen indeksin mukaan

// ImportPagesByIndex copies an explicit zero-based index array from a
// source PDF into this document. InsertAt = 0 inserts before page 1;
// PageCount appends
var
  Source: TPdf;
begin
  Source := TPdf.Create(nil);
  try
    Source.FileName := 'C:\Report.pdf';
    Source.Active   := True;

    // Import pages 0, 2, 4 (one-based: pages 1, 3, 5) from Source
    // and append them at the end of Pdf1
    Pdf1.ImportPagesByIndex(Source, [0, 2, 4], Pdf1.PageCount);

    // Empty array imports every source page
    // Pdf1.ImportPagesByIndex(Source, [], 0);
  finally
    Source.Free;
  end;
end;

N-up-yhdistelmätuloste

// ImportNPagesToOne returns a brand-new TPdf whose pages are NumX*NumY
// composites of THIS document. OutputWidth/Height are PDF user units
// (1 unit = 1/72 in)
var
  Composite: TPdf;
begin
  Pdf1.FileName := 'C:\Slides.pdf';
  Pdf1.Active   := True;
  // 4-up A4 landscape: 842 x 595 pt, 2 columns x 2 rows
  Composite := Pdf1.ImportNPagesToOne(842, 595, 2, 2);
  try
    if Composite <> nil then
      Composite.SaveAs('C:\Slides_4up.pdf');
  finally
    Composite.Free;
  end;
end;

Sivujen uudelleenjärjestäminen paikallaan

// MovePages takes the zero-based indices of the pages to move plus the
// destination index for the first moved page after the move completes
// Other pages shift around the moved block so the document length stays
// the same
//
// Example: in a 5-page document with pages numbered [0,1,2,3,4],
// MovePages([2, 3], 0) reorders to [2, 3, 0, 1, 4]
if not Pdf1.MovePages([2, 3], 0) then
  ShowMessage('Invalid indices passed to MovePages');

Vesileiman lisääminen Form XObject -sivun uudelleenkäytöllä

// CreateXObjectFromPage manufactures a reusable Form XObject handle from
// any page of any other TPdf. Stamp that handle onto as many pages of
// THIS document as you want — each stamp is a single PDFium page object
// that can be positioned, scaled, and rotated through the matrix API
uses FPdfMatrix, FPdfView;

var
  WatermarkSource: TPdf;
  XObj:            TPdfXObject;
  PageObj:         FPDF_PAGEOBJECT;
  M:               TPdfMatrix;
  RawMatrix:       FS_MATRIX;
  I:               Integer;
begin
  WatermarkSource := TPdf.Create(nil);
  try
    WatermarkSource.FileName := 'C:\Watermarks\Confidential.pdf';
    WatermarkSource.Active   := True;

    // Wrap page 0 of WatermarkSource as a reusable XObject
    XObj := Pdf1.CreateXObjectFromPage(WatermarkSource, 0);
    try
      for I := 1 to Pdf1.PageCount do
      begin
        Pdf1.PageNumber := I;
        PageObj := Pdf1.InsertFormObjectFromXObject(XObj);
        if PageObj = nil then Continue;

        // Centre the watermark on the page
        M := TPdfMatrix.Create;
        try
          M.Scale(0.5, 0.5);
          M.Translate(0.25 * Pdf1.PageWidth, 0.5 * Pdf1.PageHeight);
          RawMatrix := M.Handle;
          FPDFPageObj_SetMatrix(PageObj, RawMatrix);
        finally
          M.Free;
        end;

        Pdf1.UpdatePage;
      end;
    finally
      XObj.Free; // closes the FPDF_XOBJECT handle
    end;

    Pdf1.SaveAs('C:\Watermarked.pdf');
  finally
    WatermarkSource.Free;
  end;
end;

Demo\Delphi\WatermarkStamp-, Demo\Lazarus\WatermarkStamp- ja Demo\CBuilder\WatermarkStamp-projektit muuttavat tämän mallin ajettavaksi työnkuluksi: ne luovat kohde-PDF:n ja uudelleenkäytettävän leima-PDF:n, kietovat leimasivun CreateXObjectFromPage-kutsulla, lisäävät saman Form XObjectin jokaiselle kohdesivulle, käyttävät TPdfMatrix-muunnoksia keskitettyihin vesileimoihin ja oikean yläkulman leimoihin sekä lisäävät haluttaessa sivunumeronimikkeet ennen tuloksen tallennusta

Kuvan lisääminen (suorat AddImage-overloadit)

Rekisteröidyn kuvatiedoston tai TBitmapin lisääminen

// AddImage(FileName) accepts any format registered with the VCL or LCL
// graphics units (BMP, PNG via PngImage, JPG, ...)
Pdf1.AddImage('C:\Photo.png', 100, 500, 200, 150);

// AddImage(TBitmap) skips the TPicture intermediary, ideal when the
// bitmap already comes from rendering or batch generation work
var
  Bitmap: TBitmap;
begin
  Bitmap := Pdf1.RenderPage(0, 0, 595, 842);
  try
    Pdf1.PageNumber := 2;
    Pdf1.AddImage(Bitmap, 50, 50, 250, 350);
    Pdf1.UpdatePage;
  finally
    Bitmap.Free;
  end;
end;

Demo\Delphi\ImageToPDF-, Demo\CBuilder\ImageToPDF- ja Demo\Lazarus\ImageToPDF-projektit muuttavat tämän täydelliseksi erätyönkuluksi: valitse useita kuvatiedostoja, esikatsele valittua kohdetta, luo yksi PDF-sivu jokaista kuvaa kohti, skaalaa jokainen kuva A4-pysty- tai vaakasivulle, tallenna tulos ja avaa luotu PDF

Hakutulosten korostaminen katselimessa

Korosta jokainen osuma nykyisellä sivulla

var
  MatchCount: Integer;
begin
  // Paint a clYellow mask over every match on the view's current page
  // Switching to a different page clears the highlight automatically
  PdfView1.HighlightColor := clYellow;
  MatchCount := PdfView1.HighlightSearchText('invoice', False{Case}, True{Word});
  Status.Caption := Format('%d match(es) on this page', [MatchCount]);

  // Programmatically remove the overlay before the page changes
  PdfView1.ClearHighlight;
end;

Katselimen asettelu ja sivun väri

Sovitustila ja sivun tausta

// FitMode keeps long documents framed during Resize or page changes
// Setting Zoom directly cancels FitMode (it reverts to pfmNone)
PdfView1.FitMode := pfmFitPage;       // whole page in viewport
// PdfView1.FitMode := pfmFitWidth;   // page width matches viewport width
// PdfView1.FitMode := pfmActualSize; // 100%

// PageColor decouples the rendered PDF page from the host control's Color
// — typical dark-mode viewer setup keeps the scroll area dark while the
// PDF page stays paper-white
PdfView1.Color     := clBlack;        // scroll area
PdfView1.PageColor := clWhite;        // PDF page background

// Optional drop shadow and page border to lift the page off a dark bg
PdfView1.PageShadowSize  := 6;
PdfView1.PageShadowColor := $00404040;
PdfView1.PageBorderColor := clGray;

Katselinvuorovaikutuksen tutkiminen ViewerInteractionLabissa

Demo\Delphi\ViewerInteractionLab-, Demo\CBuilder\ViewerInteractionLab- ja Demo\Lazarus\ViewerInteractionLab-projektit pitävät modernin TPdfView-vuorovaikutuspinnan yhdessä kompaktissa lomakkeessa: avaa tai luo esimerkkipdf, vaihda DisplayMode, käytä FitMode-asetusta, muuta PageColor-väriä, kierrä sivuja, korosta nykyisen sivun hakutulokset HighlightSearchText-toiminnolla, ota käyttäjän tekstinvalinta käyttöön tai pois käytöstä, tarkastele SelectedText-arvoa, kutsu SelectAll-, CopySelectionToClipboard- ja ClearSelection-toimintoja sekä seuraa, miten DeviceToPage-koordinaatit päivittyvät tilapalkissa hiiren liikkuvan sivun päällä

Tekstin hakeminen ja valitseminen SearchAndSelect-sovelluksessa

Demo\Delphi\SearchAndSelect-, Demo\CBuilder\SearchAndSelect- ja Demo\Lazarus\SearchAndSelect-projektit muuttavat haku- ja valintatyönkulun itsenäisiksi esimerkeiksi. Ne avaavat tai luovat PDF:n, listaavat kaikkien sivujen hakutulokset sivunumeron, merkin indeksin ja esikatselutekstin kanssa, tukevat Previous / Next -navigointia ja kaksoisklikkauksella hyppäämistä tulokseen, korostavat nykyisen sivun osumat HighlightSearchText-toiminnolla, tarjoavat kirjainkoko- ja kokonaisen sanan hakuvaihtoehdot, vaihtavat AllowUserTextSelection-asetusta, esikatselevat SelectedText-arvoa, ohjaavat SelectAll-, CopySelectionToClipboard- ja ClearSelection-toimintoja painikkeista sekä antavat käyttäjän vaihtaa yksi- ja aukeamanäyttötilojen välillä

PDF-tulostusasetusten lukeminen PrintPreferences-sovelluksessa

Demo\Delphi\PrintPreferences-, Demo\CBuilder\PrintPreferences- ja Demo\Lazarus\PrintPreferences-projektit lukevat tekijän tulostusasetukset ennen tulostamista. Ne näyttävät PrintCopies-, PrintPageRanges-, PrintScaling- ja PrintPaperHandling-arvot, voivat kopioida tekijän antamat kopiomäärät ja alueet tulostussyötteisiin sekä näyttävät, miten SetPdfPrintPaperHandlingDevMode kartoittaa PDF:n duplex-aikomuksen Windowsin DEVMODE-rakenteeksi

Tavallinen PDF-tulostus PrintPDF-sovelluksessa

Demo\Delphi\PrintPDF-, Demo\CBuilder\PrintPDF- ja Demo\Lazarus\PrintPDF-projektit näyttävät tavallisen tulostustyönkulun: avaa PDF, esikatsele sivut, valitse sivualueet ja kopiomäärät alustan tulostusvalintaikkunassa, noudata lajittelutilaa, renderöi jokainen sivu TPdf.RenderPage-toiminnolla ja näytä tulostuksen edistyminen peruutusmahdollisuudella. Delphi-esimerkki hyväksyy myös pudotetut PDF-tiedostot nopeaa lataamista varten

Nopea monisivuinen katselu MultiPageViewer-sovelluksessa

Demo\Delphi\MultiPageViewer-, Demo\CBuilder\MultiPageViewer- ja Demo\Lazarus\MultiPageViewer-projektit harjoittelevat jatkuvaa, aukeama- ja kansitietoista katselua tavallisilla sivunavigointi- ja zoomatyönkuluilla. Ne sopivat myös testaamaan ensimmäiselle sivulle avaamisen nopeutta ja renderöinnin responsiivisuutta hyvin suurilla PDF:illä: suurten tiedostojen testeissä 2.x GB:n asiakirjat, joiden avaamiseen Acrobat tai Foxit voi käyttää 20-30 sekuntia ennen ensimmäistä näkymää, voivat muuttua käyttökelpoisiksi lähes välittömästi PDFium Delphi Component -katselupolussa

Rinnakkainen tarkastelu SplitView-sovelluksessa

Demo\Delphi\SplitView-, Demo\CBuilder\SplitView- ja Demo\Lazarus\SplitView-projektit näyttävät PDF:n rinnakkaisarvioinnin. C++Builder- ja Lazarus-esimerkit keskittyvät kahden tai kolmen ruudun vertailuun, aktiivisen näkymän valintaan, valinnaisiin synkronoituihin navigointikomentoihin, yhteisiin zoomaesiasetuksiin sekä valitun näkymän tai kaikkien ladattujen näkymien kiertämiseen. Käytä näitä esimerkkejä suurten asiakirjojen vertailuun niin, että ensimmäinen käyttökelpoinen näkymä ja sitä seuraava sivujen renderöinti pysyvät responsiivisina; samat 2.x GB:n PDF-tiedostot, joiden avaaminen Acrobatissa tai Foxitissa voi kestää 20-30 sekuntia, voivat muuttua käyttökelpoisiksi lähes välittömästi PDFium Delphi Component -katselupolussa

PDF-riskipinnan tarkastus SecurityAudit-sovelluksessa

Demo\Delphi\SecurityAudit-, Demo\CBuilder\SecurityAudit- ja Demo\Lazarus\SecurityAudit-projektit avaavat PDF:n vain luku -tilassa ja luettelevat käyttöoikeustilan, upotetut liitteet, asiakirjan JavaScript-toiminnot, URI- ja Launch-linkkimerkinnät, verkkolinkit, allekirjoitusten yhteenvedot, XFA-tilan, V8-apurien saatavuuden sekä tukemattomien ominaisuuksien takaisinkutsut. Samat yhteenveto- ja havaintorivit voi tallentaa tai kopioida TXT-tarkastusraporttina tai tallentaa jäsenneltynä JSONina. Ne on tarkoitettu isäntäsovelluksen riskipaneeleiksi, eivät haittaohjelmien tunnistukseen

Katselimen linkkien käsittely

Turvallinen klikkaa ja seuraa -toiminto toimintokohtaisilla sallintalistoilla

// LinkOptions controls automatic handling of the four PDF link action
// types. Defaults enable goto and URI only; launch (run a program) and
// embedded GotoR (jump to another file) stay OFF so a stray click cannot
// run arbitrary code
PdfView1.LinkOptions :=
  [loAutoGoto, loAutoOpenURI];  // safe defaults

// OnAnnotationLinkClick fires for in-document destinations (page jumps,
// named destinations, action chains). Mark Handled = True to prevent the
// view from auto-following the link
procedure TForm1.PdfView1AnnotationLinkClick(Sender: TObject;
  LinkIndex: Integer; const Action: TPdfAction;
  var Handled: Boolean);
begin
  if Action.Kind = paUri then
  begin
    if MessageDlg('Open ' + Action.Uri + '?', mtConfirmation, [mbYes, mbNo], 0)
      <> mrYes then
      Handled := True;  // suppress auto-handling
  end;
end;

// OnWebLinkClick fires for URI strings discovered by PDFium's web-link
// scanner (auto-detected URLs in the text stream, even without an
// /A <</S/URI...>> annotation)
procedure TForm1.PdfView1WebLinkClick(Sender: TObject;
  WebLinkIndex: Integer; const Url: WString;
  var Handled: Boolean);
begin
  Log.Add('Web link clicked: ' + string(Url));
end;

Katselimen lomake-widgetien muokkaus

Valinta sekä kumoa / tee uudelleen

// On the focused AcroForm widget, six new methods drive PDFium's form
// edit history. All six short-circuit safely if no widget is focused or
// the document carries no AcroForm
procedure TForm1.btnSelectAllClick(Sender: TObject);
begin
  PdfView1.SelectAllFormText;
  EditMenuCopy.Caption := 'Copy: ' + string(PdfView1.GetSelectedFormText);
end;

procedure TForm1.btnUndoClick(Sender: TObject);
begin
  btnUndo.Enabled := PdfView1.FormCanUndo;
  btnRedo.Enabled := PdfView1.FormCanRedo;
  if PdfView1.FormCanUndo then
    PdfView1.FormUndo;
end;

procedure TForm1.btnRedoClick(Sender: TObject);
begin
  if PdfView1.FormCanRedo then
    PdfView1.FormRedo;
end;

Katselimen lukitseminen kioskeja varten

Sivunavigointieleiden poistaminen käytöstä

// All three default to True so existing apps upgrade with no behaviour
// change. Flip them False for kiosks, preview panes, or read-only
// embeddings without subclassing TPdfView
PdfView1.AllowUserPageChange       := False;  // PgUp/PgDn/Ctrl+Home/End
PdfView1.ChangePageOnMouseScrolling := False; // wheel up/down jumps
PdfView1.AllowUserTextSelection    := False;  // mouse/keyboard text selection

// Ctrl+wheel is reserved for zoom and is skipped by the navigation
// handler regardless of ChangePageOnMouseScrolling.

Pikkukuvien sivupaneeli

TPdfThumbnailViewn yhdistäminen TPdfViewiin

// TPdfThumbnailView (unit FPdfThumbnail) renders one page thumbnail per
// row in a scrollable side panel
uses FPdfThumbnail;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ThumbView.Pdf              := Pdf1;
  ThumbView.ThumbnailWidth   := 120;
  ThumbView.ThumbnailHeight  := 160;
  ThumbView.SelectionColor   := $00CFA85F;  // warm highlight
  ThumbView.OnPageClick      := ThumbViewPageClick;
end;

procedure TForm1.ThumbViewPageClick(Sender: TObject; PageIndex: Integer);
begin
  PdfView1.PageNumber := PageIndex + 1; // PageIndex is zero-based
end;

procedure TForm1.PdfView1PageNumberChanged(Sender: TObject);
begin
  ThumbView.CurrentPageIndex := PdfView1.PageNumber - 1;
end;

Yhteistoiminnallisesti peruutettava erävienti

Usean tiedoston PDF -> JPG -muunnos tiedosto- ja sivukohtaisella edistymisellä

// Pattern used by Demo\Delphi\BatchExport, Demo\CBuilder\BatchExport,
// and Demo\Lazarus\BatchExport. One TPdf per file, IPdfCancellationToken
// for cancel-from-UI, TPdfStreamAdapter for buffered file load
uses FPdfAsync;

procedure ExportBatch(const Files: TArray<string>;
  const OutDir: string; DPI: Integer; Quality: Integer;
  const ACancelToken: IPdfCancellationToken);
var
  I, J: Integer;
  Job:  TPdf;
  Bmp:  TBitmap;
  Jpeg: TJPEGImage;
begin
  for I := 0 to High(Files) do
  begin
    if ACancelToken.IsCancelled then Break;
    OverallProgress.Position := I;

    Job := TPdf.Create(nil);
    try
      Job.FileName := Files[I];
      Job.Active   := True;
      PageProgress.Max := Job.PageCount;

      for J := 1 to Job.PageCount do
      begin
        if ACancelToken.IsCancelled then Break;
        PageProgress.Position := J;

        Job.PageNumber := J;
        Bmp := Job.RenderPage(
          0, 0,
          Round(Job.PageWidth  * DPI / 72),
          Round(Job.PageHeight * DPI / 72));
        try
          Jpeg := TJPEGImage.Create;
          try
            Jpeg.CompressionQuality := Quality;
            Jpeg.Assign(Bmp);
            Jpeg.SaveToFile(Format('%s\%s.p%.3d.jpg',
              [OutDir, ExtractFileName(Files[I]), J]));
          finally
            Jpeg.Free;
          end;
        finally
          Bmp.Free;
        end;
      end;
    finally
      Job.Free;
    end;
  end;
end;

Katselimen koordinaatit ja osumatestaus

Sovita nykyinen sivu ja tarkastele hiiren alla olevaa tekstiä

procedure FitCurrentPage;
begin
  PdfView1.Zoom := PdfView1.PageZoom[PdfView1.PageNumber];
end;

procedure InspectViewerPoint(X, Y: Integer);
var
  PageNo: Integer;
  PageX, PageY: Double;
  CharIndex: Integer;
  Ch: WString;
begin
  PageNo := PdfView1.PageNumber;
  if not PdfView1.DeviceToPage(X, Y, PageNo, PageX, PageY) then
    Exit;

  CharIndex := PdfView1.CharacterIndexAtPos(X, Y, 8, 8);
  if CharIndex >= 0 then
  begin
    Ch := PdfView1.Text(CharIndex, 1);
    ShowMessage(Format('Page %d, X %.2f, Y %.2f, character %s',
      [PageNo, PageX, PageY, string(Ch)]));
  end;
end;

Virheenkäsittely

Vakaat PDF-toiminnot

function LoadPdfSafely(const FileName: string): Boolean;
begin
  Result := False;
  if not FileExists(FileName) then
    Exit;
  try
    Pdf1.FileName := FileName;
    Pdf1.Active := True;
    Result := Pdf1.Active;
  except
    Result := False;
  end;
end;

function ExtractTextSafely(PageNo: Integer): WString;
begin
  Result := '';
  if not Pdf1.Active then
    Exit;
  if (PageNo < 1) or (PageNo > Pdf1.PageCount) then
    Exit;
  Pdf1.PageNumber := PageNo;
  Result := Pdf1.Text;
end;

Tagged PDF ja saavutettavuus

Tunnista Tagged PDF ja käy rakennepuu läpi

// IsTagged is a cheap catalog probe — True when /StructTreeRoot exists
// StructureElements materialises the tree into a flat array of
// TPdfStructureElement records with type, title, alternate text, actual
// text, expansion text, language, level, parent index, and child / marked
// content / attribute counts
var
  Elements: TPdfStructureElements;
  I: Integer;
begin
  if not Pdf1.IsTagged then
  begin
    Log.Add('Document is NOT tagged — accessibility tools may struggle.');
    Exit;
  end;
  if Pdf1.Language = '' then
    Log.Add('Tagged PDF without /Lang — fails PDF/UA.');

  Elements := Pdf1.StructureElements;
  for I := 0 to High(Elements) do
    Log.Add(StringOfChar(' ', Elements[I].Level * 2)
      + Elements[I].StructType + ' "' + Elements[I].Title + '"');
end;

Tulostustilan valinta

EMF / PostScript / Image-Mask -tulostustilan asettaminen

// SetPdfPrintMode is a global function that maps to PDFium's
// FPDF_SetPrintMode. Use the matching TPdfPrintMode value for the
// printer driver in use
SetPdfPrintMode(pmEmf);                   // GDI EMF (default)
// SetPdfPrintMode(pmTextOnly);           // text-only rendering
// SetPdfPrintMode(pmPostScript2);        // PostScript Level 2
// SetPdfPrintMode(pmPostScript3);        // PostScript Level 3
// SetPdfPrintMode(pmPostScript2PassThrough);
// SetPdfPrintMode(pmPostScript3PassThrough);
// SetPdfPrintMode(pmEmfImageMasks);      // EMF + image masks
// SetPdfPrintMode(pmPostScript3Type42);  // PostScript 3 + Type 42 fonts

Tekijän tarkoittamien tulostusasetusten lukeminen

// PrintCopies, PrintPageRanges, PrintScaling, and PrintPaperHandling are
// the viewer-preference dictionary entries — they encode what the
// document author wanted PDF readers to default to
var
  Ranges: TPrintPageRanges;
  Range: TPrintPageRange;
begin
  ShowMessage('Suggested copies: ' + IntToStr(Pdf1.PrintCopies));
  ShowMessage('Print scaling: ' + GetEnumName(TypeInfo(TPrintScaling),
    Ord(Pdf1.PrintScaling)));

  Ranges := Pdf1.PrintPageRanges; // one-based ranges
  for Range in Ranges do
    Log.Add(Format('Pages %d..%d', [Range.First, Range.Last]));

  if SetPdfPrintPaperHandlingDevMode(PrinterDevMode, Pdf1.PrintPaperHandling) then
    ApplyPrinterDevMode(PrinterDevMode);
end;

Asiakirjan JavaScript-toiminnot

Nimipuu-JavaScriptin läpikäynti

// Document-level JavaScript is stored in the /Names /JavaScript name
// tree. JavaScriptAction[i] returns one entry; JavaScriptActions returns
// the full array
var
  I: Integer;
  Action: TPdfJavaScriptAction;
begin
  for I := 0 to Pdf1.JavaScriptActionCount - 1 do
  begin
    Action := Pdf1.JavaScriptAction[I];
    Memo1.Lines.Add('Name:   ' + string(Action.Name));
    Memo1.Lines.Add('Script: ' + string(Action.Script));
    Memo1.Lines.Add('---');
  end;
end;

Suorituskykyvinkit

PDF-toimintojen optimointi

procedure ProcessMultiplePages;
var
  I: Integer;
  PageText: WString;
begin
  for I := 1 to Pdf1.PageCount do
  begin
    Pdf1.PageNumber := I;
    PageText := Pdf1.Text;
    ProcessPageText(PageText);
  end;
end;

procedure SetOptimalRenderOptions;
begin
  PdfView1.Options := [reAnnotations, reLcd];
end;

// Zero-copy render path (default since v1.21.0): RenderPage writes
// directly into the destination TBitmap's DIB buffer when callers pass a
// pre-allocated bitmap, avoiding one width * height * 4 bytes copy
procedure RenderIntoCallerBitmap(Bitmap: TBitmap);
begin
  Bitmap.PixelFormat := pf32bit;
  Bitmap.SetSize(2480, 3508); // A4 @ 300 DPI
  Pdf1.RenderPage(Bitmap, 0, 0, Bitmap.Width, Bitmap.Height);
end;