如何使用asp.net c#直接打印使用iTextSharp动态创建的PDF?(How to directly print a PDF dynamically created with iTextSh

编程入门 行业动态 更新时间:2024-10-24 21:31:50
如何使用asp.net c#直接打印使用iTextSharp动态创建的PDF?(How to directly print a PDF dynamically created with iTextSharp using asp.net c#?)

我环顾了很多,我似乎找不到适合我的方法。 我正在创建一个网站,我有两个按钮,一个下载按钮(工作正常)和一个打印按钮,可以打印PDF(甚至只需在打开对话框打开的Adobe打开PDF)。

我的问题和其他许多问题之间的最大区别在于我没有尝试创建新文档。 我的PDF是使用我已经创建的模板PDF生成的。

我只需要一种打印文档的方法。 我无法将它保存在服务器上,因为我希望客户端能够打印它。 我试过了一个MemoryStream,但它不工作(授予我可能没有正确写入,代码如下)。 标题从另一页抓取。

using (var ms = new MemoryStream()) { Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", header); / Response.Buffer = true; Response.Clear(); var bytes = ms.ToArray(); PdfReader r = new PdfReader(template); using (PdfStamper ps = new PdfStamper(r, Response.OutputStream)) { AcroFields af = ps.AcroFields; ... ps.FormFlattening = true; } Response.OutputStream.Write(bytes, 0, bytes.Length); Response.OutputStream.Flush();

同样,我希望能够让客户端打印一旦点击“打印”按钮后生成的PDF。

I have looked around a lot and i cant seem to find a way that works for me. I am creating a website, and I have two buttons, a Download button (which works fine) and a Print button that should print the PDF (or even just open the PDF in adobe with the print dialog open).

The biggest difference between my question and many others is that I'm not trying to create a new document. My PDF is generated using a template PDF that I have created already.

I just need a way to print the document. I cannot save it on the server because I want the client to be able to print it. I have tried a MemoryStream, but its not working(granted I probably didnt write it correctly, the code is below). The header is grabbed from a different page.

using (var ms = new MemoryStream()) { Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", header); / Response.Buffer = true; Response.Clear(); var bytes = ms.ToArray(); PdfReader r = new PdfReader(template); using (PdfStamper ps = new PdfStamper(r, Response.OutputStream)) { AcroFields af = ps.AcroFields; ... ps.FormFlattening = true; } Response.OutputStream.Write(bytes, 0, bytes.Length); Response.OutputStream.Flush();

Again, I want to be able to have the client print this PDF that is generated once they click the Print button.

最满意答案

在走这条道路之前,我只想确保一些事情得到妥善解释。 我不知道你的专业水平,所以如果我说的话很明显,请不要被冒犯。

首先,你说你有两个按钮,“下载”和“打印”。 我们知道您打算如何执行这些操作,但重要的是要了解并理解这两个选项均为“下载”。 你的“下载”按钮是真正的“下载和(可能)提示保存”,你的“打印”按钮是真正的“下载,然后如果人的浏览器中配置了PDF渲染器(大多数现代浏览器) PDF另有提示保存“。 这可能已经非常明显,但我只是想确保它很清楚。 我个人没有绑定到默认浏览器的PDF渲染器,因此每次都为我下载所有PDF。

其次,服务器为了响应请求,只能将一个“事物”发送回客户端。 这个“东西”可以有元数据(标题)与它相关联,但有很少的标准元键在那里是相关的。 一个关键(内容处置)是应该将这个响应相对于发起请求作为“内联”还是“附件”处理。 一些浏览器将后者视为“下载和(可能)保存提示”,但仍有一些浏览器会启动关联的应用程序。 还有另一个关键(MIME类型的application/octet-stream ),有些人用来进一步欺骗浏览器,基本上说“认真的老兄,我发送的这个东西太奇怪了,以至于你不可能弄清楚将其保存到磁盘“。

这一切都非常重要,因为没有“请打印这个”东西,你可以从任何标准规格的服务器发送(或者我见过的任何规范)。 另外,服务器甚至不能说“在新标签页或浏览器窗口中打开它”。 打印和窗口/选项卡控件都是客户端功能,因此您需要从这个角度进行攻击。

所以现在开始一些选择。 这些选项都假设您的客户端具有内置和启用PDF呈现器的现代浏览器。

您可以使用的一种选择是将PDF封装在带有一些JavaScript 的iframe ,该JavaScript在加载时调用window.print() 。 谷歌过去(可能还是)他们的日历。 您需要使用各种客户端渲染设备进行测试,以确保其按照您的期望工作。

另一个选择是尝试使用PDFObject来嵌入PDF,然后使用上面提到的相同的JavaScript。

最后一个选择是稍微更改您的PDF生成代码, 并将JavaScript添加到您的PDF中 , 尝试在加载时打印PDF。 PDF渲染器中的JavaScript支持不是通用的,因此您需要检查它是否适用于您的客户。 另外,这个设置被“烘焙”成你制作的PDF,所以如果有人把它保存到磁盘上,每次打开它都会尝试打印,这非常烦人。

Before going down this path too far I just want to make sure that some things are properly explained. I don't know your level of expertise so please don't be offended if I say something that is obvious.

First, you say that you have two buttons, "Download" and "Print". We know what you intend for these to do however its important to know and understand that both of these options are "download". Your "Download" button is really "download and (maybe) prompt to save" and your "Print" button is really "download and then if the person has a PDF renderer configured in their browser (which most modern browsers have) then render the PDF otherwise prompt to save". This might be very obvious already to you but I just wanted to make sure it was clear. I personally don't have a PDF renderer bound to my default browser so all PDFs download for me every time.

Second, the server gets to send one and only one "thing" back to the client in response to a request. That "thing" can have meta data (headers) associated with it but there are very few standard meta keys out there that are relevant. One key (content-disposition) is whether this response should be treated, relative to the initiating request, as "inline" or an "attachment". Some browsers treat the latter as a "download and (maybe) prompt to save" but some still will launch an associated application. There's another key (mime type of application/octet-stream) that some people use to further trick browsers that essentially says "seriously dude, this thing I'm sending is so weird that it would be impossible for you to figure it out so just save it to disk".

This is all very important because there's no "please print this" thing that you can send from the server that's in any standard spec (or any spec that I've ever seen for that matter). Also, the server cannot even say "open this in a new tab or browser window". Both print and window/tab control are client-side features so you need to attack from that angle.

So now on to some options. These options all assume that your clients have modern browsers with PDF renderers built-in and enabled.

One option that you could use would be to wrap your PDF in an iframe that has some JavaScript that calls window.print() upon loading. Google used to (and maybe still does) for their calendar. You'll need to test this with your various client rendering devices to make sure it works as you expect.

Another option is to try using PDFObject to embed the PDF and then use the same JavaScript mentioned above.

One last option is to slightly change your PDF generation code and add JavaScript to your PDF the tries to print the PDF on load. JavaScript support in PDF renderers isn't universal so you'll need to check whether this works with your clients. Also, this setting is "baked" into the PDF that you make, so if someone saves it to disk, every time they open it it will try to print which is very annoying.

更多推荐

本文发布于:2023-07-18 04:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154922.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   动态   net   asp   created

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!