使用itextsharp生成pdf文档的问题 [英] Problem in generating pdf docoments using itextsharp
本文介绍了使用itextsharp生成pdf文档的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好专家,
我正在生成pdf文档,但问题是显示了所有内容,但服务器控件除外
Default2.aspx代码
Hello experts,
i m generating pdf document but the problem is all the thing is is displayed except server controls
Code of Default2.aspx
<form id="form1" runat="server">
<asp:PlaceHolder ID="PlaceholderPdf" runat="server">
<div>
<table border="1">
<tr>
<td colspan="2">
aspdotnetcodebook
</td>
</tr>
<tr>
<td>
cell1
</td>
<td>
cell2
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblLabel" runat="server" Text="Label Test">
</td>
</tr>
<tr>
<td>
<asp:Repeater ID="Repeater1" runat="server">
<itemtemplate>
<asp:Label ID="lbl1" runat="server" Text="This is label1">
</itemtemplate>
<itemtemplate>
<asp:Label ID="lbl2" runat="server" Text="This is label2">
</itemtemplate>
<itemtemplate>
<asp:Label ID="lbl3" runat="server" Text="This is label3">
</itemtemplate>
</td>
</tr>
</table>
</div>
</form>
default2.aspx.cs的代码
Code of default2.aspx.cs
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 System.Text.RegularExpressions;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.xml;
using System.Xml;
using System.Net;
using iTextSharp.text.html.simpleparser;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string TheUrl = Server.MapPath("~/default2.aspx");
string response = GetHtmlPage(TheUrl);
CreatePDFDocument(response);
}
static string GetHtmlPage(string strURL)
{
String strResult;
WebResponse objResponse;
WebRequest objRequest = HttpWebRequest.Create(strURL);
objResponse = objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
strResult = sr.ReadToEnd();
sr.Close();
}
return strResult;
}
public void CreatePDFDocument(string strHtml)
{
string strFileName = HttpContext.Current.Server.MapPath("test.pdf");
// step 1: creation of a document-object
Document document = new Document();
// step 2:
// we create a writer that listens to the document
PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
StringReader se = new StringReader(strHtml);
HTMLWorker obj = new HTMLWorker(document);
document.Open();
obj.Parse(se);
document.Close();
ShowPdf(strFileName);
}
public void ShowPdf(string strFileName)
{
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
Response.ContentType = "application/pdf";
Response.WriteFile(strFileName);
Response.Flush();
Response.Clear();
}
}
推荐答案
一个想法:您的目标PDF是文档.您服务器控件"不是文档的一部分.它们仅仅是服务器端编程的产物.从HTML文档的角度来看,这是不存在的.如果将页面加载到客户端并使用浏览器的查看页面源代码",则可以看到实际存在的内容.这是您应该尝试转换为PDF的真实文档,而不是* .aspx页面的服务器端源代码.
另一个想法是在客户端上创建PDF文档.请在下面查看我的评论.
由于必须使用JavaScript,因此需要一个开放源代码的JavaScript库来编写PDF.
看一看: http://alivepdf.bytearray.org/ [
Just an idea: your target PDF is a document. You "server controls" are not parts of a document. They are merely the artifacts of server-side programming. From the stand point of HTML document, this do not exist. You can see what really exists if you load the page on a client side and use browser''s "View Page Source". This is the real document you should try to translate into PDF, not your server-side source code of an *.aspx page.
Another idea is to create a PDF document on the client side. Please see my comment below.
As it has to be JavaScript, you will need an Open-Source JavaScript library for writing PDF.
Look at this one: http://alivepdf.bytearray.org/[^].
—SA
这篇关于使用itextsharp生成pdf文档的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文