如何将 FileSteam 另存为 PDF 文件
我正在使用第三方工具从扫描仪获取扫描内容。单击按钮时,它会执行代码并将内容作为文件流提供。现在我需要将此 FileStream 内容作为 pdf 文件保存到指定文件夹中。
保存后我需要在浏览器中打开文件。如何将 FileStream 保存为 PDF 文件?
I am using a third party tool to get the scanned content from the scanner. On button click it executes the code and gives the content as a FileStream. Now I need to save this FileStream content as a pdf file in to a specified folder.
After saving I need to open the file in browser. How can I save the FileStream as a PDF file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果我理解正确的话,第三方库正在向您提供一个包含扫描文档数据的流,您需要将其写入文件吗?如果是这种情况,您需要在 C# 中查找文件 I/O。这是一个链接和一个示例:
If I'm understanding you correctly, the third party library is handing you a stream containing the data for the scanned document and you need to write it to a file? If that's the case you need to look up file I/O in C#. Here's a link and an example:
您可以将流直接写入响应的输出缓冲区。
因此,如果您在代码中拥有来自扫描仪的文件流。只需从扫描仪文件流中读取字节并将其写入
Response.OutputStream
将 contentType 设置为
application/pdf
确保不返回任何其他内容。用户浏览器将执行现在配置的任何操作,要么保存到磁盘,要么显示在浏览器中。此时,您还可以保存到服务器上的磁盘,以防您需要备份。
我假设您的文件流已经是 pdf,否则您需要使用 itextsharp 之类的东西来创建 pdf。
编辑
这是一些粗略且现成的代码来完成此操作。您需要对其进行整理,例如添加异常捕获以确保文件流得到正确清理。
西蒙
You can write the stream directly to the output buffer of the response.
So if you're at the point in your code where you have the filestream from the scanner. Simply read bytes from the scanner filestream and write them to the
Response.OutputStream
Set the contentType to
application/pdf
Make sure you return nothing else. The users browser will do whatever it is configured to do now, either save to disk or show in the browser. You can also save to disk on the server at this point as well in case you wanted a backup.
I'm assuming your file stream is already a pdf, otherwise you'll need to use something like itextsharp to create the pdf.
Edit
Here's some rough and ready code to do it. You'll want to tidy this up, like adding exception trapping to make sure the file stream gets cleaned up properly.
Simon
您可能想查看 SourceForge 上的 C# PDF 库: http://sourceforge.net/projects/pdflibrary /
You might want to take a look at the C# PDF Library on SourceForge: http://sourceforge.net/projects/pdflibrary/
不确定,但也许检查一下
http://sourceforge.net/projects/itextsharp/
iTextSharp + FileStream = 损坏的 PDF 文件
not sure, but maybe check this
http://sourceforge.net/projects/itextsharp/
iTextSharp + FileStream = Corrupt PDF file
另一个著名的 PDF 库(我过去也使用过)是 iTextSharp。您可以查看 本教程介绍如何将流转换为 PDF,然后让用户下载。
Another prominent PDF library (which I have used in the past as well) is iTextSharp. You can take a look at this tutorial on how to convert your Stream to PDF then have the user download it.