Last post Mar 27, 2012 04:51 AM by paritoshmmec
Member
153 Points
625 Posts
Mar 18, 2012 02:25 AM|sparrow37|LINK
I am trying to generate pdf of my gridview using following code:
HtmlForm form = new HtmlForm(); form.Controls.Add(PGGridViewDetail); StringWriter sw = new StringWriter(); HtmlTextWriter hTextWriter = new HtmlTextWriter(sw); form.Controls[0].RenderControl(hTextWriter); string htmlContent = sw.ToString();
htmlContent = Regex.Replace(htmlContent, "</?(a|A).*?>", ""); htmlContent = Regex.Replace(htmlContent, "px", "");
Document document = new Document();
// step 2: // we create a writer that listens to the document // and directs a PDF-stream to a file
string Path = Server.MapPath("~/Jaram PDF/PDFS/") + "Sample.pdf";
PdfWriter.GetInstance(document, new FileStream(Path, FileMode.Create));
// step 3: we open the document document.Open();
// step 4: we add a paragraph to the document document.Add(new Paragraph(htmlContent.ToString()));
System.Xml.XmlTextReader _xmlr = new System.Xml.XmlTextReader(new StringReader(htmlContent));
_xmlr.WhitespaceHandling = WhitespaceHandling.None;
ITextHandler xmlHandler = new ITextHandler(document); xmlHandler.Parse(_xmlr);
//HtmlParser.Parse(document, _xmlr);
// step 5: we close the document document.Close();
but is is showing the html markup of the grid instead of grid in newly generated pdf.
if I comment step 4
then I get document has no pages.
please suggest me solution
213 Points
859 Posts
Mar 25, 2012 09:54 AM|billy_111|LINK
sparrow37 I am trying to generate pdf of my gridview using following code: HtmlForm form = new HtmlForm(); form.Controls.Add(PGGridViewDetail); StringWriter sw = new StringWriter(); HtmlTextWriter hTextWriter = new HtmlTextWriter(sw); form.Controls[0].RenderControl(hTextWriter); string htmlContent = sw.ToString(); htmlContent = Regex.Replace(htmlContent, "</?(a|A).*?>", ""); htmlContent = Regex.Replace(htmlContent, "px", ""); Document document = new Document(); // step 2: // we create a writer that listens to the document // and directs a PDF-stream to a file string Path = Server.MapPath("~/Jaram PDF/PDFS/") + "Sample.pdf"; PdfWriter.GetInstance(document, new FileStream(Path, FileMode.Create)); // step 3: we open the document document.Open(); // step 4: we add a paragraph to the document document.Add(new Paragraph(htmlContent.ToString())); System.Xml.XmlTextReader _xmlr = new System.Xml.XmlTextReader(new StringReader(htmlContent)); _xmlr.WhitespaceHandling = WhitespaceHandling.None; ITextHandler xmlHandler = new ITextHandler(document); xmlHandler.Parse(_xmlr); //HtmlParser.Parse(document, _xmlr); // step 5: we close the document document.Close(); but is is showing the html markup of the grid instead of grid in newly generated pdf. if I comment step 4 // step 4: we add a paragraph to the document document.Add(new Paragraph(htmlContent.ToString())); then I get document has no pages. please suggest me solution
I had this problem too, try removing
And see if this works for you...
Participant
952 Points
293 Posts
Mar 27, 2012 04:51 AM|paritoshmmec|LINK
use this:
HtmlForm form = new HtmlForm(); form.Controls.Add(PGGridViewDetail); StringWriter sw = new StringWriter(); HtmlTextWriter hTextWriter = new HtmlTextWriter(sw); form.Controls[0].RenderControl(hTextWriter); string htmlContent = sw.ToString(); htmlContent = Regex.Replace(htmlContent, "</?(a|A).*?>", ""); htmlContent = Regex.Replace(htmlContent, "px", ""); TextReader reader = new StringReader(htmlContent); Document document = new Document(); string Path = Server.MapPath("~/Jaram PDF/PDFS/") + "Sample.pdf"; PdfWriter.GetInstance(document, new FileStream(Path, FileMode.Create)); // step 3: we open the document document.Open(); // step 4: we add a paragraph to the document document.Add(new Paragraph(htmlContent.ToString())); HTMLWorker worker = new HTMLWorker(document); document.Open(); // step 5: parse the html into the document worker.Parse(reader); worker.Close(); document.Close();
Member
153 Points
625 Posts
itextsharp document has no pages
Mar 18, 2012 02:25 AM|sparrow37|LINK
I am trying to generate pdf of my gridview using following code:
HtmlForm form = new HtmlForm(); form.Controls.Add(PGGridViewDetail); StringWriter sw = new StringWriter(); HtmlTextWriter hTextWriter = new HtmlTextWriter(sw); form.Controls[0].RenderControl(hTextWriter); string htmlContent = sw.ToString();
htmlContent = Regex.Replace(htmlContent, "</?(a|A).*?>", "");
htmlContent = Regex.Replace(htmlContent, "px", "");
Document document = new Document();
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
string Path = Server.MapPath("~/Jaram PDF/PDFS/") + "Sample.pdf";
PdfWriter.GetInstance(document, new FileStream(Path, FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add a paragraph to the document
document.Add(new Paragraph(htmlContent.ToString()));
System.Xml.XmlTextReader _xmlr = new System.Xml.XmlTextReader(new StringReader(htmlContent));
_xmlr.WhitespaceHandling = WhitespaceHandling.None;
ITextHandler xmlHandler = new ITextHandler(document);
xmlHandler.Parse(_xmlr);
//HtmlParser.Parse(document, _xmlr);
// step 5: we close the document
document.Close();
but is is showing the html markup of the grid instead of grid in newly generated pdf.
if I comment step 4
// step 4: we add a paragraph to the document document.Add(new Paragraph(htmlContent.ToString()));
then I get document has no pages.
please suggest me solution
Member
213 Points
859 Posts
Re: itextsharp document has no pages
Mar 25, 2012 09:54 AM|billy_111|LINK
I had this problem too, try removing
// step 5: we close the document
document.Close();
And see if this works for you...
Participant
952 Points
293 Posts
Re: itextsharp document has no pages
Mar 27, 2012 04:51 AM|paritoshmmec|LINK
use this:
HtmlForm form = new HtmlForm(); form.Controls.Add(PGGridViewDetail);
StringWriter sw = new StringWriter();
HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);
form.Controls[0].RenderControl(hTextWriter);
string htmlContent = sw.ToString();
htmlContent = Regex.Replace(htmlContent, "</?(a|A).*?>", "");
htmlContent = Regex.Replace(htmlContent, "px", "");
TextReader reader = new StringReader(htmlContent);
Document document = new Document();
string Path = Server.MapPath("~/Jaram PDF/PDFS/") + "Sample.pdf";
PdfWriter.GetInstance(document, new FileStream(Path, FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add a paragraph to the document
document.Add(new Paragraph(htmlContent.ToString()));
HTMLWorker worker = new HTMLWorker(document);
document.Open();
// step 5: parse the html into the document
worker.Parse(reader);
worker.Close();
document.Close();
Thanks and Regards,
Paritosh