Textbox doesn't appear while saving in pdf - c#

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();

Related

Converting a modal dialog window to pdf

I'm trying to convert a modal dialog with 3 DataTables to pdf using iTextSharp:
DataTable dtTable = (DataTable)ViewState["LoanPlan"];
DataTable dtTax = (DataTable)ViewState["PlanTaxData"];
DataTable dtPlan = (DataTable)ViewState["PlanAllData"];
I'm using this code to try and accomplish it:
protected void buttonPDF_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
But it seems that it's actually trying to convert the whole .aspx page to .pdf format. How should I go about targeting just the modal dialogue or just the 3 DataTables?

itextsharp pdf not showing

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();

Export div content to pdf format

I used the following code to export div content to pdf format using itextsharp
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Panel1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
// Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
Document pdfDoc = new Document(new Rectangle(1000f, 1000f));
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
it shows the following error
Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\Images\logo.png'.`
if i hide the logo in desin then it exported to pdf but the alignment are missing. how to correct it.The page i need to export is follows
In order to export the content to any pdf file, you need to follow the basic conventions of pdf file, those are given in the appendix G of the following pdf reference,
http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_reference_1-7.pdf
Firstly install class in Nuget console: Install-Package itextsharp.xmlworker
Then add namespace: using iTextSharp.tool.xml;
Response.ContentType = "application/pdf";
//string pdf;
// pdf = Convert.ToInt32(hidTablEmpCode.ToString()).ToString() + ".Pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
//Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
// var example_html = #"<p>This is <span class="" headline="" style="">some</span> sample text<span style="">!!!</span></p>";
StringReader sr = new StringReader(lblMessageDetail.Text);
Document pdfDoc = new Document(PageSize.A4, 50f, 50f, 50f, 50f);
var writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
for (int i = 0; i < 5; i++)
{
string imageFilePath2 = Server.MapPath(".") + "/images/GraphicNew.jpg";
iTextSharp.text.Image jpg3 = iTextSharp.text.Image.GetInstance(imageFilePath2);
jpg3.Alignment = iTextSharp.text.Image.UNDERLYING;
jpg3.ScaleToFit(3530, 835);
jpg3.SetAbsolutePosition(0, 0);
pdfDoc.Add(jpg3);
}
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
string imageFilePath = Server.MapPath(".") + "/Bimages/ns5.png";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
GetEmp();
string imageURLS = Server.MapPath("~/Emp/Document/") + hidTablEmpCode.Value + "/" + hidSign.Value.ToString();
iTextSharp.text.Image jpg2 = iTextSharp.text.Image.GetInstance(imageURLS);
jpg2.ScaleToFit(100, 50);
jpg2.SetAbsolutePosition(370, 530);
jpg2.Alignment = Element.ALIGN_LEFT;
pdfDoc.Add(jpg2);
jpg.Alignment = iTextSharp.text.Image.ALIGN_LEFT;
jpg.ScaleToFit(100, 50);
jpg.SetAbsolutePosition(50, 735);
pdfDoc.Add(jpg);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

Itextsharp pdf GridView Positioning

I am working with asp.net application and in which i am using itexsharp to generate pdf file. I need to export my gridview to pdf and the problem is after generating pdf the gridview is always postioning at the top of the page. My requriement is i need the gridview at the centre of the page and at the top i will have some text entered throught the textbox from the front end..
Please help me to find solution for this and Thanks in Advance
here is the code for my button click:
Here is my code for the button click to generate PDF:
{
string name = TextBox1.Text;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Chunk c = new Chunk("Export GridView to PDF Using iTextSharp \n",FontFactory.GetFont("Verdana", 15));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
p.Add(c);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
string s = sw.ToString();
GridView1.AllowPaging = false;
BindDDL();
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
var wri = PdfWriter.GetInstance(pdfDoc, new FileStream("D:/xyz" + ".pdf", FileMode.Create));
pdfDoc.Open();
htmlparser.Parse(sr);
string contents = File.ReadAllText(Server.MapPath("~/bill.htm"));
contents = contents.Replace("[CNAME]", name);
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);
foreach (var htmlElement in parsedHtmlElements)
pdfDoc.Add(htmlElement as IElement);
pdfDoc.Add(p);
System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(new StringReader(s));
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
set gridview property as follows to position it in center
<asp:GridView ID="GridView1" RowStyle HorizontalAlign="Center" ...>
m not gettin wat you exactly need...
but place your txtbox and gridview inside the table

'The document has no page' error thrown when closing a document

I'm generating, saving and writing PDF on output stream.
I'm getting a The document has no page error while writing on output stream.
What is the problem?
string contents;
string fileName = "aaa.pdf";
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
hw.AddStyleAttribute("font-size","11px");
abs.RenderControl(hw);
string path = Server.MapPath("../Images/a.png");
contents = sw.ToString();
contents = contents.Replace("../Images/a.png", path);
sw.Close();
hw.Close();
StringReader sr = new StringReader(contents);
System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/pdf/") + fileName, FileMode.Create);
Document pdfDoc = new Document(PageSize.A4, 30,5,35,5);
Document cpdfDoc = new Document(PageSize.A4, 30, 5, 35, 5);
pdfDoc.PageCount = 2;
cpdfDoc.PageCount = 2;
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc,fs);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
fs.Close();
HTMLWorker htmlparser2 = new HTMLWorker(cpdfDoc);
PdfWriter.GetInstance(cpdfDoc, Response.OutputStream);
cpdfDoc.Open();
htmlparser2.Parse(sr);
cpdfDoc.Close();
Response.Write(cpdfDoc);
Response.Flush();
Response.End();
No problem in saving. I'm getting error on this line cpdfDoc.Close();.
use this code and set your pdf location. and image path.
its working.
string contents = "hi";
string fileName = "aaa.pdf";
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
hw.AddStyleAttribute("font-size", "11px");
string path = Server.MapPath("~/Images/a.png");
contents = contents.Replace("~/Images/a.png", path);
sw.Close();
hw.Close();
StringReader sr = new StringReader(contents);
System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/") + DateTime.Now.Ticks, FileMode.Create);
Document pdfDoc = new Document(PageSize.A4, 30, 5, 35, 5);
Document cpdfDoc = new Document(PageSize.A4, 30, 5, 35, 5);
pdfDoc.PageCount = 2;
cpdfDoc.PageCount = 2;
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, fs);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
fs.Close();
HTMLWorker htmlparser2 = new HTMLWorker(cpdfDoc);
PdfWriter.GetInstance(cpdfDoc, Response.OutputStream);
cpdfDoc.Open();
htmlparser2.Parse(sr);
don't use this line
contents = sw.ToString();

Categories

Resources