Creating PDF Documents on the Fly

PDF (Portable Document Format) has evolved as the most widely used file format in the IT industry. This file format is used to present documents in a manner independent of Application Software, Hardware and Operating Systems. Each PDF file encapsulates a complete description of a fixed layout flat document, including text, fonts, graphics, etc. In this technology emerging era, we need to create such type of high-quality portable files using the programming languages or codes so as to automate the process. One of the important components in Studio for Winforms is theC1PDF for .NET which allows to create high-quality, device-independent, portable documents from the applications.

Creating a PDF Document and Adding Text

Add C1PDFDocument on the Form in your application, say pdf. It is basically a tray control. Now, use its DrawString method to write Text in the document.

pdf.DrawString(@"Grapecity - ComponentOne", new Font("Arial", 20), Brushes.DarkBlue, new PointF(200,10));  

The above code will generate a document like the following image. img11

Adding Image

C1PDF provides a method DrawImage to add images in the document. You can set the Image as well as its Location and Size on the page in the arguments of this method.

pdf.DrawImage(Image.FromFile("..\\\..\\\C1Logo.png"), new RectangleF(250,0,120,120));  


Adding RTF Text

C1PDF even supports writing RTF Text to a document through the DrawStringRTF method. It supports all the RTF tags to customize the string that has to be written on the page.

Font font = new Font("Arial", 50);  
RectangleF rect = pdf.PageRectangle;  
rect.Inflate(0, -130);  
pdf.DrawStringRtf(@"{\\b\\qc \*Document Heading\* \\par}", font, Brushes.Black, rect);  

Here, Font class has been used to create a new Font setting. This font setting can be passed in the arguments of the DrawStringRTF method to define the Font of the text. img33

Adding Graphics

C1PDF supports adding of any type of Graphics object like Shapes (Circle, Square, Rectangle etc.) and that too in two forms: Filled and Outlined. There are several methods available to draw these shapes, namely, DrawRectangle, DrawArc, DrawEllipse, FillRectangle, FillArc, FillEllipse etc. These methods can be further used to create a Chart (for example) like Image in the Document.

pdf.DrawRectangle(Pens.Black, 110, 520, 400, 250);  
pdf.FillRectangle(Brushes.SandyBrown, 140, 530, 10, 210);  
pdf.FillRectangle(Brushes.SandyBrown, 140, 730, 360, 10);  
pdf.FillRectangle(Brushes.Red, 180, 580, 20, 150);  
pdf.FillRectangle(Brushes.Green, 230, 610, 20, 120);  
pdf.FillRectangle(Brushes.Chocolate , 280, 640, 20, 90);  
pdf.FillRectangle(Brushes.Purple, 330, 560, 20, 170);  
pdf.FillRectangle(Brushes.Orange, 380, 590, 20, 140);  
PointF point1 = new PointF(130, 540);  
for (int i = 0; i < 10; i++)  
pdf.DrawLine(Pens.Brown, point1.X, point1.Y, point1.X+15, point1.Y);  
point1.Y += 20;  
point1 = new PointF(160, 735);  
for (int i = 0; i < 17; i++)  
pdf.DrawLine(Pens.Brown, point1.X, point1.Y, point1.X, point1.Y+15);  
point1.X += 20;  


Adding Security

If the PDF documents contain sensitive information, they can be encrypted so that only authorized users can access them. C1PDF provides a PDFSecurity class that provides various properties to apply security settings to the document. This object can be accessed by using Security property of C1PDFDocument. It includes the properties that allow to specify owner and user passwords for the document and modify the accessibility for the users.

pdf.Security.OwnerPassword = "owner";  
pdf.Security.UserPassword = "user";  
pdf.Security.AllowEditAnnotations = false;  
pdf.Security.AllowEditContent = false;  
pdf.Security.AllowPrint = false;  

So whenever the document will be opened, it will ask for the password and depending on the password entered, the document security settings will be applied. . Security

Resultant PDF Document

After summing up all the objects discussed above and the code snippets, the PDF document will be completed and will look like the below image.


Moreover, C1PDF also supports adding Files Attachment Links, Hyperlinks, Internal Target Links, Bookmarks, etc. Want to know more about Usage, visit here. Download Sample - C#
Download Sample - VB


GrapeCity Developer Tools
comments powered by Disqus