Create PDF document programmatically

Aspose.PDF for .NET API lets you create and read PDF files using C# and VB.NET. The API can be used in a variety of .NET applications including WinForms, ASP.NET, and several others. In this article, we are going to show how to use Aspose.PDF for .NET API to easily generate and read PDF files in .NET applications.

How to Create PDF File using C#

To create a PDF file using C#, the following steps can be used.

  1. Create an object of Document class
  2. Add a Page object to the Pages collection of the Document object
  3. Add TextFragment to Paragraphs collection of the page
  4. Save the resultant PDF document
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_QuickStart();

// Initialize document object
Document document = new Document();
// Add page
Page page = document.Pages.Add();
// Add text to new page
page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Hello World!"));
// Save updated PDF
document.Save(dataDir + "HelloWorld_out.pdf")

In this case, we create a PDF one-page document with A4 page size, portrait orientation. Our page will contain a “Hello, World” in the upper left part of the page.