This guide will help you get started with PDFium VCL components in just a few minutes.
Pdf property to point to your TPdf componentSimple PDF Loading:
// Load a PDF file
Pdf1.FileName := 'C:\Sample.pdf';
Pdf1.Active := True;
ShowMessage('Pages: ' + IntToStr(Pdf1.PageCount));
// Or load from a memory stream
var
Stream: TMemoryStream;
begin
Stream := TMemoryStream.Create;
try
Stream.LoadFromFile('C:\Sample.pdf');
Pdf1.LoadDocument(Stream, True);
Pdf1.Active := True;
finally
Stream.Free;
end;
end;
// Go to next page
if PdfView1.PageNumber < Pdf1.PageCount then
PdfView1.PageNumber := PdfView1.PageNumber + 1;
// Go to previous page
if PdfView1.PageNumber > 1 then
PdfView1.PageNumber := PdfView1.PageNumber - 1;
// Extract all text from the current page
Pdf1.PageNumber := 1;
Memo1.Text := Pdf1.Text;
// Extract text from a rectangular area
var
RectText: WString;
begin
RectText := Pdf1.TextInRectangle(100, 100, 500, 200);
ShowMessage(RectText);
end;
// Find text in the document
var
Position: Integer;
begin
Position := Pdf1.FindFirst('search term');
if Position >= 0 then
ShowMessage('Found at position: ' + IntToStr(Position))
else
ShowMessage('Not found');
end;
// Create a new PDF with text
Pdf1.CreateDocument;
Pdf1.Compressed := True; // enable stream compression
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:\NewDocument.pdf');
Pdf1.Active := False;
// Set zoom level (1.0 = 100%)
PdfView1.Zoom := 1.5; // 150%
// Set display mode
PdfView1.DisplayMode := dmSingleContinuous;
// Rotate page
PdfView1.Rotation := ro90;
// Read document metadata
ShowMessage('Title: ' + Pdf1.Title);
ShowMessage('Author: ' + Pdf1.Author);
ShowMessage('Creator: ' + Pdf1.Creator);
ShowMessage('Producer: ' + Pdf1.Producer);
ShowMessage('Pages: ' + IntToStr(Pdf1.PageCount));
ShowMessage('Version: ' + IntToStr(Ord(Pdf1.PdfVersion)));
// Open a password-protected PDF
Pdf1.FileName := 'C:\Protected.pdf';
Pdf1.Password := 'mypassword';
Pdf1.Active := True;
For multi-GB PDFs or remote / database-backed sources, use
LoadCustomDocument so PDFium pulls blocks on demand
instead of buffering the entire file in memory:
var
FileStream: TFileStream;
begin
FileStream := TFileStream.Create('C:\Huge.pdf', fmOpenRead or fmShareDenyWrite);
Pdf1.LoadCustomDocument(FileStream, True); // AOwnsStream
Pdf1.Active := True;
end;
FitMode keeps long documents framed automatically
on Resize and when the current page changes:
PdfView1.FitMode := pfmFitPage; // whole page in viewport
// pfmFitWidth — page width matches viewport width
// pfmActualSize — 100%
// pfmNone — manual zoom
// Dark-mode viewer pattern: dark scroll area, paper-white PDF page
PdfView1.Color := clBlack;
PdfView1.PageColor := clWhite;
HighlightSearchText scans the current view page and
paints a HighlightColor mask over every match on each
redraw:
var
MatchCount: Integer;
begin
PdfView1.HighlightColor := clYellow;
MatchCount := PdfView1.HighlightSearchText('invoice');
Status.Caption := IntToStr(MatchCount) + ' matches';
end;
// Switching to a different page clears the highlight automatically
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:
if Pdf1.SaveAsPdfA('C:\Report.pdfa.pdf', pac1b) then
ShowMessage('Saved PDF/A-1b');
// Check conformance of an incoming document
case Pdf1.PdfAConformance of
pac1b: Log.Add('PDF/A-1b');
pacNone: Log.Add('Not PDF/A');
end;