2011-12-02 307 views
2

我正在使用创新的html to pdf转换器来创建PDF文档。我已经添加了一个复选框,供用户选择是否打印或通过电子邮件发送PDF文件。打开打印机对话框

但我无法打开PDF页面来打开打印机对话框。我已经尝试了PrinterDialog类,但这不起作用,也发送一些JavaScript与window.print()没有工作。我搜索了互联网但找不到任何东西。包含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查看器/浏览器的打印按钮。但是我想让我的页面打开打印机对话框或直接将字节发送到打印机,以最小化用户必须执行的操作。

任何想法?

+2

焯芬这个答案http://stackoverflow.com/a/2495430/293712 – Maheep

+0

调用window.print()身体的onload事件应该工作。你可以显示你的代码打印? – mehul9595

+0

我没有任何特定的打印代码。只需要浏览器打开打印机对话框。我曾尝试将window.print()添加到我的头文件,正文onload和转换前的html代码体内。这一切都没有奏效。甚至试图将其放入CMS中页面的页面模板标题中。并试图在我的CMS之外运行它。这一切都没有奏效。 – Pedryk

回答

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> 
1

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

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

+0

那么,我很乐意提供建议。您知道一种将字节[]直接发送到打印机的方法吗? – Pedryk

+0

将在新窗口中打开的页面;你使用相同的流媒体代码。但是,JS从父页面执行,而不是可打印页面。这是一种解决它的方法... –

+0

我认为其中一个问题是因为您更改了内容类型,所以无法处理JavaScript(因为内容类型为PDF),但是父窗口可以... –

0

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