I will be using Spire.NET to generate various types in PDFs. It is very simple, fast and easy to use.
To use this library, you need not install Acrobat Reader and the fabulous thing is that it can be integrated with .NET, so using the C# or VB.NET language we can create our PDF document easily.
Let me show some examples that will provide you a clear idea of generating a PDF. I will try to show the following combination of PDFs:
- With text accepted from a textbox.
- With an image.
- Display the grid details shown on a screen into a PDF.
- Password-protected PDF.
The following is the procedure to generate the PDF document:
Step 1
UI Code
- <asp:TextBox ID="txtFreeText" runat="server" TextMode="MultiLine" Height="146px" Width="465px"></asp:TextBox>
- <br />
- <h4>Upload Image</h4>
- <asp:FileUpload ID="FileUpload1" runat="server" />
- <br />
- <h4>Customers</h4>
- <asp:GridView ID="grdNames" runat="server" AutoGenerateColumns="false" GridLines="Vertical" BorderColor="Blue">
- <Columns>
- <asp:BoundField DataField="Id" HeaderText="Id" />
- <asp:BoundField DataField="FirstName" HeaderText="First Name" />
- <asp:BoundField DataField="LastName" HeaderText="Last Name" />
- </Columns>
- </asp:GridView>
- <br/>
- <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Load PDF" />
Step 2
Add the reference of the Spire.PDF DLL to your project. You will also get this from Nugget.
Step 3
On the click event of the button:
- Create an object of PDF document as in the following:
- PDFDocument doc = new PDFDocument();
- Add a section for the content as in the following:
- PDFSection section = doc.Sections.Add();
- Now you need a content place holder for where to load the actual content, so add a page to the PDF document.
- PDFPageBase page = section.Pages.Add();
Creating PDF document with Text
Using the Canvas.DrawString method, add the content. Here I am using one textbox in my web page and loaded the content from that text box.
Code for adding text object
- private void LoadText(PDFSection section)
- {
- PDFPageBase page = section.Pages.Add();
- page.Canvas.DrawString(txtFreeText.Text,
- new PDFFont(PDFFontFamily.Helvetica, 20f),
- new PDFSolidBrush(Color.Red),
- 10, 10);
- }
OutputCreating PDF with image
Loading the image and adding it in a PDF document is simple.
Create an object of PDFImage that takes a parameter of a Stream object. Here I have used a FileUpload control in my web page and passed the InputStream as in the following code snippet:
- PDFImage image = PDFImage.FromStream(FileUpload1.PostedFile.InputStream);
- private void LoadImage(PdfSection section)
- {
- if (FileUpload1.HasFile)
- {
- PdfPageBase page = section.Pages.Add();
- PdfImage image = PdfImage.FromStream(FileUpload1.PostedFile.InputStream);
- float width = image.Width * 0.50f;
- float height = image.Height * 0.50f;
- float x = (page.Canvas.ClientSize.Width - width) / 2;
-
- page.Canvas.DrawImage(image, x, 60, width, height);
- }
- }
OutputCreating PDF with a grid
This is the most important type of data representation that we usually require in our application, showing the data in a grid format. This tool provides an easy way to create a grid structure.
Here we create an object of PDFTable and then using a DataSource property provides a collection object. I want to show First Name and Last Name in my grid.
- PDFTable table = new PDFTable();
Code for generating grid
- PdfPageBase page = section.Pages.Add();
-
-
- PdfTable table = new PdfTable();
- table.Style.CellPadding = 2;
- table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.SkyBlue;
- table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f));
-
- table.Style.AlternateStyle = new PdfCellStyle();
- table.Style.AlternateStyle.BackgroundBrush = PdfBrushes.LightYellow;
- table.Style.AlternateStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f));
-
- table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions;
- table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
- table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold));
- table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
-
- table.Style.ShowHeader = true;
-
- table.DataSourceType = PdfTableDataSourceType.TableDirect;
-
-
- table.DataSource = GenerateList();
-
-
- float width = page.Canvas.ClientSize.Width - (table.Columns.Count + 1);
- table.Columns[0].Width = width * 0.24f * width;
- table.Columns[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
- table.Columns[1].Width = width * 0.21f * width;
- table.Columns[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
- table.Columns[2].Width = width * 0.24f * width;
- table.Columns[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
- table.Draw(page, new PointF(0, 10));
Output
Creating Password protected PDF
One more feature about this component is to provide security to your PDF file, which means creating a password-protected PDF. The following is the code snippet to generate the password-protected file.
- doc.Security.KeySize = PdfEncryptionKeySize.Key128Bit;
- doc.Security.OwnerPassword = "pradeep";
- doc.Security.UserPassword = "123";
- doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.FillFields;
There are other features of this library that you may need at another time. It can actually not only load text, images or grid, you can show much other data as in the following:
- Adding Watermark
- Drawing objects
- Adding action to the links within the PDF
- Adding Paging
- Generating Barcodes
- Merging more than one PDF into single PDF
- Splitting one PDF into multiple
Conclusion
Here I end my article about generating a PDF that I think was easy to use and simple to understand. So try creating it yourself. I hope you liked this article and please do comment even if it is good or bad. Your comment will definitely help me to get better and write better for you.
References: