Easily and quickly create a PDF document in Visual Studio C# Class.

PDF (Portable Document Format) is a file format that has replicate all the characteristics of a printed document that you can read, write, print or forward to someone else. You can create PDF file programmatically from C# applications very easily. When you create documents, graphics etc. they look just like they would if printed.

In XsPDF you are responsible for controlling every aspect of the rendering process which is done by drawing lines, strings and shapes at specified coordinates on a PDF graphics context. The toolkit has mimicked the Microsoft GDI+ interface and thus many of the APIs and objects will be familiar to developers with GDI+ experience.

XsPDF library allows you to create PDF files directly from your C# application.


// Create a new PDF document.
PdfDocument document = new PdfDocument();
document.Info.Title = "Created with XsPDF SDK";
document.Info.Author = "sample";

// Create an empty page in this document.
PdfPage page1 = document.AddPage();

// Obtain an XGraphics object to render to
XGraphics g1 = XGraphics.FromPdfPage(page1);

// Get the centre of the page
double y = page1.Height / 2;

//Draw one line in the center of page
g1.DrawLine(XPens.Red, 0, y, page1.Width, y);

// Create a font
double fontHeight = 36;
XFont font = new XFont("Times New Roman", fontHeight, XFontStyle.BoldItalic);

// Create a rectangle to draw the text in and draw in it
XRect rect = new XRect(0, y, page1.Width, fontHeight);
g1.DrawString("This is the first page! ", font,
              XBrushes.Black, rect, XStringFormats.Center);

// Create a second page in this document.
PdfPage page2 = document.AddPage();

// Get an XGraphics object for drawing
//You can customize each page content with graphics from each PDF page 
XGraphics g2 = XGraphics.FromPdfPage(page2);

//Draw one line in the center of page
g2.DrawLine(XPens.Red, 0, y, page1.Width, y);

// Add different text in another page
g2.DrawString("This is the second page! ", font,
             XBrushes.Black, rect, XStringFormats.Center);

// Save and show the document
document.Save("Sample-Document.pdf");
Process.Start("Sample-Document.pdf");

More PDF tutorial