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();
No related posts.
Categories: C#, Code Snippets
Recent Comments