Getting Started | |
To start using SelectPdf Library, follow the instructions from the Installation section to set up a project and add the SelectPdf.Universal NuGet package (assembly SelectPdf.Universal.dll).
This section will show how to create a simple 'Hello World!' application with Select.Pdf.
The first thing to do when using Select.Pdf library is to create the pdf document that is an instance of PdfDocument class.
Once the document is created, a first page is need in the pdf document. To do that, the method AddPage of the PdfDocument class can be used. A page in a pdf document is an instance of the PdfPage class.
To add a text to a pdf document, a font is needed. A font in a pdf document is an instance of the PdfFont class.
To add a text to a pdf document, the PdfTextElement class can be used. After the PdfTextElement object is created, it must be added to the appropriate PdfPage object.
Once the content is created, you can save the document. The document can be saved to a stream or file on disk.
PdfDocument implements IDisposable, so a using statement closes it too. Calling Close and disposing the document are the same; doing both is safe.
The full code of the 'Hello World!' application looks like this:
// create a new pdf document PdfDocument doc = new PdfDocument(); // add a new page to the document PdfPage page = doc.AddPage(); // create a new pdf font PdfFont font = doc.Fonts.Add(PdfStandardFont.Helvetica); font.Size = 20; // create a new text element and add it to the page PdfTextElement text = new PdfTextElement(50, 50, "Hello world!", font); page.Add(text); // save pdf document doc.Save(file); // close pdf document doc.Close();