Saturday, November 8, 2008

create PDF file using C#.net

use below link to download pdfsharp dll file

http://sourceforge.net/projects/pdfsharp/

download the file and add reference to ur program then only u can create pdf file


code:

using System;
using System.Diagnostics;
using System.IO;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
namespace HelloWorld
{
class Program
{
[STAThread] static void Main(string[] args)
{

// Create a new PDF document
PdfDocument document = new PdfDocument();

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

// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);

XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);

// Create a font
XFont font = new XFont("Arial", 20, XFontStyle.Bold, options);

// Draw the text
gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);

// Save the document...
string filename = "HelloWorld.pdf";
document.Save(filename);

// ...and start a viewer.
Process.Start(filename);
}
}
}

No comments: