Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Create PDF File in ASP.NET
WhatsApp
Kumar Bhimsen
Dec 25
2015
1.4
k
0
0
itextsharpdll.zip
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.IO;
using
iTextSharp.text;
using
iTextSharp.text.pdf;
public
partial
class
_Default: Page {
protected
void
Page_Load(
object
sender, EventArgs e) {}
protected
void
GenerateReport_Click(
object
sender, EventArgs e) {
var doc =
new
Document(PageSize.A4);
// defining the docment
var output =
new
MemoryStream();
var wri = PdfWriter.GetInstance(doc, output);
var fpara =
new
Phrase(
"\n"
);
fpara.Add(
" * * * * * * You are Welcome * * * * * * "
);
fpara.Add(
" \n"
);
fpara.Add(
" \n"
);
string
url =
"~/Image/kbs.jpg"
;
var headerImage = iTextSharp.text.Image.GetInstance(
new
Uri(Server.MapPath(url)));
/// Adding border to image if required
headerImage.Border = iTextSharp.text.Rectangle.BOX;
headerImage.BorderColor = iTextSharp.text.Color.BLACK;
headerImage.BorderWidth = 3 f;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
doc.Open();
Rectangle defaultPageSize = PageSize.A4;
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252,
false
);
Font fontH1 =
new
Font(bfTimes, 9, Font.NORMAL, Color.BLUE);
PdfPTable table =
new
PdfPTable(1);
// You can change the number of columns as per required in our case i think we need only one column
List <
string
> paras =
new
List <
string
> ();
foreach
(
string
p1
in
paras) {
PdfPCell cell =
new
PdfPCell(
new
Phrase(p1, fontH1));
//cell.FixedHeight = 15f;
table.AddCell(cell);
}
doc.Add(headerImage);
/// added header image
fpara.Add(
"visit us : kumarbhimsenbgp.blogspot.in"
);
fpara.Add(
"\n"
);
doc.Add(fpara);
/// added 1
doc.Add(table);
/// added table
doc.NewPage();
/// aad a new Page when doc item exceed.
doc.Close();
//Close document
Response.ContentType =
"application/pdf"
;
Response.AddHeader(
"content-disposition"
,
"attachment;filename=KbsTest.pdf"
);
Response.BinaryWrite(output.ToArray());
}
}
create pdf in c#
Up Next
Create PDF File in ASP.NET