[英]Creating a PDF or 'print report' in ASP.net
有沒有辦法直接使用 ASP.net 創建 PDF 文檔或“打印報告”? 因此,用戶在文本框中輸入數據,然后單擊保存,它被保存到 SQL,當他們單擊查看預覽時。 我想給它添加一個打印按鈕,或者一個在“打印報告”中打開它的選項,這樣他們就可以打印一份硬拷貝。 我正在使用 visual studio 並在 C# 中進行編碼,我看到有人說安裝這個或那個,但我想知道是否有一種方法可以在不安裝其他軟件的情況下進行安裝?
您將需要外部資源來生成 pdf。
有一個名為Crystal Reports for Visual Studio的完整解決方案。
你可以嘗試下面的步驟在gridview的基礎上生成一個pdf的文件。
首先,我們需要安裝 nuget package itextSharp
。
其次,這是您可以參考的代碼示例。
protected void btnPrint_Click(object sender, EventArgs e)
{
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("d:\\Test.pdf", FileMode.Create));
doc.Open();//Open Document to write
Paragraph paragraph = new Paragraph("data Exported From Gridview!");
//Create table by setting table value
PdfPTable table = new PdfPTable(3);
DataTable dt = (DataTable)GridView1.DataSource;
//Create Table Header
table.AddCell("Name");
table.AddCell("Age");
table.AddCell("ID");
foreach (GridViewRow rows in GridView1.Rows)
{
string Name = GridView1.Rows[rows.RowIndex].Cells[0].Text;
string Age = GridView1.Rows[rows.RowIndex].Cells[1].Text;
string ID= GridView1.Rows[rows.RowIndex].Cells[2].Text;
//Create Cells
table.AddCell(Name);
table.AddCell(Age);
table.AddCell(ID);
}
doc.Add(paragraph);
doc.Add(table);
doc.Close(); //Close document
//
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + "success" + "');", true);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.