Table of Contents
Intro
PDFium Component Suite for FireMonkey has so many capabilities to handle PDF documents. It uses PDFium open-source PDF rendering engine and supports Windows, macOS, iOS, and Android.
PDFium itself is used by the Chromium project. So, you can understand that “ium” comes from Chromium! This is a fully open-source project and you can contribute to that here.
Installation
Since this is a component suite and supports different operating systems, it has its installation file. But, if you are in doubt, you can follow a short video on how to install the PDFium Component Suite for FireMonkey.
Overview
When you successfully install you will get two components on your Palette.
TFPdf handles PDF documents. With that component, you can create and process PDF documents.
TFPdfView is the component that shows the PDF documents in your FireMonkey applications.
What you can do with the PDFium Suite?
- Create PDF documents
- Extract Images from PDF documents
- Extract Text
- Extract Text Pages
- Merge PDF documents
- Convert Pdf To Jpg
Development
Let’s investigate how the PDFium Suite converting PDF documents to Jpg image files.
On Form Create this will get the pixels per inch by using the GetDisplayMetrics function.
1 2 3 4 5 6 7 |
procedure TFormMain.FormCreate(Sender: TObject); var DeviceBehavior: IDeviceBehavior; begin PixelsPerInch := 96; if TBehaviorServices.Current.SupportsBehaviorService(IDeviceBehavior, DeviceBehavior, Self) then PixelsPerInch := DeviceBehavior.GetDisplayMetrics(Self).PixelsPerInch; end; |
The Convertor button starts the operation. It starts from page number 0, from the beginning. And with the RenderPage procedure, you can render the pages. And finally, you can save them to the TBitmap based data to files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
procedure TFormMain.ButtonConvertClick(Sender: TObject); var I: Integer; FileName: string; Bitmap: TBitmap; begin ProgressBar.Value := 0; EditPdfFile.Enabled := False; ButtonPdfFile.Enabled := False; ButtonConvert.Enabled := False; FormMain.Cursor := crHourGlass; try FPdf.FileName := EditPdfFile.Text; FPdf.PageNumber := 0; FPdf.Active := True; ProgressBar.Max := FPdf.PageCount; FileName := ExtractFileName(FPdf.FileName); FileName := Copy(FileName, 1, Length(FileName) - Length(ExtractFileExt(FPdf.FileName))); FileName := ExtractFilePath(FPdf.FileName) + FileName; for I := 1 to FPdf.PageCount do begin FPdf.PageNumber := I; Bitmap := FPdf.RenderPage(0, 0, Round(PointsToPixels(FPdf.PageWidth, PixelsPerInch)), Round(PointsToPixels(FPdf.PageHeight, PixelsPerInch))); try Bitmap.SaveToFile(FileName + '_Page' + IntToStr(I) + '.jpg'); finally Bitmap.Free; end; ProgressBar.Value := I; Application.ProcessMessages; end; finally FormMain.Cursor := crDefault; FPdf.Active := False; EditPdfFile.Enabled := True; ButtonPdfFile.Enabled := True; ButtonConvert.Enabled := True; end; end; |
Extracting Images from PDF document
Taking file name and path and setting the initial point of the process is similar, but when you want to get images from PDF document. You can just iterate over the TFPdf using the BitmapCount and you can receive the images by its index.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
for I := 1 to FPdf.PageCount do begin FPdf.PageNumber := I; for J := 0 to FPdf.BitmapCount - 1 do begin Bitmap := FPdf.Bitmap[J]; try Bitmap.SaveToFile(FileName + '_Page' + IntToStr(I) + '_Image' + IntToStr(J + 1) + '.bmp'); finally Bitmap.Free; end; end; ProgressBar.Value := I; Application.ProcessMessages; end; |
Extracting Text
By calling Text function you can fetch the text from PDF documents and save the content to a text file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
FileStream := TFileStream.Create(FileName + '.txt', fmCreate); try for I := 1 to FPdf.PageCount do begin FPdf.PageNumber := I; Text := UTF8Encode(FPdf.Text); if Text <> '' then begin FileStream.WriteBuffer(Text[1], Length(Text)); FileStream.WriteBuffer(NewLine[1], Length(NewLine)); FileStream.WriteBuffer(NewLine[1], Length(NewLine)); end; ProgressBar.Value := I; Application.ProcessMessages; end; finally FileStream.Free; end; |
Be sure to check out the demo action video that shows all the demo projects in action.
Head over and check out the full WINSOFT PDFium for FireMonkey Library
Like what you see? You can get PDFium for FireMonkey and over 100 other fantastic WinSoft components with our Enterprise Component Pack. For a limited time, when you purchase RAD Studio Enterprise or Architect Edition at special Upgrade Pricing, you will also get this package of third-party software worth over $13,000, including the full WinSoft Component Library, at NO EXTRA COST! Step up to RAD Studio 10.4.1 today!
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
Is it possible to save in PDF/A format? And to sign with certificate?
From what I can see it looks like PDFium cannot produce PDF/A format files. I do see that most of the component vendors have support for creating PDF/A though. You could try one of them.
This link talks about it: https://stackoverflow.com/questions/4804935/report-generators-that-can-produce-pdf-a-compliant-files
So does this from TMS Software: https://doc.tmssoftware.com/flexcel/vcl/samples/delphi/printing-and-exporting/pdfa/index.html
and Fast Report: https://www.fast-report.com/en/blog/show/export-pdf-a-delphi/
and WpCubed: http://www.wpcubed.com/pdf/products/wpdf/pdfa/
Should this work with the latest version ? I tried installing, but it says it can’t load the BPL for it when I launch Delphi. Looking at the installation folder it’s referring to, I don’t see a BPL there
Yes it should work with version 11 – at least it says so on their website. You might have to contact WinSoft for support to resolve the issue.
Buenos dias, tengo un problema con la libreria pdfium. He intentado ejecutarlo desde diferentes versiones de delphi (10.3,10,4 y 12) y en todas cuando lo ejecuto me pone “No se puede encontrar el modulo especificado :PDFIUM.dll “. Tengo indicado en la libreria de delphi la ruta hacia la dll, e incluso lo he introducido como archivo de la aplicacion. Pero me sigue indicando el mismo error. Estoy intentado ejecutarlo como aplicacion de escritorio win 32. Rogaria me indicaran si estoy cometiendo algun error. Muchas Gracias y felices fiestas.
The best place to get help with PDFium is to use this forum: https://bugs.chromium.org/p/pdfium/issues/list
Não tem suporte ao Android
You can try this library instead for Android: https://www.winsoft.sk/apdflibrary.htm
or use Skia4Delphi to create PDFs – their example works fine on Android, and Skia is built into RAD Studio with Delphi 12.2: https://github.com/skia4delphi/skia4delphi/tree/main/Samples/Demo/FMX/Projects