Home > C#, Code Snippets > iTextSharp Hello World sample

iTextSharp Hello World sample

Get a version of iTextSharp from sourceforge first. Add a reference to the core dll – itextsharp.dll in the project.

Use this in all the scripts:

using iTextSharp.text;
using iTextSharp.text.pdf;

Here goes the beginner’s hello world code snippet:

 

Document doc = new Document();
FileStream fs = new FileStream("c:\\test.pdf", FileMode.Create);
PdfWriter pw = PdfWriter.GetInstance(doc, fs);
doc.Open();
doc.Add(new Paragraph("Hello World"));
doc.Close();
fs.Close();

 

Below code snippet can be used to position text:

 

Document doc = new Document(PageSize.A4);
FileStream fs = new FileStream("c:\\test.pdf", FileMode.Create);
PdfWriter pw = PdfWriter.GetInstance(doc, fs);
doc.Open();
PdfContentByte cb = pw.DirectContent;
BaseFont bfnt = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, 
	BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
cb.SetFontAndSize(bfnt, 50);
cb.SetTextMatrix(200, 600);
cb.ShowText("Hello World");
doc.Close();
fs.Close();
VN:F [1.9.18_1163]
Rating: 5.0/5 (2 votes cast)
iTextSharp Hello World sample, 5.0 out of 5 based on 2 ratings

No related posts.

Categories: C#, Code Snippets