1
這是工作和使用itextsharp在新窗口中打開PDF中的正確信息。 但第一列太寬,第二,三,四列太小。 我如何設計它?itextsharp中的gridview列寬pdf工作需要樣式
<form id="f1">
<asp:ImageButton BorderStyle="0" CssClass="submitbutton" runat="server" AlternateText="Get PDF" ID="LinkButton1" OnClick="btnGenerateReport" />
<asp:GridView BackColor="#FFFFFF" ID="gvEventCaf" runat="server" >
</asp:GridView>
</form>
這裏是get PDF的onclick代碼隱藏:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.IO;
protected void btnGenerateReport(object sender, EventArgs e)
{
MyPage tmpPage = new MyPage();
HtmlForm form = new HtmlForm();
form.Controls.Add(gvEventCaf);
tmpPage.Controls.Add(form);
StringWriter sw = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
form.Controls[0].RenderControl(htmlWriter);
string htmlContent = sw.ToString();
Document document = new Document();
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.GetInstance(document, new FileStream("c:\\users\\My\\Downloads\\Calendar.pdf", 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));
HtmlParser.Parse(document, _xmlr);
// step 5: we close the document
document.Close();
ShowPdf("c:\\users\\Me\\Downloads\\Calendar.pdf");
}
這裏的地方我看到了PDF:
private void ShowPdf(string s)
{
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=" + s);
Response.ContentType = "application/pdf";
Response.WriteFile(s);
Response.Flush();
Response.Clear();
}
這裏是Mypage.cs幫助文件我把在App_Code中:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for MyPage
/// </summary>
public class MyPage : Page
{
public override void VerifyRenderingInServerForm(Control control)
{
GridView grid = control as GridView;
if (grid != null && grid.ID == "gvEventCaf")
return;
else
base.VerifyRenderingInServerForm(control);
}
}
,如果你刪除的Asp.Net這將有助於標籤並向我們展示了與任何支持CSS一起呈現的實際(縮寫)HTML。 –
我編輯了我的gridview,它仍然拉動正確的信息。完美的作品。無論我如何在頁面上設置樣式,它都不會複製到PDF中。我在想,任何CSS變化都必須在「btnGenerateReport」 – user2600293
再次,沒有看到HTML和CSS,我們不能幫助太多。 –