简体   繁体   English

在 ASP.net 中创建 PDF 或“打印报告”

[英]Creating a PDF or 'print report' in ASP.net

Is there a way of creating a PDF document or 'print report' directly using ASP.net?有没有办法直接使用 ASP.net 创建 PDF 文档或“打印报告”? So the user enters data in the textboxes, then clicks save, it gets saved to SQL and when they click view it previews.因此,用户在文本框中输入数据,然后单击保存,它被保存到 SQL,当他们单击查看预览时。 I want to add a print button to it, or an option for it to open in a 'print report' so they can print a hard copy.我想给它添加一个打印按钮,或者一个在“打印报告”中打开它的选项,这样他们就可以打印一份硬拷贝。 I am using visual studio and coding in C#, I read people saying install this or that, but I want to know is there a way of doing it without installing additional software?我正在使用 visual studio 并在 C# 中进行编码,我看到有人说安装这个或那个,但我想知道是否有一种方法可以在不安装其他软件的情况下进行安装?

You will need an external resource for pdf generation.您将需要外部资源来生成 pdf。

There is a complete solution called Crystal Reports for Visual Studio .有一个名为Crystal Reports for Visual Studio的完整解决方案。

You can try to the following stpes to generate a pdf file based on the gridview.你可以尝试下面的步骤在gridview的基础上生成一个pdf的文件。

First, we need to install the nuget package itextSharp .首先,我们需要安装 nuget package itextSharp

Second, here is code example you can refer to.其次,这是您可以参考的代码示例。

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);
    }

PDF file: PDF 档案: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM