3
我嘗試使用下面的代碼生成從我的GridView PDF:iTextSharp的文檔沒有頁
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();
但它顯示網格而不是在新生成的PDF網格的HTML標記。
如果我評論第4步
// step 4: we add a paragraph to the document
document.Add(new Paragraph(htmlContent.ToString()));
然後我得到一個沒有頁的文檔。
任何想法我做錯了什麼?