2

我正在使用 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();

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

有任何想法吗?

4

3 回答 3

1

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

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

于 2011-12-02T13:26:34.247 回答
0

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

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

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

然后标题中的javascript工作了!

<script type="text/javascript">
window.print();
</script>
于 2011-12-05T07:30:28.283 回答
0

当 PDF 文档在查看器中打开时,您实际上可以添加要执行的 Acrobat JavaScript 代码。当您选择打开打印对话框选项时,您可以在打开文档时执行 Acrobat JavaScript 代码演示中看到一个工作示例。该演示中的相关 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();
    }
}
于 2014-09-08T10:42:22.530 回答