wpf 将内存中的 PDF 加载到 Telerik:RadPdfViewer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21433807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Load PDF from memory into telerik:RadPdfViewer
提问by Joel
I have a PDF file stored in a database as a byte array. I'm reading the PDF byte array from my database back into my application.
我有一个 PDF 文件作为字节数组存储在数据库中。我正在将数据库中的 PDF 字节数组读回到我的应用程序中。
Now, I'm trying to display the PDF with the RadPdfViewer but it is not working.
现在,我正在尝试使用 RadPdfViewer 显示 PDF,但它不起作用。
Here is my code:
这是我的代码:
byte[] pdfAsByteArray= File.ReadAllBytes(@"C:\Users\Username\Desktop\Testfile.pdf");
//Save "pdfAsByteArray" into database
//...
//Load pdf from database into byte[] variable "pdfAsByteArray"
using (var memoryStream = new MemoryStream(pdfAsByteArray))
{
this.PdfViewer.DocumentSource = new PdfDocumentSource(memoryStream);
}
when I execute the application I just get an empty PdfViewer.
当我执行应用程序时,我只会得到一个空的 PdfViewer。
Question:How do I set the DocumentSource the right way?
问题:如何以正确的方式设置 DocumentSource?
Question:How do I dispose the stream? (note that usingdoesn't works)
问题:如何处理流?(请注意,这using不起作用)
Note: I wan't to avoid things like writing a temp file to disk
注意:我不想避免将临时文件写入磁盘之类的事情
Edit:
编辑:
I figured it out but I am not completely satisfied with this solution:
我想通了,但我对这个解决方案并不完全满意:
Not working:
不工作:
using (var memoryStream = new MemoryStream(pdfAsByteArray))
{
this.PdfViewer.DocumentSource = new PdfDocumentSource(memoryStream);
}
Working:
在职的:
var memoryStream = new MemoryStream(pdfAsByteArray);
this.PdfViewer.DocumentSource = new PdfDocumentSource(memoryStream);
I don't know how teleriks RadPdfViewer component works but I wan't to dispose the Stream.
我不知道 Teleriks RadPdfViewer 组件是如何工作的,但我不想处理 Stream。
回答by Sven Grosen
From the Telerik documentation(particularly with regards to the "Caution" stating that this loading is done asynchronously), I believe this should work while still providing you a way to close the stream (not as cleanly as if you were able to use a usingblock, but still better than leaving it open):
从 Telerik文档(特别是关于说明此加载是异步完成的“警告”),我相信这应该可以工作,同时仍然为您提供关闭流的方法(不像您能够使用using块那样干净),但总比让它打开要好):
//class variable
private MemoryStream _stream;
_stream = new MemoryStream(pdfAsByteArray);
var docSource = new PdfDocumentSource(memoryStream);
docSource.Loaded += (sender, args) => { if (_stream != null) _stream.Dispose();};
this.PdfViewer.DocumentSource = docSource;
I did this free-hand and don't have access to the Telerik API so the exact details of the Loaded event are not available to me.
我是徒手完成的,并且无法访问 Telerik API,因此我无法获得 Loaded 事件的确切详细信息。
EDIT
Here's the relevant details from documentation I found (emphasis mine):
编辑
这是我找到的文档中的相关详细信息(重点是我的):
The PdfDocumentSource loads the document asynchronously. If you want to obtain a reference to the DocumentSource after you have imported a document, you should use the Loaded event of the PdfDocumentSource object to obtain the loaded document. This is also a convenient method that can be used to close the stream if you are loading a PDF from a stream.
PdfDocumentSource 异步加载文档。如果要在导入文档后获取对 DocumentSource 的引用,则应使用 PdfDocumentSource 对象的 Loaded 事件来获取加载的文档。如果您从流加载 PDF,这也是一种方便的方法,可用于关闭流。
回答by Dmitriy Khaykin
You need to implement PdfDocumentSource Loaded event. This is when the stream gets loaded and used up, and can be closed / disposed at that time.
您需要实现 PdfDocumentSource Loaded 事件。这是流被加载和用完的时间,并且可以在那时关闭/处理。

