如何在ASP.net中打印.rdlc按钮单击

本文关键字:打印 rdlc 按钮 单击 net ASP | 更新日期: 2023-09-27 18:17:17

我有一个web应用程序,当用户点击一个按钮,它打印一个。rdlc直接到打印机没有打印对话框。当我在本地运行它时,它可以正常工作,但是当我将它上传到内部网时,它根本不起作用。有人能解释一下如何在内部网中实现这个功能吗?

更新
        private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
    {
        Stream stream = new MemoryStream();
        m_streams.Add(stream);
        return stream;
    }
    private void Export(LocalReport report)
    {
        string deviceInfo =
          "<DeviceInfo>" +
          "  <OutputFormat>EMF</OutputFormat>" +
          "  <PageWidth>8.5in</PageWidth>" +
          "  <PageHeight>11in</PageHeight>" +
          "  <MarginTop>0.25in</MarginTop>" +
          "  <MarginLeft>0.25in</MarginLeft>" +
          "  <MarginRight>0.25in</MarginRight>" +
          "  <MarginBottom>0.25in</MarginBottom>" +
          "</DeviceInfo>";
        Warning[] warnings;
        m_streams = new List<Stream>();
        report.Render("Image", deviceInfo, CreateStream, out warnings);
        foreach (Stream stream in m_streams)
        {
            stream.Position = 0;
        }
    }
    private void PrintPage(object sender, PrintPageEventArgs ev)
    {
        Metafile pageImage = new Metafile(m_streams[currentPageIndex]);
        ev.Graphics.DrawImage(pageImage,ev.PageBounds);
        currentPageIndex++;
        ev.HasMorePages = (currentPageIndex < m_streams.Count);
    }
    private void Print_Ticket()
    {
        const string printerName = "HPLaser"
        if (m_streams == null || m_streams.Count == 0)
            return;
        PrintDocument printDoc = new PrintDocument();
        printDoc.PrinterSettings.PrinterName = printerName;
        if (!printDoc.PrinterSettings.IsValid)
        {
            string msg = String.Format("Can't find printer '"{0}'".", printerName);
            Console.WriteLine(msg);
            return;
        }
        printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
        printDoc.Print(); //Prints Document
    }
    public void WriteTicket(string ticket_number, string queue)
    {
        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(ticket_number);
        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(queue);
        report.SetParameters(new ReportParameter[] { location_parameter });
        Export(report);
        currentPageIndex = 0;
        Print_Ticket();
    }
    //Clears stream
    public new void Dispose()
    {
        if (m_streams != null)
        {
            foreach (Stream stream in m_streams)
                stream.Close();
        }
    }

如何在ASP.net中打印.rdlc按钮单击

看起来是您的IIS应用程序负责启动打印作业。用户无法控制打印

虽然这是可行的,但您需要首先在IIS服务器上执行以下步骤:

  1. 在IIS服务器上创建本地用户帐户
  2. 使用新用户帐户登录,并设置打印机(确保将其设置为默认打印机)
  3. 打印出一个测试页面(如果不工作,检查服务器使用哪些端口与打印机通信,并确保这些端口是打开的)
  4. 设置IIS应用程序池,使用您创建的新用户帐户的凭据运行(它必须是一个交互式帐户—例如,不要在networkservice或localservice下运行站点)。
  5. 尝试从aspx页面打印

您需要将报告导出为PDF,然后使用iTextSharp打印。客户端还需要安装PDF阅读器。

创建隐藏iFrame:

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

添加ASP。净按钮:

<asp:ImageButton ID="btnPrint" runat="server" OnClick="btnPrint_Click"  />

添加以下引用:

using iTextSharp.text.pdf;
using iTextSharp.text;
using System.IO;

添加以下代码到按钮点击事件:

Warning[] warnings;              
string[] streamids;
string mimeType;
string encoding;
string extension;
byte[] bytes = View.ReportViewer.LocalReport.Render("PDF", null, out mimeType, 
               out encoding, out extension, out streamids, out warnings);
FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("output.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("output.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;
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";

"