
Generate PDFs Programmatically
Document Solutions for PDF .NET makes it easy to generate PDF files directly from your .NET applications. Developers can create a new PDF document, add pages, place text at specific coordinates, and save the final file with only a few lines of C# or VB.NET code. This is ideal for building invoices, reports, statements, labels, certificates, and other documents that need to be generated dynamically.
With DsPdf, content can be drawn directly onto the page using a graphics-based API, giving developers precise control over text placement, fonts, sizing, and layout. This approach is especially useful when your application needs to position content exactly, such as placing a title one inch from the top-left corner or aligning generated content to a pre-defined layout.
Aspose.PDF .NET also provides APIs for creating PDFs from scratch, adding pages, inserting text, and saving the result. The code comparison below shows how both products handle a simple “Hello World” PDF generation workflow, including creating the document, adding text, and saving the file.
Aspose PDF:
// NuGet: Install-Package Aspose.PDF
using Aspose.Pdf;
using Aspose.Pdf.Text;
using System;
using System.IO;
class Program
{
static void Main()
{
// Create a new PDF document.
using var document = new Document();
// Add a page.
var page = document.Pages.Add();
// Create text content.
var text = new TextFragment("Hello World!");
// Optional: position the text on the page.
text.Position = new Position(72, 720);
text.TextState.FontSize = 14;
text.TextState.Font = FontRepository.FindFont("Times New Roman");
// Add text to the page.
page.Paragraphs.Add(text);
// Save the PDF document.
document.Save("helloWorld.pdf");
Console.WriteLine("PDF created successfully.");
}
}
DsPdf .NET:
// NuGet: Install-Package DS.Documents.Pdf
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using System;
using System.Drawing;
using System.IO;
class Program
{
static void Main()
{
var outputFile = Path.Combine("Resources", "hello-world.pdf");
// Create a new PDF document.
var doc = new GcPdfDocument();
// Add a page and get its graphics object.
var g = doc.NewPage().Graphics;
// Define the text format.
var textFormat = new TextFormat()
{
Font = StandardFonts.Times,
FontSize = 14
};
// Draw text at a specific position on the page.
// PDF coordinates use points; 72 points equals 1 inch.
g.DrawString(
"Hello World!",
textFormat,
new PointF(72, 72)
);
// Save the PDF document.
doc.Save(outputFile);
Console.WriteLine("PDF created successfully.");
}
}


