c# - 在 asp.net 中打印 PDF,无需打印对话框

标签 c# asp.net

我在 ASP.NET 中有一个按钮,当单击它时,它会获取一个 rdlc 文件并生成一个带有打印对话框屏幕的 PDF 文件。我想要的是直接打印它而不需要打印对话框,我知道它可以用 Javascript 完成,但我不知道如何在 javascript 中做到这一点。

    <iframe id="frmPrint" name="IframeName" width="500" height="200" runat="server"    style="display: none"></iframe>   

aspx.cs中的代码

public void PrintTicket()
    {
        string[] streamids;
        string reportType = "PDF";
        string mimeType;
        string encoding;
        //string fileNameExtension = "pdf";
        string extension;

        LocalReport report = new LocalReport();
        //Displays ticket letter and number in ticket
        report.ReportPath = "PrintTicket.rdlc";
        ReportParameter ticket_parameter = new ReportParameter();
        ticket_parameter.Name = "Ticket";
        ticket_parameter.Values.Add(TicketNo);
        report.SetParameters(new ReportParameter[] { ticket_parameter });

        //Displays date and time in ticket
        ReportParameter date = new ReportParameter();
        date.Name = "Date_Time";
        date.Values.Add(System.DateTime.Now.ToString());
        report.SetParameters(new ReportParameter[] { date });

        //Displays branch location in ticket
        ReportParameter location_parameter = new ReportParameter();
        location_parameter.Name = "Location";
        location_parameter.Values.Add(location);
        report.SetParameters(new ReportParameter[] { location_parameter });


        string deviceInfo =
          "<DeviceInfo>" +
          "  <OutputFormat>EMF</OutputFormat>" +
          "  <PageWidth>8.5in</PageWidth>" +
          "  <PageHeight>11in</PageHeight>" +
          "  <MarginTop>0.10in</MarginTop>" +
          "  <MarginLeft>0.02in</MarginLeft>" +
          "  <MarginRight>0.25in</MarginRight>" +
          "  <MarginBottom>0.15in</MarginBottom>" +
          "</DeviceInfo>";

        Warning[] warnings;
        byte[] bytes = report.Render("PDF", deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);

        FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("output2.pdf"), FileMode.Create);
        fs.Write(bytes, 0, bytes.Length);
        fs.Close();

        //Open existing PDF
        Document document = new Document(PageSize.LETTER);
        PdfReader reader = new PdfReader(HttpContext.Current.Server.MapPath("output2.pdf"));
        //Getting a instance of new PDF writer
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(HttpContext.Current.Server.MapPath("Print.pdf"), FileMode.Create));
        document.Open();
        PdfContentByte cb = writer.DirectContent;

        int i = 0;
        int p = 0;
        int n = reader.NumberOfPages;
        iTextSharp.text.Rectangle psize = reader.GetPageSize(1);

        float width = psize.Width;
        float height = psize.Height;

        //Add Page to new document
        while (i < n)
        {
            document.NewPage();
            p++;
            i++;

            PdfImportedPage page1 = writer.GetImportedPage(reader, i);
            cb.AddTemplate(page1, 0, 0);
        }

        //Attach javascript to the document
        PdfAction jAction = PdfAction.JavaScript("this.print(true);\r", writer);
        writer.AddJavaScript(jAction);
        document.Close();

        //Attach pdf to the iframe
        frmPrint.Attributes["src"] = "Print.pdf";
    }  

最佳答案

就像 Steven V 所说,JavaScript 本身永远不会在没有对话框的情况下进行打印。你能想象你的令人毛骨悚然的打印机突然打印奇怪的页面吗?

不过我可以建议一个替代方案。既然您使用的是 ASP.NET,为什么不使用第三方 PDF 生成器,在服务器端生成 PDF 文件,然后通过 ajax 将链接返回给用户。用户将看到一个按钮:“获取 PDF”,他将单击它,然后会出现一个指向真实 PDF 的链接!瞧!

PS:您可以在此处查看一些第三方 pdf 生成器: How to create PDF in ASP.NET

关于c# - 在 asp.net 中打印 PDF,无需打印对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18088053/

相关文章:

c# - 从数据文件启动应用程序

javascript - OpenLayers map 未正确加载

c# - ASP.Net 标识 : Difference between UseOAuthBearerTokens and UseCookieAuthentication?

c# - 移动文件 3 次时出现异常

c# - 我应该如何处理基于 C++ 的 dll 中的错误?

c# - 如何从 ASP.NET 中的 ListView 中删除列标题

c# - 为什么此存储过程不更新数据库中的数据?

asp.net - 如何从一个文本框中获取多个值?

c# - 如果在连接字符串时出错,则表示简写

asp.net - 如何在 Azure 中对多个 Web/辅助角色实例进行内部负载平衡?