Quick And Easy Way To Create And Manage PDF Documents Programmatically
The Portable Document Format (commonly known as “PDF”) is a file format developed in the early 1990s as a way to share documents created by computers, including text formatting and inline images. PDF was designed to allow viewing documents independent of the application software, operating system and hardware used to create them.
PDF files encapsulate a complete description of a fixed-layout document, including the text, fonts, graphics, and other information needed to display it. PDF files may also include a wide variety of other content, from hyperlinks to metadata to logical structure to JavaScript and attached files, that allow the format to meet a wide variety of functional and workflow needs for electronic documents.
If you need to handle creation and manipulation of PDF documents on your existing or new application, Winsoft .sk PDF Library is the easiest way to do it. Key features are:
- Uses PDFsharp library
- Supports Windows 32 and Windows 64
- Available for Delphi/C++ Builder 7 – 10.4
- Royalty free distribution in applications
Installation
There is no need of installing this component. All features are available via runtime. You just need to download lib from https://winsoft.sk/pdflibrary.htm and copy to your preferred Delphi components folder.
This is a non-visual library, so you don’t need to install it on IDE.
Add folder according your Delphi version and Platform in Tools->Options->Language->Delphi->Library to Library Path

Samples
For all examples bellow, you need to add ShellApi, PdfSharp unit to your uses clauses and declare a generic OpenPDF procedure as follow:
procedure OpenPdf(const FileName: string);
begin
ShellExecute(0, 'open', PChar(FileName), nil, nil, SW_SHOWNORMAL)
end;
Creating a Basic PDF File
procedure CreateBasicPDFFile;
var
Document: _PdfDocument;
Page: _PdfPage;
Graphics: _XGraphics;
Font: _XFont;
Rect: XRect;
Brush: _XBrush;
FileName: string;
begin
// create Document and Page objects
Document := CoPdfDocument.Create;
Page := Document.AddPage;
// Font and Brush to be used on text writing
Font := CoXFontClass.Create.XFont_2('Arial', 20, XFontStyle_BoldItalic);
Brush := CoXBrushesClass.Create.Blue.XBrush;
// coordinates on where text will be drawn
Rect._x := 0;
Rect._y := 0;
Rect._width := Page.Width._value;
Rect._height := Page.Height._value;
// Create Graphic object wrere text will be drawn
Graphics := CoXGraphicsClass.Create.FromPdfPage(Page);
Graphics.DrawString_6('Hello, world!', Font, Brush, Rect, CoXStringFormatsClass.Create.Center);
// Save generated page to file
FileName := GetCurrentDir + 'document.pdf';
Document.Save(FileName);
// Open just created PDF file on default PDF application
OpenPdf(FileName);
end;
Create PDF with some drawings and Bitmap file attached
procedure CreateDrawingAndBitmapPDF;
var
Document: _PdfDocument;
Page: _PdfPage;
Graphics: _XGraphics;
Pen: _XPen;
Brush: _XBrush;
FileName: string;
begin
// Create document object
Document := CoPdfDocument.Create;
Document.Info.Title1 := 'Graphics example';
// Create Page and Graphics object
Page := Document.AddPage;
Graphics := CoXGraphicsClass.Create.FromPdfPage(Page);
// Create 3 pens of diffrent colors for draw rectangles at fixed positions
Pen := CoXPensClass.Create.Teal;
Brush := CoXBrushesClass.Create.LightGreen.XBrush;
Graphics.DrawRectangle_6(Pen, Brush, 60, 40, 200, 90);
Pen := CoXPensClass.Create.Black;
Brush := CoXBrushesClass.Create.Yellow.XBrush;
Graphics.DrawRoundedRectangle_6(Pen, Brush, 280, 40, 200, 90, 50, 50);
Pen := CoXPensClass.Create.Red;
Brush := CoXBrushesClass.Create.LightPink.XBrush;
Graphics.DrawPie_6(Pen, Brush, 60, 150, 200, 200, 120, 270);
Pen := CoXPensClass.Create.Blue;
Brush := CoXBrushesClass.Create.LightBlue.XBrush;
Graphics.DrawEllipse_6(Pen, Brush, 280, 170, 200, 160);
Pen := CoXPensClass.Create.Cyan;
Graphics.DrawLine_2(Pen, 60, 400, 260, 400);
// End drawing rectangles
// nsert log.bmp on pdf file
Graphics.DrawImage_2(CoXImageClass.Create.FromFile(GetCurrentDir + 'logo.bmp'), 280, 350);
// Save created document to file
FileName := GetCurrentDir + 'graphics.pdf';
Document.Save(FileName);
// Open pdf file with default application to open PDF files
OpenPdf(FileName);
end;
Protecting PDF files
// code for creating Docuemnt object.
// also you could load some existing document
Document.SecuritySettings.UserPassword := 'user';
Document.SecuritySettings.OwnerPassword := 'owner';
Document.SecuritySettings.PermitPrint := False;
Document.SecuritySettings.PermitExtractContent := False;
//
// Save document with new credentials and permissions
FileName := GetCurrentDir + 'protected.pdf';
Document.Save(FileName);
Head over and find out more about the WINSOFT PDF library for Delphi and then download it.
Leave Your Comment