Creating PDF documents from code offers a wide range of benefits. For example, you can easily incorporate dynamic content such as user input, database records, or real-time data. Code-based PDF generation allows for greater customization and automation, minimizing manual intervention in creating highly tailored documents. In this article, you will learn how to create a PDF document from scratch in C# and VB.NET using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Background Knowledge
A page in Spire.PDF (represented by PdfPageBase) consists of client area and margins all around. The content area is for users to write various contents, and the margins are usually blank edges.
As shown in the figure below, the origin of the coordinate system on the page is located at the top left corner of the client area, with the x-axis extending horizontally to the right and the y-axis extending vertically down. All elements added to the client area must be based on the specified coordinates.
In addition, the following table lists the important classes and methods, which can help you easily understand the code snippet provided in the following section.
Member | Description |
PdfDocument class | Represents a PDF document model. |
PdfPageBase class | Represents a page in a PDF document. |
PdfSolidBrush class | Represents a brush that fills any object with a solid color. |
PdfTrueTypeFont class | Represents a true type font. |
PdfStringFormat class | Represents text format information, such as alignment, characters spacing and indent. |
PdfTextWidget class | Represents the text area with the ability to span several pages. |
PdfTextLayout class | Represents the text layout information. |
PdfDocument.Pages.Add() method | Adds a page to a PDF document. |
PdfPageBase.Canvas.DrawString() method | Draws string on a page at the specified location with specified font and brush objects. |
PdfTextWidget.Draw() method | Draws the text widget on a page at the specified location. |
PdfDocument.Save() method | Saves the document to a PDF file. |
Create a PDF Document from Scratch in C# and VB.NET
Although Spire.PDF for .NET supports adding various kinds of elements to PDF documents, this article only demonstrates how to create a PDF document with plain text. The following are the detailed steps.
- Create a PdfDocument object.
- Add a page using PdfDocument.Pages.Add() method.
- Create brush and font objects.
- Draw string on the page at a specified coordinate using PdfPageBase.Canvas.DrawString() method.
- Create a PdfTextWidget object to hold a chunk of text.
- Draw the text widget on the page at a specified location using PdfTextWidget.Draw() method
- Save the document to a PDF file using PdfDocument.Save() method.
- C#
- VB.NET
using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; namespace CreatePdfDocument { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument doc = new PdfDocument(); //Add a page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins(35f)); //Specify heading text String titleText = "What is MySQL"; //Create solid brushes PdfSolidBrush titleBrush = new PdfSolidBrush(new PdfRGBColor(Color.Blue)); PdfSolidBrush paraBrush = new PdfSolidBrush(new PdfRGBColor(Color.Black)); //Create true type fonts PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new Font("Times New Roman", 18f, FontStyle.Bold),true); PdfTrueTypeFont paraFont = new PdfTrueTypeFont(new Font("Times New Roman", 12f, FontStyle.Regular), true); //Set the text alignment via PdfStringFormat class PdfStringFormat format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Center; //Draw heading on the center of the page page.Canvas.DrawString(titleText, titleFont, titleBrush, page.Canvas.ClientSize.Width / 2, 20, format); //Get paragraph content from a .txt file string paraText = File.ReadAllText("C:\\Users\\Administrator\\Desktop\\content.txt"); //Create a PdfTextWidget object to hold the paragrah content PdfTextWidget widget = new PdfTextWidget(paraText, paraFont, paraBrush); //Create a rectangle where the paragraph content will be placed RectangleF rect = new RectangleF(0, 50, page.Canvas.ClientSize.Width, page.Canvas.ClientSize.Height); //Set the PdfLayoutType to Paginate to make the content paginated automatically PdfTextLayout layout = new PdfTextLayout(); layout.Layout = PdfLayoutType.Paginate; //Draw the widget on the page widget.Draw(page, rect, layout); //Save to file doc.SaveToFile("CreatePdfDocument.pdf"); doc.Dispose(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.