คู่มือนี้จะช่วยให้คุณเริ่มใช้งานคอมโพเนนต์ PDFium VCL ได้ภายในไม่กี่นาที
Pdf ของ TPdfView ให้ชี้ไปยังคอมโพเนนต์ TPdf ของคุณการโหลด PDF แบบง่าย:
// โหลดไฟล์ PDF
Pdf1.FileName := 'C:\Sample.pdf';
Pdf1.Active := True;
ShowMessage('Pages: ' + IntToStr(Pdf1.PageCount));
// หรือโหลดจาก 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;
// ไปยังหน้าถัดไป
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 ใหม่พร้อมข้อความ
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;
// อ่านข้อมูลเมตาของเอกสาร
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)));
// เปิด PDF ที่มีการป้องกันด้วยรหัสผ่าน
Pdf1.FileName := 'C:\Protected.pdf';
Pdf1.Password := 'mypassword';
Pdf1.Active := True;
สำหรับ PDF ขนาดหลาย GB หรือแหล่งข้อมูลจากระยะไกล / ฐานข้อมูล ให้ใช้
LoadCustomDocument เพื่อให้ PDFium ดึงข้อมูลเป็นบล็อกตามต้องการ
แทนที่จะบัฟเฟอร์ไฟล์ทั้งหมดไว้ในหน่วยความจำ:
var
FileStream: TFileStream;
begin
FileStream := TFileStream.Create('C:\Huge.pdf', fmOpenRead or fmShareDenyWrite);
Pdf1.LoadCustomDocument(FileStream, True); // AOwnsStream
Pdf1.Active := True;
end;
FitMode ช่วยให้เอกสารยาวอยู่ในกรอบอย่างอัตโนมัติ
เมื่อ Resize และเมื่อหน้าปัจจุบันเปลี่ยนไป:
PdfView1.FitMode := pfmFitPage; // ทั้งหน้าพอดีในวิวพอร์ต
// pfmFitWidth — ความกว้างหน้าพอดีกับความกว้างวิวพอร์ต
// pfmActualSize — 100%
// pfmNone — ซูมด้วยตนเอง
// รูปแบบโปรแกรมดูโหมดมืด: พื้นที่เลื่อนสีมืด, หน้า PDF สีขาวกระดาษ
PdfView1.Color := clBlack;
PdfView1.PageColor := clWhite;
HighlightSearchText สแกนหน้าที่กำลังแสดงอยู่และ
ลงมาสก์ HighlightColor ทับทุกผลลัพธ์ที่ตรงกันในแต่ละครั้ง
ที่วาดซ้ำ:
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 จะประมวลผลการบันทึกพื้นฐานเพิ่มเติมด้วย
incremental update ที่แทรก XMP metadata stream, sRGB
ICC OutputIntent และ document catalog ที่อัปเดตแล้ว:
if Pdf1.SaveAsPdfA('C:\Report.pdfa.pdf', pac1b) then
ShowMessage('Saved PDF/A-1b');
// ตรวจสอบความสอดคล้อง (conformance) ของเอกสารที่รับเข้ามา
case Pdf1.PdfAConformance of
pac1b: Log.Add('PDF/A-1b');
pacNone: Log.Add('Not PDF/A');
end;