C# 打开打印机对话框

C# 打开打印机对话框,c#,asp.net,winnovative,C#,Asp.net,Winnovative,我正在使用winnovative html到pdf转换器创建pdf文档。我添加了一个复选框,用户可以选择是否打印或通过电子邮件发送pdf文件 但我无法获取pdf页面以打开打印机对话框。我尝试了PrinterDialog类,但这不起作用,同时发送一些带有window.print的javascript也不起作用。我在网上搜索过,但什么也没找到 包含PDF的我的页面具有以下代码: Response.Clear(); Response.ContentType = "application/pdf"; R

我正在使用winnovative html到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();
pdfConverter.ScriptsEnabled = true;
pdfConverter.ScriptsEnabledInImage = true;
pdfConverter.InternetSecurityZone = InternetSecurityZone.LocalMachine;
这将在包含我的pdf页面的浏览器中打开pdf查看器。从这里,用户可以单击pdf查看器/浏览器的“打印”按钮。但是我想让我的页面打开打印机对话框或直接将字节发送到打印机,以最小化用户必须执行的操作


有什么想法吗?

因为您正在播放PDF,所以您的选择有限


我认为最好的方法是使用这种方法:。在新窗口中打开PDF,该新窗口可以对其进行流式处理。如果您使用window.open打开它,您可以从父窗口调用window.print,甚至在完成后关闭窗口。

今天早上解决了这个问题,这似乎只是一个愚蠢的错误。 winnovative转换器有一个用于启用脚本的参数,该参数默认设置为false。将此设置为true使我能够从pdf内部使用javascript

阅读了向我推荐的帖子后,我发现在PDF中使用javascript是可能的。在搜索了更多内容(包括winnovative的常见问题)后,我添加了以下代码:

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();
pdfConverter.ScriptsEnabled = true;
pdfConverter.ScriptsEnabledInImage = true;
pdfConverter.InternetSecurityZone = InternetSecurityZone.LocalMachine;
然后,标题中的javascript就开始工作了

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

实际上,您可以添加一个Acrobat JavaScript代码,以便在查看器中打开PDF文档时执行。选择“打开打印”对话框选项时,可以在中看到一个工作示例。该演示中的相关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();
    }
}

在呼叫窗口中检查此应答。打印已加载的正文应能正常工作。你能出示你的打印代码吗?我没有打印的具体代码。只需要浏览器打开打印机对话框。在转换之前,我已经尝试将window.print添加到我的标题、body onload和html代码体中。这一切都不起作用。甚至试图把它放在CMS中我的页面的页面模板标题中。并试图在我的CMS之外运行它。这一切都不起作用。嗯,我愿意听取建议。您知道将字节[]直接发送到打印机的方法吗?将在新窗口中打开的页面;您使用相同的流代码。但是,JS从父页面执行,而不是从可打印页面执行。这是一种解决问题的方法…我认为其中一个问题是,由于您更改了内容类型,它无法处理JavaScript,因为内容类型是PDF,但父窗口可以…我还应该澄清我在这里的理论,我实际上没有这样做。