I am using the following code to generate a PDF using iTextSharp from a GridView however the generated PDF is not visible to me. How can I view it in my html page?
GridView1.Visible = false;
SqlConnection sql = Connection.con();
sql.Open();
SqlCommand cmd = new SqlCommand("spGetSalesbyCustomer", sql);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#CustomerId", Convert.ToInt32(TextBox1.Text));
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dd = new DataTable();
adp.Fill(dd);
GridView2.DataSource = dd;
GridView2.DataBind();
int cellCount = GridView2.Columns.Count;
sql.Close();
if (cellCount > 0)
{
GridView2.AllowPaging = false;
GridView2.DataBind();
BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + #"\fonts\ARIALUNI.TTF", BaseFont.IDENTITY_H, true);
iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(cellCount);
int[] widths = new int[cellCount];
for (int x = 0; x < cellCount; x++)
{
widths[x] = (int)GridView2.Columns[x].ItemStyle.Width.Value;
string cellText = Server.HtmlDecode(GridView2.HeaderRow.Cells[x].Text);
//Set Font and Font Color
iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
//font.Color = new Color(GridView2.HeaderStyle.ForeColor);
iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font));
//Set Header Row BackGround Color
//cell.BackgroundColor = new Color(GridView2.HeaderStyle.BackColor);
table.AddCell(cell);
}
table.SetWidths(widths);
for (int i = 0; i < GridView2.Rows.Count; i++)
{
if (GridView2.Rows[i].RowType == DataControlRowType.DataRow)
{
for (int j = 0; j < GridView2.Columns.Count; j++)
{
string cellText = Server.HtmlDecode(GridView2.Rows[i].Cells[j].Text);
//Set Font and Font Color
iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
//font.Color = new Color(GridView2.RowStyle.ForeColor);
iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font));
//Set Color of row
if (i % 2 == 0)
{
//Set Row BackGround Color
//cell.BackgroundColor = new Color(GridView2.RowStyle.BackColor);
}
table.AddCell(cell);
}
}
}
//Create the PDF Document
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
int pages = pdfDoc.;
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
Your question is a little unclear but if your code is correct (and I know it isn't 100% based on the seventh last line) then you're not actually adding your PdfPTable to the Document:
//Create the PDF Document
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
//Bind a writer to our document abstraction and our output stream
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
//Open the document for writing
pdfDoc.Open();
//This next line is a syntax error
//int pages = pdfDoc.;
//Add the table to the PDF
pdfDoc.Add(table);
//Close the document
pdfDoc.Close();
//ASP.Net/HTTP stuff
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Do not use this next line, it doesn't do what you think it does
//Response.Write(pdfDoc);
Response.End();
Related
I Created ExportToPDF Button to export Gridview to PDF file, then how can I add an image background as watermark to the PDF file?
protected void ExportToPDF(object sender, EventArgs e)
{
using (StringWriter sw = new StringWriter())
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
gvOrders.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}
One solution would be;
private static Image watermark_600
{
get
{
if (_watermark_600 == null)
_watermark_600 = Image.FromFile(HttpContext.Current.Server.MapPath(#"~/content/Images/watermark600.png"));
return _watermark_600;
}
}
then,
public static byte[] AddWaterMark(byte[] fileContent)
{
if (fileContent == null)
return null;
//create a image object containing the photograph to watermark
using (var img = new ImageFactory())
using (var imgWatermark = new ImageLayer())
{
img.Load(fileContent);
if (img.Image.Width < 750 && img.Image.Height < 750)
imgWatermark.Image = (Image)watermark_200.Clone();
else if (img.Image.Width < 1500 && img.Image.Height < 1500)
imgWatermark.Image = (Image)watermark_400.Clone();
else if (img.Image.Width < 2500 && img.Image.Height < 2500)
imgWatermark.Image = (Image)watermark_600.Clone();
else
imgWatermark.Image = (Image)watermark_800.Clone();
int x = Math.Abs((img.Image.Width - imgWatermark.Image.Width)) / 2;
int y = Math.Abs((img.Image.Height - imgWatermark.Image.Height)) / 2;
imgWatermark.Position = new Point(x, y);
imgWatermark.Opacity = 90;
img.Overlay(imgWatermark);
using (MemoryStream msbyte = new MemoryStream())
{
img.Save(msbyte);
return msbyte.ToArray();
}
}
}
I have 11 grid view in my page I want to export it into single pdf file, I searched this query and implemented the code which I got but it didn't work and generate pdf with black background only headers is showing not the content. Please provide me the code to export all 10 grid views into single pd
protected void btnExportToPDF_Click(object sender, EventArgs e)
{
GridView[] gvpdf = new GridView[] { GV1,GV2,GV3,GV4,GV5,GV6,GV7,GV8,GV9,GV10,GV11};
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
MemoryStream ms = new MemoryStream();
PdfWriter.GetInstance(pdfDoc, ms);
pdfDoc.Open();
for (int i = 0; i < gvpdf.Length; i++)
{
if (gvpdf[i].Visible)
{
PdfPTable pdfTbl = new PdfPTable(gvpdf[i].HeaderRow.Cells.Count);
pdfTbl.SpacingAfter = 20f;
foreach (TableCell headerTblCell in gvpdf[i].HeaderRow.Cells)
{
System.Drawing.Font font = new System.Drawing.Font("Arial", 24, FontStyle.Bold);
font.Color = new BaseColor(gvpdf[i].HeaderStyle.ForeColor);
PdfPCell pdfCell = new PdfPCell(new Phrase(headerTblCell.Text));
pdfCell.BackgroundColor = new BaseColor(gvpdf[i].HeaderStyle.ForeColor);
pdfTbl.AddCell(pdfCell);
}
foreach (GridViewRow gvRow in gvpdf[i].Rows)
{
foreach (TableCell tblCell in gvRow.Cells)
{
System.Drawing.Font font = new System.Drawing.Font("Arial", 24, FontStyle.Bold);
font.Color = new BaseColor(gvpdf[i].RowStyle.ForeColor);
PdfPCell pdfCell = new PdfPCell(new Phrase(tblCell.Text));
pdfCell.BackgroundColor = new BaseColor(gvpdf[i].RowStyle.ForeColor);
pdfTbl.AddCell(pdfCell);
}
}
pdfDoc.Add(pdfTbl);
}
}
pdfDoc.Close();
byte[] content = ms.ToArray();
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "attachment;filename=report_" + ".pdf");
Response.BinaryWrite(content);
Response.Flush();
Response.End();
}
You may want to try another approach. Just read all your data from mentioned grids, store it in POCO classes and then use some library to convert this information into a PDF file with desired formatting.
Personally, I recommend a free and open-source library called QuestPDF (notice, I am its creator). It offers an easy to use fluent API, detailed documentation and a getting started tutorial showing how to generate an invoice document.
This is my first question on stackoverflow.
I hope to be welcome.
My question.
I trying to use iTextSharp for create PDF file with header, footer, number of pages and logo.
My code below and my problem is error on this line of my code behind:
pdfDoc.Close();
If I disable this line the PDF file is created but damaged it cannot be opened.
The error is :
Object reference not set to an instance of an object
I really hope in your help.
Create a PDF file:
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
pdffile.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
string imagepath = Server.MapPath("..") + "\\Logo.jpg";
Document pdfDoc = new Document(PageSize.A4, 20f, 20f, 10f, 20f);
using (MemoryStream memoryStream = new MemoryStream())
{
try
{
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
writer.PageEvent = new Footer();
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
iTextSharp.text.Font times = new iTextSharp.text.Font(bfTimes, 12, iTextSharp.text.Font.ITALIC, iTextSharp.text.BaseColor.MAGENTA);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagepath);
pdfDoc.Add(image);
var cssResolver = new StyleAttrCSSResolver();
var cssFile = XMLWorkerHelper.GetCSS(new FileStream(HttpContext.Current.Server.MapPath("style.css"), FileMode.Open));
cssResolver.AddCss(cssFile);
// HTML
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
// Pipelines
PdfWriterPipeline pdfFile = new PdfWriterPipeline(pdfDoc, writer);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdfFile);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
// XML Worker
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);
p.Parse(sr);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
}
catch (Exception ex)
{
throw (ex);
}
}
}
}
Add header/footer to PDF
public partial class Footer : PdfPageEventHelper
{
PdfContentByte cb;
PdfTemplate headerTemplate, footerTemplate;
BaseFont bf = null;
DateTime PrintTime = DateTime.Now;
iTextSharp.text.Image image;
private string _header;
public string Header
{
get { return _header; }
set { _header = value; }
}
public override void OnOpenDocument(PdfWriter writer, Document document)
{
try
{
PrintTime = DateTime.Now;
bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb = writer.DirectContent;
headerTemplate = cb.CreateTemplate(100, 100);
footerTemplate = cb.CreateTemplate(50, 50);
}
catch (DocumentException de)
{
//handle exception here
}
catch (System.IO.IOException ioe)
{
//handle exception here
}
}
public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer,
iTextSharp.text.Document document)
{
base.OnEndPage(writer, document);
iTextSharp.text.Font baseFontNormal =
new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12f,
iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK);
iTextSharp.text.Font baseFontBig =
new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12f,
iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK);
Phrase p1Header = new Phrase("testing", baseFontNormal);
PdfPTable pdfTab = new PdfPTable(3);
PdfPCell pdfCell1 = new PdfPCell();
PdfPCell pdfCell2 = new PdfPCell(p1Header);
PdfPCell pdfCell3 = new PdfPCell();
String text = "Page " + writer.PageNumber + " of ";
{
cb.BeginText();
cb.SetFontAndSize(bf, 12);
cb.SetTextMatrix(document.PageSize.GetRight(200), document.PageSize.GetTop(45));
cb.ShowText(text);
cb.EndText();
float len = bf.GetWidthPoint(text, 12);
cb.AddTemplate(headerTemplate, document.PageSize.GetRight(200) + len, document.PageSize.GetTop(45));
}
{
cb.BeginText();
cb.SetFontAndSize(bf, 12);
cb.SetTextMatrix(document.PageSize.GetRight(180), document.PageSize.GetBottom(30));
cb.ShowText(text);
cb.EndText();
float len = bf.GetWidthPoint(text, 12);
cb.AddTemplate(footerTemplate, document.PageSize.GetRight(180) + len, document.PageSize.GetBottom(30));
Paragraph footer =
new Paragraph("©All Rights Reserved",
FontFactory.GetFont(FontFactory.HELVETICA, 8, iTextSharp.text.Font.ITALIC));
footer.Alignment = Element.ALIGN_RIGHT;
PdfPTable footerTbl = new PdfPTable(1);
footerTbl.TotalWidth = 800;
footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell cell = new PdfPCell(footer);
cell.Border = 0;
cell.PaddingLeft = 10;
footerTbl.AddCell(cell);
footerTbl.WriteSelectedRows(0, -1, 0, 30, writer.DirectContent);
}
PdfPCell pdfCell4 = new PdfPCell(new Phrase("test", baseFontNormal));
PdfPCell pdfCell5 = new PdfPCell(new Phrase("Date:" + PrintTime.ToShortDateString(), baseFontBig));
PdfPCell pdfCell6 = new PdfPCell();
PdfPCell pdfCell7 = new PdfPCell(new Phrase("Hour:" + string.Format("{0:t}", DateTime.Now), baseFontBig));
pdfCell1.HorizontalAlignment = Element.ALIGN_CENTER;
pdfCell2.HorizontalAlignment = Element.ALIGN_CENTER;
pdfCell3.HorizontalAlignment = Element.ALIGN_CENTER;
pdfCell4.HorizontalAlignment = Element.ALIGN_CENTER;
pdfCell5.HorizontalAlignment = Element.ALIGN_CENTER;
pdfCell6.HorizontalAlignment = Element.ALIGN_CENTER;
pdfCell7.HorizontalAlignment = Element.ALIGN_CENTER;
pdfCell2.VerticalAlignment = Element.ALIGN_BOTTOM;
pdfCell3.VerticalAlignment = Element.ALIGN_MIDDLE;
pdfCell4.VerticalAlignment = Element.ALIGN_TOP;
pdfCell5.VerticalAlignment = Element.ALIGN_MIDDLE;
pdfCell6.VerticalAlignment = Element.ALIGN_MIDDLE;
pdfCell7.VerticalAlignment = Element.ALIGN_MIDDLE;
pdfCell4.Colspan = 3;
pdfCell1.Border = 0;
pdfCell2.Border = 0;
pdfCell3.Border = 0;
pdfCell4.Border = 0;
pdfCell5.Border = 0;
pdfCell6.Border = 0;
pdfCell7.Border = 0;
pdfTab.AddCell(pdfCell1);
pdfTab.AddCell(pdfCell2);
pdfTab.AddCell(pdfCell3);
pdfTab.AddCell(pdfCell4);
pdfTab.AddCell(pdfCell5);
pdfTab.AddCell(pdfCell6);
pdfTab.AddCell(pdfCell7);
pdfTab.TotalWidth = document.PageSize.Width - 80f;
pdfTab.WidthPercentage = 70;
pdfTab.WriteSelectedRows(0, -1, 40, document.PageSize.Height - 30, writer.DirectContent);
cb.MoveTo(40, document.PageSize.Height - 100);
cb.LineTo(document.PageSize.Width - 40, document.PageSize.Height - 100);
cb.Stroke();
cb.MoveTo(40, document.PageSize.GetBottom(50));
cb.LineTo(document.PageSize.Width - 40, document.PageSize.GetBottom(50));
cb.Stroke();
}
public override void OnCloseDocument(PdfWriter writer, Document document)
{
base.OnCloseDocument(writer, document);
headerTemplate.BeginText();
headerTemplate.SetFontAndSize(bf, 12);
headerTemplate.SetTextMatrix(0, 0);
headerTemplate.ShowText((writer.PageNumber - 1).ToString());
headerTemplate.EndText();
footerTemplate.BeginText();
footerTemplate.SetFontAndSize(bf, 12);
footerTemplate.SetTextMatrix(0, 0);
footerTemplate.ShowText((writer.PageNumber - 1).ToString());
footerTemplate.EndText();
}
}
You are welcome "Uncle Vince" !
See this
And try this solution.
I hope I was helpful.
MemoryStream memoryStream = new MemoryStream();
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pdffile.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
string imagepath = Server.MapPath("..") + "\\Logo.jpg";
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
writer.PageEvent = new Footer();
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
iTextSharp.text.Font times = new iTextSharp.text.Font(bfTimes, 12, iTextSharp.text.Font.ITALIC, iTextSharp.text.BaseColor.MAGENTA);
pdfDoc.Open();
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagepath);
pdfDoc.Add(image);
var cssResolver = new StyleAttrCSSResolver();
var cssFile = XMLWorkerHelper.GetCSS(new FileStream(HttpContext.Current.Server.MapPath("style.css"), FileMode.Open));
cssResolver.AddCss(cssFile);
//HTML
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
//Pipelines
PdfWriterPipeline pdfFile = new PdfWriterPipeline(pdfDoc, writer);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdfFile);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
//XML Worker
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);
p.Parse(sr);
writer.CloseStream = false;
pdfDoc.Close();
memoryStream.Close();
I am trying to export multiple gridviews into single pdf using iTextSharp. I am looping through the gridviews and then looping through the rows of the gridview. The looping is going ok. But after pdf download, only the last gridview can be seen. It seems the gridviews are overwriting each other and only last one remains. Here is my code. What am I doing wrong?
protected void btnExportToPDF_Click(object sender, EventArgs e)
{
GridView[] gvExcel = new GridView[] { gridvw1,gridvw2,gridvw3 };
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
for (int i = 0; i < gvExcel.Length; i++)
{
if (gvExcel[i].Visible)
{
PdfPTable pdfTbl = new PdfPTable(gvExcel[i].HeaderRow.Cells.Count);
foreach (TableCell headerTblCell in gvExcel[i].HeaderRow.Cells)
{
Font font = new Font();
font.Color = new BaseColor(gvExcel[i].HeaderStyle.ForeColor);
PdfPCell pdfCell = new PdfPCell(new Phrase(headerTblCell.Text));
pdfCell.BackgroundColor = new BaseColor(gvExcel[i].HeaderStyle.ForeColor);
pdfTbl.AddCell(pdfCell);
}
foreach (GridViewRow gvRow in gvExcel[i].Rows)
{
foreach (TableCell tblCell in gvRow.Cells)
{
Font font = new Font();
font.Color = new BaseColor(gvExcel[i].RowStyle.ForeColor);
PdfPCell pdfCell = new PdfPCell(new Phrase(tblCell.Text));
pdfCell.BackgroundColor = new BaseColor(gvExcel[i].RowStyle.ForeColor);
pdfTbl.AddCell(pdfCell);
}
}
//Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
pdfDoc.Add(pdfTbl);
}
}
pdfDoc.Close();
//Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "attachment;filename=report_" + startDate + "-" + endDate + ".pdf");
Response.Write(pdfDoc);
Response.Flush();
Response.End();
}
One of your errors is not an iText error; it's is a simple logical error that can be solved with common sense. The other error is weird. You aren't using the Response correctly.
protected void btnExportToPDF_Click(object sender, EventArgs e)
{
GridView[] gvExcel = new GridView[] { gridvw1,gridvw2,gridvw3 };
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
MemoryStream ms = new MemoryStream();
PdfWriter.GetInstance(pdfDoc, ms);
pdfDoc.Open();
for (int i = 0; i < gvExcel.Length; i++)
{
if (gvExcel[i].Visible)
{
PdfPTable pdfTbl = new PdfPTable(gvExcel[i].HeaderRow.Cells.Count);
pdfTbl.SpacingAfter = 20f;
foreach (TableCell headerTblCell in gvExcel[i].HeaderRow.Cells)
{
Font font = new Font();
font.Color = new BaseColor(gvExcel[i].HeaderStyle.ForeColor);
PdfPCell pdfCell = new PdfPCell(new Phrase(headerTblCell.Text));
pdfCell.BackgroundColor = new BaseColor(gvExcel[i].HeaderStyle.ForeColor);
pdfTbl.AddCell(pdfCell);
}
foreach (GridViewRow gvRow in gvExcel[i].Rows)
{
foreach (TableCell tblCell in gvRow.Cells)
{
Font font = new Font();
font.Color = new BaseColor(gvExcel[i].RowStyle.ForeColor);
PdfPCell pdfCell = new PdfPCell(new Phrase(tblCell.Text));
pdfCell.BackgroundColor = new BaseColor(gvExcel[i].RowStyle.ForeColor);
pdfTbl.AddCell(pdfCell);
}
}
pdfDoc.Add(pdfTbl);
}
}
pdfDoc.Close();
byte[] content = ms.ToArray();
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "attachment;filename=report_" + startDate + "-" + endDate + ".pdf");
Response.BinaryWrite(content);
Response.Flush();
Response.End();
}
There might be some other issues, but I hope you understand the logical error:
You created a new PDF file with PdfWriter every time you entered the loop. You should only create one PdfWriter instance if you only want to create one PDF.
It's better to create the PDF in a MemoryStream, and then write the content of that stream to the Response object as a binary stream. I really didn't understand what you were doing there (and why you tried to do it that way).
I also introduced a SpacingAfter of 20 user units, otherwise it will look as if all tables all glued together into one big table.
There may still be some errors in the way you send the file to the Response, but this should already get you on your way. (Why aren't you sending the file size to the browser? You know that size, don't you? It's the number of bytes in the content object.)
I'm working on a asp.Net project where it requires to save in as pdf. I need to create a Label and a TextBox dynamically based on the data present in the database.
My problem is that when I try to save it in pdf,only Labels can be saved while the TextBox goes missing.
Below is my code for creating Label & TextBox dynamically.
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox txtSkills = new TextBox();
TextBox txtProficiency = new TextBox();
Label lblSkills = new Label();
Label lblProficiency = new Label();
txtSkills.ID = "txtSkills" + i.ToString();
txtSkills.Text = dt.Rows[i].ItemArray[1].ToString();
txtProficiency.ID = "txtProficiency" + i.ToString();
txtProficiency.Text = dt.Rows[i].ItemArray[2].ToString();
lblSkills.ID = "lblSkills" + i.ToString();
lblSkills.Text = "Skills:";
lblProficiency.ID = "lblProficiency" + i.ToString();
lblProficiency.Text = "Proficiency:";
Panel3.Controls.Add(lblSkills);
Panel3.Controls.Add(txtSkills);
Panel3.Controls.Add(lblProficiency);
Panel3.Controls.Add(txtProficiency);
Code for saving in pdf :-
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" somename ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Panel3.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
Paragraph p1 = new Paragraph();
p1.SpacingAfter = 15f;
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
pdfDoc.Add(new Paragraph("Education"));
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();