TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Priya Bm
NA
33
24.1k
How to change font style in pdf document using c#
May 6 2015 1:08 AM
Hi,
I have certificate that will download to pdf. but in pdf document font style is taking Arial.
Below is my code.
Please let me know where i am wrong?
protected void btnpdf_Click(object sender, EventArgs e)
{
Document doc = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
//Font Normalfont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK);
using (System.IO.MemoryStream memorystream = new System.IO.MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(doc, memorystream);
Phrase phrase = null;
PdfPCell cell = null;
PdfPTable table = null;
doc.Open();
table = new PdfPTable(1);
table.TotalWidth = 400f;
table.LockedWidth = true;
table.SetWidths(new float[] { 1f });
phrase = new Phrase();
phrase.Add(new Chunk("COMPLETION OF TRAINING\n\n", FontFactory.GetFont("Colonna MT", 16, Font.BOLD, BaseColor.BLUE)));
phrase.Add(new Chunk("This Certifies That\n\n", FontFactory.GetFont("Algerian", 14, Font.BOLD, BaseColor.BLACK)));
phrase.Add(new Chunk(lblname.Text + "\n\n", FontFactory.GetFont("Cambria", 14, Font.BOLD, BaseColor.RED)));
phrase.Add(new Chunk("Has successfully attended and completed\n\n", FontFactory.GetFont("Agency FB", 14, Font.BOLD, BaseColor.BLACK)));
phrase.Add(new Chunk("INDUCTION TRAINING", FontFactory.GetFont("Gabriola", 14, Font.BOLD, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
table.AddCell(cell);
doc.Add(table);
PdfContentByte content = writer.DirectContent;
Rectangle rectangle = new Rectangle(doc.PageSize);
rectangle.Left += doc.LeftMargin;
rectangle.Right -= doc.RightMargin;
rectangle.Top -= doc.TopMargin;
rectangle.Bottom += doc.BottomMargin;
content.SetColorStroke(BaseColor.YELLOW);
content.Rectangle(rectangle.Left, rectangle.Bottom, rectangle.Width, rectangle.Height);
content.Stroke();
doc.Close();
byte[] bytes = memorystream.ToArray();
memorystream.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-Disposition", "attachment; filename=InductionCertificate.pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
}
}
private static PdfPCell PhraseCell(Phrase phrase, int align)
{
PdfPCell cell = new PdfPCell(phrase);
cell.BorderColor = BaseColor.WHITE;
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
return cell;
}
}
Reply
Answers (
4
)
How to retrieve the image from database in mvc 4
Image soruce is from webresource.axd?