0

In my asp.net 4.0 application I have a word document residing on the back end. When the user choses to print a letter, using ASPose I am filling in the mail merge fields behind the scenes,

I am opening a browser window and displaying a PDF via the code below in Page_Load. I would like to tell it to print so that it opens up the standard Printer Dialog, prints if the user says to do so, then closes that browser window. How can I invoke the print (local printer) from this point?

    using (FileStream sourceStream = new FileStream(pdfFilePath, FileMode.Open, FileAccess.Read))
    {
        using (var memoryStream = new MemoryStream())
        {
            sourceStream.CopyTo(memoryStream);
            byte[] b =  memoryStream.ToArray();
            Response.AddHeader("content-disposition", "inline;filename=" + Path.GetFileName(pdfFilePath));
            Response.ContentType = System.Net.Mime.MediaTypeNames.Application.Pdf;
            Response.OutputStream.Write(b, 0, b.Length);        
        }
    }
Joe
  • 715
  • 2
  • 10
  • 18

1 Answers1

0

One not-so-pretty solution to direct send a command to a printer involves using a Java Applet. In that case, you could generate an URL to your document and pass it as parameter to the applet using Javascript. I use this solution in one of my applications, but I should warn you that is very prone to fail in case of browser security configurations or an outdated JRE.

As you are sending a PDF to the printer, you should use the PDDocument class to load the PDF document and then send it to a PrinterJob:

PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setPrintService("MyPrinterName");

URL uri = new URL("http://example.com/docs/doc.pdf");
PDDocument docPdf = PDDocument.load(uri);
docPdf.silentPrint(printerJob);

In this case you need to know the name of the printer in the client, which ou can obtain using Java as well:

public String[] getPrinters() {
    PrintService[] printers = PrintServiceLookup.lookupPrintServices(null, null);
    String[] printerNames = new String[impressoras.length];

    for (int i = 0; i < printers.length; i++) {
        printerNames[i] = printers[i].getName();
    }

    return printerNames;
}

Remember that you can call any method of the Applet using Javascript, searching in the web you can find how to do it.

Once you obtain the printers from the clent, you could use your own Printer Dialog, so the user could select which printer he/she wants to use.


Some resources you may find useful:
Marcus Vinicius
  • 1,891
  • 1
  • 19
  • 35