Generate PDF using iTextSharp

In this post I am going to explain in short how you can generate PDF from scratch using ITextSharp

First Install Itext Starp using the the command in your Visual Studio Package Manager Console

Install-Package iTextSharp

Check your project  reference , it must have been added by now.

Then include reference in your class

using iTextSharp;

using iTextSharp.text;

using iTextSharp.text.pdf;

using iTextSharp.text.pdf.parser;

using iTextSharp.text.html.simpleparser;

——————————————————————————

Then in your method create memory stream as below

byte[] bytes;

MemoryStream AgendaDocumentMemoryStream = new MemoryStream();

var document = new Document(PageSize.A4, 50, 50, 25, 25);

var writer = PdfWriter.GetInstance(document, AgendaDocumentMemoryStream);

document.Open();

———————————————————————————————–

If you want to create a Table then here is some sample code

———————————————————————————

 

PdfPTable AddressTable;

Font boldTableFont;

PdfPTable AddressInWalseTable;

PdfPTable AddressInEnglishTable;

 

AddressTable = new PdfPTable(2);

AddressTable.HorizontalAlignment = 0;

AddressTable.SpacingBefore = 10;

AddressTable.SpacingAfter = 10;

AddressTable.WidthPercentage = (100.0f);

AddressTable.DefaultCell.Border = 0;

AddressTable.DefaultCell.Border = Rectangle.NO_BORDER;

AddressTable.SetWidths(new int[] { 80, 20 });

boldTableFont = FontFactory.GetFont(“Arial”, 12, Font.BOLD);

AddressInWalseTable = new PdfPTable(1);

AddressInWalseTable.HorizontalAlignment = 0;

AddressInWalseTable.SpacingBefore = 10;

AddressInWalseTable.SpacingAfter = 10;

AddressInWalseTable.WidthPercentage = (50.0f);

AddressInWalseTable.DefaultCell.Border = 0;

AddressInWalseTable.DefaultCell.Border = Rectangle.NO_BORDER;

AddressInWalseTable.SetWidths(new int[] { 30 });

AddressInEnglishTable = new PdfPTable(1);

AddressInEnglishTable.HorizontalAlignment = 2;

AddressInEnglishTable.SpacingBefore = 10;

AddressInEnglishTable.SpacingAfter = 10;

AddressInEnglishTable.WidthPercentage = (50.0f);

AddressInEnglishTable.DefaultCell.Border = 0;

AddressInEnglishTable.DefaultCell.Border = Rectangle.NO_BORDER;

AddressInEnglishTable.SetWidths(new int[] { 30 });

var parsedHtmlAddressInWales = HTMLWorker.ParseToList(new StringReader(AddressInWales), null);

foreach (var htmlAddressInWales in parsedHtmlAddressInWales)

for (int i = 0; i < htmlAddressInWales.Chunks.Count; i++)

if (htmlAddressInWales.Chunks[i].Content != “\n”)

AddressInWalseTable.AddCell(new Phrase(htmlAddressInWales.Chunks[i].Content, boldTableFont));

var parsedHtmlAddressInEnglish = HTMLWorker.ParseToList(new StringReader(AddressInEnglish), null);

foreach (var htmlAddressInEnglish in parsedHtmlAddressInEnglish)

for (int i = 0; i < htmlAddressInEnglish.Chunks.Count; i++)

if (htmlAddressInEnglish.Chunks[i].Content != “\n”)

AddressInEnglishTable.AddCell(new Phrase(htmlAddressInEnglish.Chunks[i].Content, boldTableFont));

AddressTable.AddCell(AddressInWalseTable);

AddressTable.AddCell(AddressInEnglishTable);

document.Add(AddressTable);

————————————————————

document.Close();

bytes = AgendaDocumentMemoryStream.ToArray();

AgendaDocumentMemoryStream.Close();

——————————————————————

Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *