从 System.IO.Stream 对象创建 pdf 文档?

发布于 2024-11-11 18:28:15 字数 105 浏览 2 评论 0原文

我有 Stream 对象,其中包含我想要在 PDF 文档中显示的数据。

如何从该 Stream 对象创建并保存 PDF 文档? (Stream对象是使用MigraDoc工具创建的)

I have Stream object that contains data that i want to show in PDF document.

How do i create and Save PDF document from that Stream object? (The Stream object is created using MigraDoc tool)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

瘫痪情歌 2024-11-18 18:28:15

我想你可能会感到困惑。 MigraDoc 创建 Pdf 文档。因此,如果您有来自 MigraDoc 的流(来自它的 PdfDocument 对象),我猜测将流保存到磁盘作为“document.pdf”将是最佳选择。

请参阅 Jon Skeets 对此的回答:

如何保存流到 C# 中的文件?

请记住,流只是二进制数据。因此,当您想要读取和写入该数据时,您可以使用 System.IO 将流读取或写入某个位置,无论是磁盘、内存还是网络传输。

我会查看 MigraDoc 示例以获取更多信息:

http://www.pdfsharp.net /wiki/MigraDocSamples.ashx

如果您将其写入 HTTP 输出,那么我将执行以下操作:

byte[] buffer = new byte[8192];

pdfStream.Seek(0, SeekOrigin.Begin);

Response.ClearContent();
Response.ClearHeaders();
Response.BufferOutput = true;
Response.ContentType = "application/pdf";

int bytesRead = pdfStream.Read(buffer, 0, 8192);
while(bytesRead > 0)
{
byte[] buffer2 = new byte[bytesRead];
System.Buffer.BlockCopy(buffer, 0, buffer2, 0, bytesRead);

Response.BinaryWrite(buffer2);
Response.Flush();

bytesRead = pdfStream.Read(buffer, 0, 8192);
}
Response.End();

I think you may be confused. MigraDoc creates Pdf documents. So if you have a stream from MigraDoc (which comes from it's PdfDocument object) I would guess that saving the stream to disk as "document.pdf" would be the best option.

See Jon Skeets answer to this here:

How do I save a stream to a file in C#?

Remember a stream is simply binary data. So when you want to read and write that data you use System.IO to read or write the stream into some location be it disk, memory or a network transmission.

I would take a look at the MigraDoc samples for more info:

http://www.pdfsharp.net/wiki/MigraDocSamples.ashx

if you are writing this to an HTTP output then I would do as follows:

byte[] buffer = new byte[8192];

pdfStream.Seek(0, SeekOrigin.Begin);

Response.ClearContent();
Response.ClearHeaders();
Response.BufferOutput = true;
Response.ContentType = "application/pdf";

int bytesRead = pdfStream.Read(buffer, 0, 8192);
while(bytesRead > 0)
{
byte[] buffer2 = new byte[bytesRead];
System.Buffer.BlockCopy(buffer, 0, buffer2, 0, bytesRead);

Response.BinaryWrite(buffer2);
Response.Flush();

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