Announcement

Create pdf file with logo along with table formatting

Pdf with Logo techiners
In the previous post Create Pdf file using C#, we have seen how to create a pdf file using C# and to display the data in the table format. This is like exporting HTML table to pdf format. Creating report in pdf format is not the motto but what if the report comes along with the company logo and the data is properly formatted. YES! That will look like a perfect report and official too. So, here is how we export company logo along with the properly formatted data in pdf format.

Steps For inserting the logo:


Step 1 : First we fetch the path of the Image in a string variable along with the file name.

Step 2 : Then using GetInstance() method of the Image class we get the image in jpg Image object.

Step 3 : Then we resize the image as per our requirement. Add some space before or after the image, if required.

Step 4 : Then we add the logo in the PdfPCell along with setting the proper alignment and border.

Step 5 : Then we add an empty space between the logo of the company and the table that will display the contents.

Step 6 : Then we display the content that we received as an argument of type DataTable. We iterate over each rows to display Name, Department, Designation, and Salary. Setting the font as Times New Roman, font size as 11 and style as BOLD.

Step 7 : We have created two paragraphs: one for adding Name, Department, Designation, and Salary and a content for declaration that will be aligned towards LEFT. Another paragraph for adding For Techiners and Authorised Signatory that will be aligned towards right.

Step 8 : Then we add the paragraphs to two cell in a single row setting the alignment of each cell.

Setp 9 : That's it. You are ready to execute the method with DataTable as an argument.




public void ExportToPdf(DataTable myDataTable)

{
Document pdfDoc = new Document(PageSize.A4, 10, 10, 20, 10);
try
{
PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
DataTable dt = myDataTable;
if (dt != null)
{
//Craete instance of the pdf table and set the number of column in that table 
PdfPTable table = new PdfPTable(4);
table.TotalWidth = 500f;
table.SpacingBefore = 10f;
table.LockedWidth = true;

string imageFilePath = Server.MapPath("~/Content/Images") + "/Techiners.png";

iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);

//Resize image depend upon your need 
jpg.ScaleToFit(80f, 60f);

//Give space before image 
jpg.SpacingBefore = 0f;

//Give some space after the image 
jpg.SpacingAfter = 10f;

PdfPCell logoHeader = new PdfPCell(jpg);
logoHeader.Colspan = 4;
logoHeader.HorizontalAlignment = Element.ALIGN_RIGHT;
logoHeader.BorderWidth = 0;
table.AddCell(logoHeader);

//Insert an empty space between table and logo.

Paragraph blankPara;
blankPara = new Paragraph(16, " ");
PdfPCell EmptyCell = new PdfPCell();
EmptyCell.Colspan = 4;
EmptyCell.BorderWidth = 0;
EmptyCell.AddElement(blankPara);
table.AddCell(EmptyCell);

foreach (DataRow row in dt.Rows)
{
table.TotalWidth = 500f;
table.LockedWidth = true;

var p = new Paragraph();
string Name = "\n" +"techiners" 
string Department = "\n" + "Blogosphere"
string Designation = "\n" + "ProBlogger"
string Salary = "\n" + "2500"

Font TimesNewRoman = FontFactory.GetFont("Times New Roman", 11, Font.BOLD);
Chunk cName = new Chunk(Name, TimesNewRoman);
Chunk cDepartment = new Chunk(Department, TimesNewRoman);
Chunk cDesignation = new Chunk(Designation, TimesNewRoman);
Chunk cSalary = new Chunk(Salary, TimesNewRoman);
cName.setLineHeight(13);
cDepartment.setLineHeight(13);
cDesignation.setLineHeight(13);
cSalary.setLineHeight(13);
Phrase NamePhrase = new Phrase(cName);
Phrase DepartmentPhrase = new Phrase(cDepartment);
Phrase DesignationPhrase = new Phrase(cDesignation);
Phrase SalaryPhrase = new Phrase(cSalary);


p.Add(NamePhrase);
p.Add(DepartmentPhrase);
p.Add(DesignationPhrase);
p.Add(SalaryPhrase);

/* For Techiners alignment */

var p2 = new Paragraph();

string ForTechiners = "\n\n\n\n\n"+"For Techiners";
Font TimesNewRoman = FontFactory.GetFont("Times New Roman", 11, Font.BOLD);
Chunk cForTechiners = new Chunk(ForB4U, TimesNewRoman);
Phrase ForTechinersPhrase = new Phrase(cForTechiners);

p2.Add(ForTechinersPhrase);

string dec = "\n"+"We Declare that this Report Shows the actual Information of the
goods described and that all particulars are true and correct.";

string AuthorisedSignatory = "\n" + "Authorised Signatory";
string decContent = dec.Insert(dec.IndexOf("We"), "\n").Insert(dec.IndexOf("goods") + 1, "\n");

Font bookAntiqua = FontFactory.GetFont("Times New Roman", 8f);
Chunk beginning = new Chunk(decContent, bookAntiqua);
beginning.setLineHeight(13);
Phrase p1 = new Phrase(beginning);
p.Add(p1);

Chunk authorSign = new Chunk(AuthorisedSignatory, bookAntiquaCodes);
authorSign.setLineHeight(13);
Phrase authorSignPhrase = new Phrase(authorSign);
p2.Add(authorSignPhrase);

PdfPCell cellText = new PdfPCell(new Paragraph(p));
cellText.Colspan = 2;
cellText.BorderWidthRight= 0f;
cellText.HorizontalAlignment = Element.ALIGN_LEFT;
cellText.NoWrap = false;
table.AddCell(cellText);

PdfPCell cellText2 = new PdfPCell(new Paragraph(p2));
cellText2.UseVariableBorders = true;
cellText2.BorderWidthLeft = 0f;
cellText2.Colspan = 2;
cellText2.HorizontalAlignment = Element.ALIGN_RIGHT;
cellText2.NoWrap = false;
table.AddCell(cellText2);

}
}
}
}


Here is the output:
pdf file with logo
pdf file with logo















CONCLUSION

In the previous post we have seen how to create pdf file using C#, this post take us one step ahead. With this post successfully implemented we have learnt to create properly formatted pdf report along with the logo of the company at the top. We have even seen text formatting with in a single cell, two cells and displaying it as a single cell.

If you have any doubt or any better idea to implement please do comment. Thanks and enjoy programming. :)

Very soon we will come up with the implementation of creating word file in C# in MVC4. So, stay tuned... God Bless!


No comments: