Table of Contents
Intro
With the PDF Library for Android by WINSOFT, you can create, process, and render PDF documents in your Delphi/C++ Builder FireMonkey Android applications.
The PDF Library for Android by WINSOFT uses the PdfBox-Android library. So, this is our main library here. Because the PdfBox-Android library offers full control over PDF documents.
The library by the WINSOFT allows us to easily talk with the PdfBox-Android library. You may use IDE Software to integrate the PdfBox-Android library for full optimization and control of PDF documents when creating Android applications.
Specifications
- Uses PdfBox-Android library
- Available for Delphi/C++ Builder 10 – 10.4
Overview
The first thing is to configure the library into your RAD Studio. Please, follow this short tutorial if you don’t have a component/library installing/configuring experience!
Also, watch see this demo action video that shows how this PDF library works in a Delphi FireMonkey application
Development
Every Android application runs in a limited sandbox. If an application needs permission to do something inside Android OS, you should get permission.
Requesting permission is easy in Delphi/C++ Builder FireMonkey. So, you should request a write external storage permission and then you can start processing PDF documents.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
procedure TFormMain.ButtonCreatePdfClick(Sender: TObject); begin {$ifdef D103PLUS} PermissionsService.RequestPermissions([ JStringToString(TJManifest_permission.JavaClass.WRITE_EXTERNAL_STORAGE)], procedure(const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>) begin if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then CreatePdf else ShowMessage('Permission denied'); end); {$else} CreatePdf; {$endif D103PLUS} end; |
Here is our CreatePdf procedure which initializes PDF library, creates a Pdf Document object, adds a new page, and creates a content stream.
You have many features that you can use, for instance:
- Adding Text Content
- Utilize Unicode Font
- Draw Lines
- Draw Shapes
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
procedure TFormMain.CreatePdf; var Document: TPdfDocument; Page: TPdfPage; PageHeight: Single; UnicodeFont: TPdfFont; Content: TPdfPageContentStream; DashPattern: TSingleDynArray; CenterX, CenterY: Single; Matrix: TPdfMatrix; FileName: string; begin PdfLibrary.Initialize; Document := TPdfDocument.Create; try Document.Creator := 'PDF Library for Android'; Document.CreationDate := Now; Page := Document.AddPage; try PageHeight := -Page.MediaBox.Height; Content := Page.CreateContentStream; try // add text Content.BeginText; Content.NonStrokingColor(TAlphaColorRec.Green); Content.Font(TPdfFont.Helvetica, 16); Content.NewLineAtOffset(20, PageHeight - 20); Content.Text('Hello, world!'); Content.Font(TPdfFont.HelveticaBold, 24); Content.NewLineAtOffset(0, -30); Content.Text('Hello, world!'); UnicodeFont := Document.LoadFontAsset('com/tom_roush/pdfbox/resources/ttf/LiberationSans-Regular.ttf'); try Content.Font(UnicodeFont, 24); Content.NewLineAtOffset(0, -30); Content.Text('Hello, World!'); Content.EndText; // draw lines Content.StrokingColor(TAlphaColorRec.Orange); Content.LineWidth(2); Content.MoveTo(20, PageHeight - 100); Content.LineTo(100, PageHeight - 100); Content.Stroke; Content.LineWidth(4); Content.LineCapStyle(TPdfLineCapStyle.lcRound); Content.MoveTo(20, PageHeight - 114); Content.LineTo(150, PageHeight - 114); Content.Stroke; SetLength(DashPattern, 2); DashPattern[0] := 4; DashPattern[1] := 2; Content.LineWidth(8); Content.LineCapStyle(TPdfLineCapStyle.lcButt); Content.LineDashPattern(DashPattern); Content.MoveTo(20, PageHeight - 132); Content.LineTo(200, PageHeight - 132); Content.Stroke; // draw rectangles Content.StrokingColor(TAlphaColorRec.Red); Content.LineWidth(2); Content.Rectangle(TRectF.Create(20, PageHeight - 150, 100, PageHeight - 230)); Content.Stroke; Content.NonStrokingColor(TAlphaColorRec.Red); Content.Rectangle(TRectF.Create(120, PageHeight - 150, 200, PageHeight - 230)); Content.Fill; CenterX := 110; CenterY := PageHeight - 300; Matrix := TPdfMatrix.Create; try Matrix.Translate(CenterX, CenterY); Matrix.Rotate(DegToRad(45)); Matrix.Translate(-CenterX, -CenterY); Content.NonStrokingColor(TAlphaColorRec.LightBlue); Content.Transform(Matrix); Content.Rectangle(TRectF.Create(CenterX - 40, CenterY - 40, CenterX + 40, CenterY + 40)); Content.Fill; Content.StrokingColor(TAlphaColorRec.Blue); Content.LineWidth(2); Content.Rectangle(TRectF.Create(CenterX - 40, CenterY - 40, CenterX + 40, CenterY + 40)); Content.Stroke; Content.Close; // render to bitmap ImageViewer.Bitmap := Document.RenderPage(0); // save to file FileName := TPath.GetSharedDocumentsPath + PathDelim + 'new.pdf'; Document.Save(FileName); ShowMessage('File ' + FileName + ' created'); finally Matrix.Free; end; finally UnicodeFont.Free end; finally Content.Free; end; finally Page.Free; end; finally Document.Free; end; end; |
Head over and check out the full WINSOFT PDF Library for Android
Like what you see? You can get PDF Library for Android 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