如何将 FileSteam 另存为 PDF 文件

发布于 2024-11-25 11:35:41 字数 143 浏览 4 评论 0原文

我正在使用第三方工具从扫描仪获取扫描内容。单击按钮时,它会执行代码并将内容作为文件流提供。现在我需要将此 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

就是爱搞怪 2024-12-02 11:35:41

如果我理解正确的话,第三方库正在向您提供一个包含扫描文档数据的流,您需要将其写入文件吗?如果是这种情况,您需要在 C# 中查找文件 I/O。这是一个链接和一个示例:

Stream sourceStream = scanner.GetOutput(); // whereever the source stream is
FileStream targetStream = File.OpenWrite(filename, FileMode.Create());
int bytesRead = 0;
byte[] buffer = new byte[2048];
while (true) {
     bytesRead = sourceStream.Read(buffer, 0, buffer.length);
     if (bytesRead == 0)
         break;
     targetStream.Write(buffer, 0, bytesRead);
}
sourceStream.Close();
targetStream.Close();

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:

Stream sourceStream = scanner.GetOutput(); // whereever the source stream is
FileStream targetStream = File.OpenWrite(filename, FileMode.Create());
int bytesRead = 0;
byte[] buffer = new byte[2048];
while (true) {
     bytesRead = sourceStream.Read(buffer, 0, buffer.length);
     if (bytesRead == 0)
         break;
     targetStream.Write(buffer, 0, bytesRead);
}
sourceStream.Close();
targetStream.Close();
浅紫色的梦幻 2024-12-02 11:35:41

您可以将流直接写入响应的输出缓冲区。

因此,如果您在代码中拥有来自扫描仪的文件流。只需从扫描仪文件流中读取字节并将其写入 Response.OutputStream

将 contentType 设置为 application/pdf

确保不返回任何其他内容。用户浏览器将执行现在配置的任何操作,要么保存到磁盘,要么显示在浏览器中。此时,您还可以保存到服务器上的磁盘,以防您需要备份。

我假设您的文件流已经是 pdf,否则您需要使用 itextsharp 之类的东西来创建 pdf。

编辑
这是一些粗略且现成的代码来完成此操作。您需要对其进行整理,例如添加异常捕获以确保文件流得到正确清理。

    public void SaveToOutput(Stream dataStream)
    {
        dataStream.Seek(0, SeekOrigin.Begin);
        FileStream fileout = File.Create("somepath/file.pdf");

        const int chunk = 512;
        byte[] buffer = new byte[512];

        int bytesread = dataStream.Read(buffer,0,chunk);

        while (bytesread == chunk)
        {
            HttpContext.Current.Response.OutputStream.Write(buffer, 0, chunk);
            fileout.Write(buffer, 0, chunk);
            bytesread = dataStream.Read(buffer, 0, chunk);
        }

        HttpContext.Current.Response.OutputStream.Write(buffer, 0, bytesread);
        fileout.Write(buffer, 0, bytesread);
        fileout.Close();

        HttpContext.Current.Response.ContentType = "application/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.

    public void SaveToOutput(Stream dataStream)
    {
        dataStream.Seek(0, SeekOrigin.Begin);
        FileStream fileout = File.Create("somepath/file.pdf");

        const int chunk = 512;
        byte[] buffer = new byte[512];

        int bytesread = dataStream.Read(buffer,0,chunk);

        while (bytesread == chunk)
        {
            HttpContext.Current.Response.OutputStream.Write(buffer, 0, chunk);
            fileout.Write(buffer, 0, chunk);
            bytesread = dataStream.Read(buffer, 0, chunk);
        }

        HttpContext.Current.Response.OutputStream.Write(buffer, 0, bytesread);
        fileout.Write(buffer, 0, bytesread);
        fileout.Close();

        HttpContext.Current.Response.ContentType = "application/pdf";
    }

Simon

鹤舞 2024-12-02 11:35:41

您可能想查看 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/

北恋 2024-12-02 11:35:41

另一个著名的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文