2

PDFドキュメントを作成するためにwinnovative html to 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

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 に答える
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>
于 2011-12-05T07:30:28.283 に答える