PDFsharp 保存到 MemoryStream
我想通过其 Save 方法将 PdfSharp.Pdf.PdfDocument 保存到 Stream,但它没有将 PDF 标头设置附加到其中。 因此,当我读回 Stream 并将其返回给用户时,他会看到 PDF 文件无效。 PDFsharp 保存到内存时是否有附加 PDF 标题设置的解决方案?
I want to save a PdfSharp.Pdf.PdfDocument by its Save method to a Stream, but it doesn't attach the PDF header settings to it. So when I read back the Stream and return it to the user, he see that the PDF file is invalid. Is there a solution to attach the PDF header settings when PDFsharp saves to memory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您认为 PdfDocument.Save 有问题,请在 PDFsharp 论坛上报告此问题(但请更具体地说明您的错误描述)。
你的“解决方案”对我来说看起来像是一个黑客。
“pdfRenderer.Save”在内部调用“PdfDocument.Save”。
无论问题是什么 - 您的“解决方案”仍然调用相同的保存例程。
编辑:
要获取包含 PDF 文件的 byte[],您只需调用:
PDFsharp 的早期版本不会重置流位置。
必须调用
所以在从流中读取之前 重置流位置; 当前版本不再需要此操作。
通常可以使用 ToArray 来代替从流中读取。
编辑2:使用
stream.GetBuffer()
代替stream.ToArray()
可能会更有效,但是这个缓冲区通常比PDF文件大,你只需必须使用该缓冲区中的stream.Length
字节。 对于采用byte[]
和长度参数的方法非常有用。If you think there is an issue with PdfDocument.Save, then please report this on the PDFsharp forum (but please be more specific with your error description).
Your "solution" looks like a hack to me.
"pdfRenderer.Save" calls "PdfDocument.Save" internally.
Whatever the problem is - your "solution" still calls the same Save routine.
Edit:
To get a byte[] containing a PDF file, you only have to call:
Early versions of PDFsharp do not reset the stream position.
So you have to call
to reset the stream position before reading from the stream; this is no longer required for current versions.
Using ToArray can often be used instead of reading from the stream.
Edit 2: instead of
stream.ToArray()
it may be more efficient to usestream.GetBuffer()
, but this buffer is usually larger than the PDF file and you only have to usestream.Length
bytes from that buffer. Very useful for method that take abyte[]
along with a length parameter.所以解决方案是:
PdfSharp 附带有 MigraDoc 的东西,但我几乎找不到任何合适的文档/常见问题解答。 经过几个小时的谷歌搜索后,我发现了一个类似这样的片段。 现在可以了。
So the solution:
There is this MigraDoc stuff which comes with PdfSharp, but i hardly found any proper doc/faq for it. After hours of googling i've found a snippet which was something like this. Now it works.
我找到了更简单的解决方案:
来源:
http: //usefulaspandcsharp.wordpress.com/2010/03/09/save-a-pdf-to-a-byte-array-using-pdf-sharpmigradoc/
I found simpler solution:
Source:
http://usefulaspandcsharp.wordpress.com/2010/03/09/save-a-pdf-to-a-byte-array-using-pdf-sharpmigradoc/
对于 MigraDoc(版本 1.30),我可以将其保存为
For MigraDoc (ver 1.30) I could save it with
谢谢米尼奥解决方案。 但对我来说它的工作原理是这样的:
Thanks Misnyo Solution. But for me it works like this: