PDFium Component Docs

빠른 시작 가이드

이 가이드는 단 몇 분 만에 PDFium VCL 컴포넌트를 시작하는 데 도움이 됩니다.

1단계: 새 프로젝트 만들기

  1. IDE에서 새 VCL Forms Application을 만듭니다
  2. 편리한 위치에 프로젝트를 저장합니다

2단계: 폼에 컴포넌트 추가

  1. losLab 컴포넌트 팔레트를 엽니다
  2. TPdf 컴포넌트를 폼에 드롭합니다
  3. TPdfView 컴포넌트를 폼에 드롭합니다
  4. TPdfView의 Pdf 속성을 TPdf 컴포넌트를 가리키도록 설정합니다

3단계: 첫 번째 PDF 로드

간단한 PDF 로딩:

// PDF 파일 로드
Pdf1.FileName := 'C:\Sample.pdf';
Pdf1.Active := True;
ShowMessage('Pages: ' + IntToStr(Pdf1.PageCount));

// 또는 메모리 스트림에서 로드
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;

4단계: 기본 PDF 작업

페이지 탐색

// 다음 페이지로 이동
if PdfView1.PageNumber < Pdf1.PageCount then
  PdfView1.PageNumber := PdfView1.PageNumber + 1;

// 이전 페이지로 이동
if PdfView1.PageNumber > 1 then
  PdfView1.PageNumber := PdfView1.PageNumber - 1;

텍스트 추출

// 현재 페이지에서 모든 텍스트 추출
Pdf1.PageNumber := 1;
Memo1.Text := Pdf1.Text;

// 사각형 영역에서 텍스트 추출
var
  RectText: WString;
begin
  RectText := Pdf1.TextInRectangle(100, 100, 500, 200);
  ShowMessage(RectText);
end;

텍스트 검색

// 문서에서 텍스트 찾기
var
  Position: Integer;
begin
  Position := Pdf1.FindFirst('search term');
  if Position >= 0 then
    ShowMessage('Found at position: ' + IntToStr(Position))
  else
    ShowMessage('Not found');
end;

새 PDF 만들기

// 텍스트가 포함된 새 PDF 만들기
Pdf1.CreateDocument;
Pdf1.Compressed := True; // 스트림 압축 활성화
Pdf1.Active := True;
Pdf1.AddPage(0, 595, 842); // A4 크기
Pdf1.AddText('Hello World!', 'Arial', 24, 100, 700, clBlack, $FF, 0.0);
Pdf1.SaveAs('C:\NewDocument.pdf');
Pdf1.Active := False;

줌 및 표시

// 줌 레벨 설정 (1.0 = 100%)
PdfView1.Zoom := 1.5; // 150%

// 표시 모드 설정
PdfView1.DisplayMode := dmSingleContinuous;

// 페이지 회전
PdfView1.Rotation := ro90;

5단계: 문서 속성

// 문서 메타데이터 읽기
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)));

6단계: 암호로 보호된 문서

// 암호로 보호된 PDF 열기
Pdf1.FileName := 'C:\Protected.pdf';
Pdf1.Password := 'mypassword';
Pdf1.Active := True;

7단계: 대용량 문서 (스트리밍 로드)

멀티 GB PDF 또는 원격 / 데이터베이스 기반 소스의 경우 PDFium이 전체 파일을 메모리에 버퍼링하는 대신 필요에 따라 블록을 가져오도록 LoadCustomDocument를 사용합니다.

var
  FileStream: TFileStream;
begin
  FileStream := TFileStream.Create('C:\Huge.pdf', fmOpenRead or fmShareDenyWrite);
  Pdf1.LoadCustomDocument(FileStream, True); // AOwnsStream
  Pdf1.Active := True;
end;

8단계: 페이지 레이아웃 (TPdfView Fit 모드)

FitMode는 크기 조정 시 그리고 현재 페이지가 변경될 때 긴 문서를 자동으로 프레임에 맞춥니다.

PdfView1.FitMode := pfmFitPage; // 전체 페이지가 뷰포트에
// pfmFitWidth — 페이지 너비가 뷰포트 너비와 일치
// pfmActualSize — 100%
// pfmNone — 수동 줌

// 다크 모드 뷰어 패턴: 어두운 스크롤 영역, 종이 흰색 PDF 페이지
PdfView1.Color := clBlack;
PdfView1.PageColor := clWhite;

9단계: 검색 강조 표시

HighlightSearchText는 현재 뷰 페이지를 스캔하고 다시 그리기 때마다 모든 일치 항목 위에 HighlightColor 마스크를 그립니다.

var
  MatchCount: Integer;
begin
  PdfView1.HighlightColor := clYellow;
  MatchCount := PdfView1.HighlightSearchText('invoice');
  Status.Caption := IntToStr(MatchCount) + ' matches';
end;

// 다른 페이지로 전환하면 강조 표시가 자동으로 지워집니다

10단계: PDF/A 아카이브 출력

SaveAsPdfA는 XMP 메타데이터 스트림, sRGB ICC OutputIntent 및 업데이트된 Catalog를 주입하는 증분 업데이트로 기본 저장을 사후 처리합니다.

if Pdf1.SaveAsPdfA('C:\Report.pdfa.pdf', pac1b) then
  ShowMessage('Saved PDF/A-1b');

// 수신 문서의 준수성 확인
case Pdf1.PdfAConformance of
  pac1b: Log.Add('PDF/A-1b');
  pacNone: Log.Add('Not PDF/A');
end;

다음 단계