打开打印机对话框

时间:2011-12-02 11:35:55

标签: c# asp.net winnovative

我正在使用winnovative html to pdf转换器来创建pdf文档。我为用户添加了一个复选框,供他们选择打印或通过电子邮件发送pdf文件。

但是我无法通过pdf页面打开打印机对话框。我已经尝试过PrinterDialog类,但是这不起作用,也发送了一些带有window.print()的javascript无效。我在互联网上搜索但找不到任何东西。

我的包含PDF的页面包含以下代码:

Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "inline;filename=Offerte.pdf");
Response.BufferOutput = true;
Response.AddHeader("Content-Length", downloadBytes.Length.ToString());
Response.BinaryWrite(downloadBytes); //downloadBytes = the byte array created by winnovative converter
Response.End();

这将打开包含我的页面为PDF的浏览器内的pdf查看器。从这里,用户可以单击pdf查看器/浏览器的打印按钮。但我想让我的页面打开打印机对话框或直接将字节发送到打印机,以最大限度地减少用户必须执行的操作。

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

由于您正在流式传输PDF,因此选项有限。

我认为最好的方法是使用这种方法:https://stackoverflow.com/a/2495430/293712。在新窗口中打开PDF(这个新窗口可以流式传输它)。然后,您可以从父窗口调用window.print(如果使用window.open打开它),甚至在完成后关闭窗口。

答案 1 :(得分:0)

今天早上解决了,似乎只是一个愚蠢的错误。 winnovative转换器有一个用于启用脚本的参数,默认情况下设置为false。将此设置为true使我能够在pdf中使用javascript。

在阅读了向我建议的帖子后,我发现必须能够在PDF中使用javascript。在搜索了一些之后,包括来自winnovative的FAQ,我添加了以下代码:

pdfConverter.ScriptsEnabled = true;
pdfConverter.ScriptsEnabledInImage = true;
pdfConverter.InternetSecurityZone = InternetSecurityZone.LocalMachine;

然后标题内的javascript工作了!

<script type="text/javascript">
window.print();
</script>

答案 2 :(得分:0)

您可以实际添加在查看器中打开PDF文档时要执行的Acrobat JavaScript代码。当您选择“打开打印对话框”选项时,可以在Execute Acrobat JavaScript Code when Document is Opened demo中看到一个工作示例。该演示中的相关C#代码复制如下:

protected void convertToPdfButton_Click(object sender, EventArgs e)
{
    // Create a HTML to PDF converter object with default settings
    HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

    // Set license key received after purchase to use the converter in licensed mode
    // Leave it not set to use the converter in demo mode
    htmlToPdfConverter.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";

    Document pdfDocument = null;
    try
    {
        // Convert a HTML page to a PDF document object
        pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(urlTextBox.Text);

        string javaScript = null;
        if (alertMessageRadioButton.Checked)
        {
            // JavaScript to display an alert mesage 
            javaScript = String.Format("app.alert(\"{0}\")", alertMessageTextBox.Text);
        }
        else if (printDialogRadioButton.Checked)
        {
            // JavaScript to open the print dialog
            javaScript = "print()";
        }
        else if (zoomLevelRadioButton.Checked)
        {
            // JavaScript to set an initial zoom level 
            javaScript = String.Format("zoom={0}", int.Parse(zoomLevelTextBox.Text));
        }

        // Set the JavaScript action
        pdfDocument.OpenAction.Action = new PdfActionJavaScript(javaScript);

        // Save the PDF document in a memory buffer
        byte[] outPdfBuffer = pdfDocument.Save();

        // Send the PDF as response to browser

        // Set response content type
        Response.AddHeader("Content-Type", "application/pdf");

        // Instruct the browser to open the PDF file as an attachment or inline
        Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Execute_Acrobat_JavaScript.pdf; size={0}", outPdfBuffer.Length.ToString()));

        // Write the PDF document buffer to HTTP response
        Response.BinaryWrite(outPdfBuffer);

        // End the HTTP response and stop the current page processing
        Response.End();
    }
    finally
    {
        // Close the PDF document
        if (pdfDocument != null)
            pdfDocument.Close();
    }
}
相关问题