1

I have a button in ASP.NET that when its clicked it gets a rdlc file and generates a PDF file with a print dialog screen. What I want is to print it directly without the print dialog, I know it can be done with Javascript but I dont have any idea how to do this in javascript.

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

Code in 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";
    }  
Apollo
  • 1,990
  • 12
  • 44
  • 65
  • 1
    You mean the system print dialog box? That will always be there using Javascript. Could you imagine if anyone could print anything without confirmation? – Steven V Aug 06 '13 at 18:44
  • @StevenV the requirement is to print without the dialog box. – Apollo Aug 06 '13 at 18:48
  • I understand the requirement you have. It isn't going to happen using Javascript. You would need direct access to the printer port on the machine you want to print from. I only see that happening from a desktop application that you've written and installed on the machine you want to print from. – Steven V Aug 06 '13 at 18:55
  • @StevenV that is the challenge its a WebApp and it has to be used that way. Making it in a desktop application is the easy part. – Apollo Aug 06 '13 at 19:03
  • Possible Silverlight plugin with full trust? – Mike Cole Aug 06 '13 at 19:24

2 Answers2

2

Like Steven V said, JavaScript on it`s own will never ever print without a dialog box. Can you imagine your creepy printer suddenly printing strange pages all of the sudden?

I can suggest an alternative however. Since you are using ASP.NET why not use a third party PDF generator, generate a PDF file on the serverside, then ajax back a link to the user. User will see a button: "Get PDF," he will click on it and a link to real PDF appears! Voila!

PS: You can look at some third party pdf-generators here: How to create PDF in ASP.NET

Community
  • 1
  • 1
RealityDysfunction
  • 2,609
  • 3
  • 26
  • 53
  • I have the PDF already, what I want is to print it wiotuh the print dialog box – Apollo Aug 06 '13 at 20:02
  • How would you know which printer to print to? What if I have 4 printers? To be honest, doing this in a web app is difficult because browsers separate web from OS very very well. Are you sure it is a requirement to print without a dialog? – RealityDysfunction Aug 06 '13 at 20:05
  • it prints to default. – Apollo Aug 06 '13 at 20:07
  • I found a way to stop the dialog on Internet Explorer. As far as I know, other browsers cannot do this: http://stackoverflow.com/questions/4292373/javascript-print-without-print-dialog-box – RealityDysfunction Aug 06 '13 at 20:11
  • Let me try it, thats good because I only need internet explorer. – Apollo Aug 06 '13 at 20:40
  • This may be because of IE security settings, vbscript is often considered dangerous code, so regular security settings often do not allow you to run it. Try setting security to LOW in IE. – RealityDysfunction Aug 06 '13 at 21:01
  • This is printing whats on the screen. I want to print the pdf that is in the iframe or in the directory. See code – Apollo Aug 06 '13 at 21:01
  • You cannot specify this as far as I know. Maybe you can use jQuery to hide all the other elements on the page and only leave the iframe before the printing happens. – RealityDysfunction Aug 06 '13 at 21:04
1

I think that a generic solution working for every browser is very difficult to implement, but there are some browser-specific workarounds that may work.

For example in Firefox you can activate silent printing following these steps:

  1. In the address bar type about:config then enter
  2. Right click in the white space and select New > Boolean
  3. Create a new string called print.always_print_silent and set it to True

In Internet Explorer you can use this VBScript sub:

<script language='VBScript'>
Sub Print()
       OLECMDID_PRINT = 6
       OLECMDEXECOPT_DONTPROMPTUSER = 2
       OLECMDEXECOPT_PROMPTUSER = 1
       call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)
End Sub
document.write "<object ID='WB' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>"
</script>

Then you can print via Javascript using:

window.print(); 
Andrea
  • 11,801
  • 17
  • 65
  • 72