c# - 如何显示没有链接的PDF文件

标签 c# pdf itext

我使用 iTextSharp 在我的 C# 代码中创建了一个 PDF 文件。这是我在 View 中的代码:

 <span style="float:left;text-align:left;">            
                <a class="k-button k-button-icontext k-grid-Patient" id="hrefCheckedPatients" href="#" onclick="getChecked();">Export to PDF</a>&nbsp;            
                <a href="#" id="lnkPdfDownload" style="display:none;" onclick="$(this).hide();">Download Generated PDF</a>            
</span>...and then:
     $.ajax({
                type: "POST",
                url: "/PatientReport/ExportToPDF",
                dataType: "json",
                traditional: true,
                data: { uniqueIds: ids },
                success: function (data) {
                    if (data.success) {                    
                        $('#lnkPdfDownload').show();
                        $('#lnkPdfDownload').attr('href', '/PatientReport/DownloadFile' + '?fName=' + data.fName);
                    } else {
                        $('#lnkPdfDownload').hide();
                    }

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    $('#checkedMsg').text('@ELSORegistry.Resources.Views.Patient.PatientStrings.CheckedError').show();
                    $('#hrefCheckedPatients').blur();
                }
            });

我在 Controller 中的代码:

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ExportToPDF(List<String> uniqueIds)       {

            //step 1: we create a memory stream that listens to the document
            var output = new MemoryStream();            

            PdfWriter writer = PdfWriter.GetInstance(document, output);
            writer.CloseStream = false;
            //Here is the code for generating PDF...
            //The End of ExportToPDF method:
            byte[] byteInfo = output.ToArray();
            output.Write(byteInfo, 0, byteInfo.Length);
            output.Position = 0;          


            var fName = string.Format("File-{0}.pdf", DateTime.Now.ToString("s"));
            Session[fName] = output;

            return Json(new { success = true, fName }, JsonRequestBehavior.AllowGet);

        }
//And the other method in the controller is here:
public ActionResult DownloadFile(string fName)
        {
            var ms = Session[fName] as MemoryStream;
            if (ms == null)
                return new EmptyResult();
            Session[fName] = null;
            return File(ms, "application/pdf", fName);
        }

因此,我单击“导出为 PDF”按钮生成报告,然后显示“下载生成的 PDF”链接,用户应单击该链接以查看报告。我想在不单击链接的情况下显示报告,只需单击按钮即可。怎么做?提前感谢您的帮助。

最佳答案

将 iframe 添加到您的 html 并将该 iframe 的 src 属性设置为 DownloadFile url。

<iframe id="myframe" src="" />
                .
                .   
                .
                success: function (data) {
                    if (data.success) {                    
                        //maybe show the iframe here on this line
                        $('#myframe').attr('src', '/PatientReport/DownloadFile' + '?fName=' + data.fName);
                    } else {
                        //do something else
                    }

                }
                .
                .
                .

关于c# - 如何显示没有链接的PDF文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37603899/

相关文章:

c# - 以存储数据的属性为键的字典

c# - 检测何时创建新的托管线程

php - 如何在php中显示pdf

java - Itext 签名空签名字段

c# - 将字节数组设置为unity texture2d的快速方法

c# - 使用 AES/Hex 将 ColdFusion 加密转换为 C#

r - 将 PDF 中未嵌入的字体替换为同系列其他字体的最佳方法是什么?

php - pdftk表格填写字体调整大小

android-gradle-plugin - 无法使用 Gradle 在 Android Studio 中编译 'com.itextpdf:itextg:5.5.11'

java - ITextPDF - 矩形颜色重叠/隐藏 block 的文本