简体   繁体   English

使用iTextSharp将pdf显示到网页?

[英]Displaying a pdf to webpage using iTextSharp?

I am generating a pdf using iTextSharp. 我正在使用iTextSharp生成pdf。 I would like to display it on the webpage and let the user save it from the online pdf viewer. 我想在网页上显示它,让用户从在线pdf查看器中保存它。 dynamicpdf has a drawtoweb() method, but it is not free to use and I cannot find the same functionality using iTextSharp. dynamicpdf有drawtoweb()方法,但它不是免费使用,使用iTextSharp的我找不到相同的功能。 How can I display the pdf? 我该如何显示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();

Is there a reason you can't just display the PDF like any other website does? 有没有理由你不能像其他任何网站一样显示PDF? Just send the generated file to the user as a response stream with the correct meta data. 只需将生成的文件作为具有正确元数据的响应流发送给用户。 It will open automatically in its default PDF viewer, most likely inside the browser. 它将在其默认PDF查看器中自动打开,很可能在浏览器中。
As far as I understand the DrawToWeb method of dynamicpdf that's exactly what it is doing. 据我所知, Dynamicpdf的DrawToWeb方法正是它正在做的事情。

UPDATE: 更新:
Some research brought up a solution like this: 一些研究提出了这样的解决方案:
Add an iframe to your web page: 将iframe添加到您的网页:

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

and populate it with the PDF file: 并使用PDF文件填充它:

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

See How <iframe src can read local temp file? 请参阅如何<iframe src可以读取本地临时文件? for more info. 了解更多信息。

I ended up just buying a license to dynamicpdf . 我最后只是购买了dynamicpdf的许可证。 It was much easier to work with than itextsharp. 使用比itextsharp更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM