【问题标题】:Displaying a pdf to webpage using iTextSharp?使用 iTextSharp 在网页上显示 pdf?
【发布时间】:2011-07-30 03:25:05
【问题描述】:

我正在使用 iTextSharp 生成 pdf。我想在网页上显示它并让用户从在线 pdf 查看器中保存它。 dynamicpdf 有一个 drawtoweb() 方法,但它不是免费使用的,我无法使用 iTextSharp 找到相同的功能。如何显示pdf?

string newFile = "Pdf Document.pdf";
        Document doc = new Document();
        PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(newFile, FileMode.Create));
        doc.AddCreator("Myself");
        doc.AddTitle("Sample PDF Document");
        doc.Open();
        doc.Add(new Paragraph("Hello, World!"));
        doc.Close();

【问题讨论】:

    标签: c# asp.net pdf pdf-generation itextsharp


    【解决方案1】:

    您是否有理由不能像其他任何网站一样只显示 PDF?只需将生成的文件作为具有正确元数据的响应流发送给用户。它将在其默认 PDF 查看器中自动打开,很可能在浏览器中打开。
    据我了解,DrawToWeb method of dynamicpdf 正是它正在做的事情。

    更新:
    一些研究提出了这样的解决方案:
    将 iframe 添加到您的网页:

    <IFrame runat="server" id="iframepdf">
    </IFrame>
    

    并用 PDF 文件填充它:

    iframepdf.Attributes.Add("src", "showpdf.ashx?pdf=" + xyz);
    

    请参阅How <iframe src can read local temp file? 了解更多信息。

    【讨论】:

    • 也许吧。我该怎么做呢?我正在查看 Response.TransmitFile,但它不会接受 Response.TransmitFile(doc);或 Response.TransmitFile("Pdf Document.pdf");
    • 谢谢,但我在 iframe 中无法找到资源。
    【解决方案2】:

    我最终只是购买了 dynamicpdf 的许可证。它比 itextsharp 更容易使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多