public void PDFGeneration()
{
var output = new MemoryStream();
var document = new Document();
var writer = PdfWriter.GetInstance(document, output);
writer.CloseStream = false;
document.Open();
var titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD);
var subTitleFont = FontFactory.GetFont("Arial", 14, Font.BOLD);
var boldTableFont = FontFactory.GetFont("Arial", 12, Font.BOLD);
var endingMessageFont = FontFactory.GetFont("Arial", 10, Font.ITALIC);
var bodyFont = FontFactory.GetFont("Arial", 12, Font.NORMAL);
document.Add(new Paragraph("Hungry Bytes User Details", titleFont));
var orderInfoTable = new PdfPTable(2);
orderInfoTable.HorizontalAlignment = 0;
orderInfoTable.SpacingBefore = 10;
orderInfoTable.SpacingAfter = 10;
orderInfoTable.DefaultCell.Border = 0;
orderInfoTable.SetWidths(new int[] { 1, 4 });
orderInfoTable.AddCell(new Phrase("Name:", boldTableFont));
orderInfoTable.AddCell(txtName.Text);
orderInfoTable.AddCell(new Phrase("Surname:", boldTableFont));
orderInfoTable.AddCell(txtSurname.Text);
orderInfoTable.AddCell(new Phrase("Gender:", boldTableFont));
orderInfoTable.AddCell(GenderDropDownList.SelectedValue);
orderInfoTable.AddCell(new Phrase("Address 1:", boldTableFont));
orderInfoTable.AddCell(txtAddress1.Text);
orderInfoTable.AddCell(new Phrase("Address 2:", boldTableFont));
orderInfoTable.AddCell(txtAddress2.Text);
orderInfoTable.AddCell(new Phrase("Country:", boldTableFont));
orderInfoTable.AddCell(CountryDropDownList.SelectedItem.ToString());
orderInfoTable.AddCell(new Phrase("Town:", boldTableFont));
orderInfoTable.AddCell(TownDropDownList.SelectedItem.ToString());
orderInfoTable.AddCell(new Phrase("Email Address:", boldTableFont));
orderInfoTable.AddCell(txtEmail.Text);
orderInfoTable.AddCell(new Phrase("Date of Birth:", boldTableFont));
orderInfoTable.AddCell(txtDateOfBirth.Text);
orderInfoTable.AddCell(new Phrase("Blood Group:", boldTableFont));
orderInfoTable.AddCell(BloodGroupDropDownList.SelectedItem.ToString());
orderInfoTable.AddCell(new Phrase("Contact Number:", boldTableFont));
orderInfoTable.AddCell(txtContactNumber.Text);
orderInfoTable.AddCell(new Phrase("Username:", boldTableFont));
orderInfoTable.AddCell(txtUsername.Text);
orderInfoTable.AddCell(new Phrase("Password:", boldTableFont));
orderInfoTable.AddCell(txtPassword.Text);
orderInfoTable.AddCell(new Phrase("Confirm Password:", boldTableFont));
orderInfoTable.AddCell(txtConfirmPass.Text);
GeneratePDFwithImage();
document.Add(orderInfoTable);
document.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename=Receipt-{0}.pdf", txtUsername.Text));
Response.BinaryWrite(output.ToArray());
_sendMail(output);
}
private void _sendMail(Stream output)
{
string FromMail = "hungrybytes061@gmail.com";
string ToMail = txtEmail.Text;
string Subject = "User Details PDF";
string Body = "Your Registration Details";
using (Attachment att = new Attachment(output, "UserDetails.pdf", MediaTypeNames.Application.Pdf))
{
using (MailMessage mm = new MailMessage(
FromMail, ToMail, Subject, Body))
{
mm.Attachments.Add(att);
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new NetworkCredential("hungrybytes061", "hungrybytes061");
smtp.EnableSsl = true;
smtp.Send(mm);
}
}
}
protected void GeneratePDFwithImage()
{
string path = Server.MapPath("~/images/");
string fileName = "pdfDocument" + DateTime.Now.Ticks + ".pdf";
Document doc = new Document();
try
{
PdfWriter.GetInstance(doc, new FileStream(path + fileName, FileMode.Create));
doc.Open();
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(Server.MapPath("~/images/banner1.jpg"));
doc.Add(jpg);
}
catch (Exception ex)
{
lblMessage.Text = ex.ToString();
}
finally
{
doc.Close();
}
}
Hi, I have tried different methods in order to add an image/logo to the pdf using ITextSharp and have failed doing so. In one of the methods I stated the coordinates where the image should be but I don't really know how coordinates work exactly and the image would not be positioned in the desired position. The other method I used didn't return any image at all.
Also when a new user registers to my website, an email is sent with user details via pdf but the generated pdf is empty when attached to the email.
I would really appreciate some help in this pls.
Thanks in advance.