Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating PDF file in .NET [closed]

Tags:

.net

pdf

I am using C# .NET 2.0.I want to generate a PDF file . I should write a text in the file.

What are the prerequsites we need to generate the PDF.

Any simplae coding samples will help me.

PLease suggest opensource libraries.

Thanks in advance.

like image 930
Jebli Avatar asked Aug 07 '09 10:08

Jebli


2 Answers

Use PDFSharp

PDFsharp is the Open Source library that easily creates PDF documents from any .NET language. The same drawing routines can be used to create PDF documents, draw on the screen, or send output to any printer.

// 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);

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

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

// Save the document...
string filename = "HelloWorld.pdf";
document.Save(filename);
// ...and start a viewer.
Process.Start(filename);

Samples

like image 72
Sorantis Avatar answered Nov 10 '22 09:11

Sorantis


UPDATE: SharpPDF looks dead. Go with PDFSharp instead...

This question has been asked a few times on SO:

  • How do I programmatically create a PDF in my .NET application?

SharpPDF - with tutorials here.

like image 1
Mitch Wheat Avatar answered Nov 10 '22 09:11

Mitch Wheat